bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: readarray leaves a NULL char embedded in each element


From: Greg Wooledge
Subject: Re: readarray leaves a NULL char embedded in each element
Date: Mon, 24 Jun 2024 14:15:30 -0400

On Mon, Jun 24, 2024 at 20:01:45 +0200, Davide Brini wrote:
> $ ./printarg "${X[0]}A"
> 65 32 65 0 83
> 
> That is, "A", a space, and "A" again (which is the result of the quoted
> expansion), 0 for the string terminator, and a random 83 which is
> whatever follows in memory (strangely, it seems to be 83 consistently
> though).

As a guess, it might be coming from envp[], the environment variables.
On my system at least, the first environment variable happens to be
SHELL.  And of course 83 is 'S'.

Obviously this isn't a safe thing to do.  The compiler would be within
its rights to arrange for a segfault when reading beyond the end of an
individual argv[] string.  It's just bad luck that it printed garbage
output instead of crashing.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]