The following piece of c code appears to take up to 8 characters in the input and then give segFaults for larger inputs.
int main()
{
char a[1];
printf("Input:\n");
scanf("%s",a);
printf("%s\n",a);
printf("%d\n",strlen(a));
printf("%d\n",sizeof(a));
return 0;
}
Outputs
Case 1:
Input:
aaaaaaaa
aaaaaaaa
8
1
Case 2:
Input:
aaaaaaaaa
aaaaaaaaa
9
1
[1] 15688 segmentation fault (core dumped)
My machine is a 64-bit Intel Linux
Compiler is gcc version 6.1.1 20160802 (GCC)
Sequence of commands performed:
gcc -c -g test.c
- creates output file test.o
gcc -o test test.o
./test
I am a beginner at c programming. Any insight is much appreciated.
On the surface I would expect it to give some error or warning on input of 2 or more characters.
Also, objdump -d test
gave sub $0x10,%rsp
which implies that the stack stores 16 bytes for the main(). So maybe it should be taking 16 characters and not 8 as input.