M
Markus Pitha
Hello,
Some time ago I tried to find a solution for preventing buffer overflows
in stdin. I thought getc was the solution but today I came to a problem.
I wanted to use my cognitions for a prompt but I recognized that the
following stdout of my program is used as stdin for the stream. So I
couldn't enter text, because the text was already read in (due to this
following stdout).
I thought about stopping the program-flow before stdin reads the whole
following stdout, so I set just a scanf before, and it works without
problems.
int c, i = 0;
char publisher[21];
printf("Enter the publisher (max. 20): ");
scanf("%c", &c);
while ( ( (c = getchar()) != '\n') && (i < 20) ) {
publisher[i++] = c;
publisher = '\0';
}
Although my solution seems to work perfectly, I'm still a little bit
anxious about scanf, because I'm not exactly sure what's happening
between scanf and the line after.
Actually every entry by the user is a buffer overflow, but it's fielded
due to the next line. Did I understand that right? Could there appear
any non-predictable errors or is this safe?
Thanks,
Markus
Some time ago I tried to find a solution for preventing buffer overflows
in stdin. I thought getc was the solution but today I came to a problem.
I wanted to use my cognitions for a prompt but I recognized that the
following stdout of my program is used as stdin for the stream. So I
couldn't enter text, because the text was already read in (due to this
following stdout).
I thought about stopping the program-flow before stdin reads the whole
following stdout, so I set just a scanf before, and it works without
problems.
int c, i = 0;
char publisher[21];
printf("Enter the publisher (max. 20): ");
scanf("%c", &c);
while ( ( (c = getchar()) != '\n') && (i < 20) ) {
publisher[i++] = c;
publisher = '\0';
}
Although my solution seems to work perfectly, I'm still a little bit
anxious about scanf, because I'm not exactly sure what's happening
between scanf and the line after.
Actually every entry by the user is a buffer overflow, but it's fielded
due to the next line. Did I understand that right? Could there appear
any non-predictable errors or is this safe?
Thanks,
Markus