This week, my colleague discussed one problem:
Code Example 1:
int main()
{
#define Str "This is String."
char dest[1];
char buff[10];
strncpy(dest, Str, sizeof(Str));
printf("Dest: %s\n", dest);
printf("Buff: %s\n", buff);
}
Output:
Dest: This is String.
Buff: his is String.
Code Example 2:
int main()
{
#define Str "This is String."
char dest[1];
strncpy(dest, Str, sizeof(Str));
printf("Dest: %s\n", dest);
}
Conclusion:
Dest: This is String.
*** stack smashing detected ***: ./test terminated
Aborted (core dumped)
I do not understand why I get this conclusion in case 1? since buff is not even used in strncpy, and if I comment on the buff variable, it will detect a stack split, but with the output for dest. Also for buff, why do I get Output as "its like a string".
source
share