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