preventing following stdout getting into the stdin-stream

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
 
K

kernelxu

Markus said:
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. May not be what you expected.

int c, i = 0;
char publisher[21];

printf("Enter the publisher (max. 20): ");
scanf("%c", &c);

When I compiled it with GCC, there was an warning:
warning: format '%c' expects type 'char *', but argument 2 has
type 'int *'.
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.


I think it doesn't seem to work. You may miss the first character
because
"scanf()" eats it.
IMHO, it may work to replace "scanf()" with "fflush(stdout)".
 
M

Markus Pitha

Hello,
May not be what you expected.

Obviously, you are right.
I think it doesn't seem to work. You may miss the first character
because
"scanf()" eats it.
IMHO, it may work to replace "scanf()" with "fflush(stdout)".

Yes, that's the problem. I didn't recongnize it first, but I'll try
fflush. Maybe you are right.


Markus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top