flat assembler
Message board for the users of flat assembler.
Index
> High Level Languages > gets after printf, seems to fail |
Author |
|
cthug 26 Apr 2010, 01:28
You have a possible buffer overflow if you input 10 characters(because of the null termination), If not try debugging .
NOTE: most compilers warn about using gets as it is not safe, if you are using gcc, it prints a report of stack smashing if a buffer overflow occurs |
|||
26 Apr 2010, 01:28 |
|
Tyler 26 Apr 2010, 01:53
It's not printf, it's scanf. scanf leaves the (cr)lf in the input buffer, which gets then reads, causing gets to terminate. Use scanf or don't use scanf, you just can't mix it with other stdin function because of it leaving the \n on the buffer and causing you problems.
cthug wrote:
Neither is scanf, or most other commonly used libc input methods. Use fread because it allows you to specify the length of your buffer. |
|||
26 Apr 2010, 01:53 |
|
baldr 26 Apr 2010, 12:44
Tyler,
scanf() works as specified. If programmer wants to skip the rest of input line, he should do this explicitly: "5Hello!\n" is valid input for the original program (almost overflows buffer, though). scanf() is unsafe in the same sense as gets(): if you haven't specified field width, it's your problem, not libc; scanf() provides facility to do so, thus it's safer. ----8<---- sleepsleep, Tyler's right, gets() should be banished. fgets(), probably? |
|||
26 Apr 2010, 12:44 |
|
vid 26 Apr 2010, 18:00
Something might need to be added. Whenever scanf finds blank characters in format string, it skips all blank characters (including EOLs) on the input.
|
|||
26 Apr 2010, 18:00 |
|
baldr 27 Apr 2010, 07:27
vid,
while (!feof(stdin) && '\n'!=getc(stdin)); before gets() does it. |
|||
27 Apr 2010, 07:27 |
|
vid 27 Apr 2010, 08:25
baldr wrote: vid, How so? You mean won't be shown properly in case when both stdin and stdout point to same interactive console? Or what? Quote: and aa can't have leading whitespace; why not " %d " then? (Of course, this eats blank lines, so it may not be desired in some cases) Quote:
I didn't know about this. Is this standard C, or GNU extension? while (!feof(stdin) && '\n'!=getc(stdin)); before gets() does it.[/quote] |
|||
27 Apr 2010, 08:25 |
|
baldr 27 Apr 2010, 08:45
vid,
I mean "AA : " prompt won't be shown until you enter both n and aa values. vid wrote: I didn't know about this. Is this standard C, or GNU extension? fgets() / sscanf seems to be proper solution: first shows that program expects entire lines as input, second parses them. |
|||
27 Apr 2010, 08:45 |
|
revolution 27 Apr 2010, 08:49
Just roll your own proc. Problem solved.
|
|||
27 Apr 2010, 08:49 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.