#include <stdio.h>
int main()
{
char buf[100];
char s[100];
int x = 1;
fgets(s, 100, stdin);
snprintf(buf, sizeof buf, s);
printf("Buffer size is: (%d) \nData input: %s \n", strlen(buf), buf );
printf("X equals: %d/ in hex: %x\nMemory address for x: (%p) \n", x, x, &x);
return 0;
}
When I run this simple c program, the program begins executing, waits for stdin input, and then executes the print statements.
Everything works as expected normally, but when I enter '%n' to stdin, I receive:
*** %n in writable segment detected ***
Aborted
What is happening, and why is this input to fgets() causing this?
snprintf(buf, sizeof buf, "%n");without further argument... the last argument is the format. you have to specify more arguments. - Jean-François Fabrefgets. It's thesnprintf. Theresis interpreted as a format string. Trysnprintf(buf,sizeof buf,"%s",s)instead - Craig Estey