Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Davide Brini Newsgroups: gnu.bash.bug Subject: When reading less than wanted characters, "read" does not detect NUL bytes Date: Fri, 15 Jun 2018 15:03:21 +0200 Lines: 34 Approved: bug-bash@gnu.org Message-ID: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: usenet.stanford.edu 1529067817 7609 208.118.235.17 (15 Jun 2018 13:03:37 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org X-Mailer: Claws Mail 3.16.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) X-Provags-ID: V03:K1:oaPm6ESs6H8CQHbCNqKUkonYblDKY8k4E8O/Yu72Fv5YDwlvD8f s2qk7jC6A+pD6U0xZGI5WBgP1rh/j6PdE+qxGJfDbnwhjt2JWzx/haKbUE1/qU71YtoWNlX /2DW3UU3iz/4jdthkvjYKowDKMtvDANhgMZ1OsakVbiocdXrrEhI+yLEcrRQJuxYNJ8iwUG UzHdl3SHgZiCUzzNjxwwg== X-UI-Out-Filterresults: notjunk:1;V01:K0:V+QfQhKZ0SM=:1N849hd6fbpUskXJ5AKknB m9Lxm4gSfmM9BDJgONOzBv1VOxY2bt6eiVWx3qn+STYlivWKOdyPplip9hxo4XAEf2BG5ktx8 F1ARxtBF6yCTz84QH8h0VwHnrKaLEf4gTZKxCgI21wSsTA1g88J6/AhGlAt7RZkLvS81WIURV KX5EeG+XvfY7NcWtd7kdgLov1Sb0VmxUh16x4kH2Bud5KJexp2Iw5KHxbkiMgk3qPpt/A5oez LvhSbLqJtC6RH+mC8XBRH0wQtTBb4jzXaPdBLaoiClzBhrC10y50MKMwELy88hxSFaPE5HJWv jcVvNaDGmIVthQcvv8p7n4hLiERP2i2zTtnDfPrEPpk5lQwBgTwV9DATO+h9qH/yvb04vLYol BW5LMQoolXaY3TqoUYxtjjHUnopst5QvwlL+F+zOfJ5fWU68gowS5owt1HZVJLPh/53alTm7x vpmUYamzYlfeC2Vw7LFMZWVSkviKCXSoTKe+aFMO5JIfEzirVQmU2NdZBFgiB1R1vx/YXVIoB J5sUW8fe86W0aRsL7R17lYrkl+oksKaBPRA+gTeOhjdAF+wjZdkkbOUsKSGt/CYs3DNbyCv3l uZxnouGhSWYqG5R8vItbsqVlX9hqWcrlk/pY8oj2Q0Kf2cQU342DW+MAgIh/P+LJWQxV70Cai K0gkABP534uqFuWTdQTemstVCnnRDk3OFqKwtCYLJ1F3ZGiRdmuaJSgBbc8945/sOpN3keQmS wK++6Hhuf81CS0JYQ7Qjgwvxp6itoRvYNW6FdSql1uTsCmvBDdiJ0/DUR9KiSEhZ4+4NSOvot QTvCQ8C X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.15.15 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:14237 Best explained with an example: $ printf 'a\x00\x00bc' | { while IFS= read -d '' -n 1 var; do echo "read: $var, length: ${#var}"; done; } read: a, length: 1 read: , length: 0 read: , length: 0 read: b, length: 1 read: c, length: 1 This is as expected, and allows detecting a NUL in the input when the length of $var is 0 (let's not talk about EOF and non-newline terminated inputs here, this is not the issue). However, if trying to read 2 (or more) characters at a time, it's no longer true: $ printf 'a\x00\x00bc' | { while IFS= read -d '' -n 2 var; do echo "read: $var, length: ${#var}"; done; } read: a, length: 1 read: , length: 0 read: bc, length: 2 I would expect there to be another read of length 0 between the "a" and the "bc". Apologies if this has been already discussed and/or if I'm missing something. $ bash --version GNU bash, version 4.4.23(1)-release (x86_64-unknown-linux-gnu) -- D.