J.Doe J.Doe - 2 months ago 21
C Question

Byte '\x09' terminates the string?

I found an unexpected (to me) behaviour between my program and the

echo
command.

Here's my source code:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]){
printf("argv length: %d\n", strlen(argv[1]));
return 0;
}


If I use the byte
0x09
in the argument, it will terminate the string, as if I used the byte
0x00
.

Examples:

user@linux:~$ ./a.out `echo -e '\x41'`
argv length: 1

user@linux:~$ ./a.out `echo -e '\x41\x41'`
argv length: 2

user@linux:~$ ./a.out `echo -e '\x41\x09'`
argv length: 1

user@linux:~$ ./a.out `echo -e '\x41\x09\x41'`
argv length: 1


Can someone explain why does this occur?

Answer

\x09 is the tabulation char. It does not terminate a string, but here you ran your arguments without protecting them.

In the '\x41\x09' case, the tab char was just stripped by the shell.

In the '\x41\x09\0x41' case, the tab char acted as an argument separator, thus creating another argument.

The quoting was consumed by echo, but echo loses it when echoing back, hence the need to adding another quoting.

Try that:

./a.out `echo -e "'\x41\x09\x41'"`

you'll see you get a 3-byte sole argument