Is it even possible in the first place? Since checks are usually performed after the function has been executed, I can only assume that this is not possible because by then the buffer would have overflowed, and the check that comes after it would be irrelevant. Is that right?
Asked
Active
Viewed 38 times
0
-
Did you see the large list of suggested existing questions? – Zan Lynx Aug 12 '15 at 00:04
-
1Some more advice: Only put one question into a StackOverflow question. Your second paragraph is a different question. – Zan Lynx Aug 12 '15 at 00:06
-
If you read the line with `fgets()` what would be the reason to read it again with `gets()`? You already have the line of data. – Michael Burr Aug 12 '15 at 00:20
-
I'm probably repeating info here, as for checking in advance of using `gets()`, you can sometimes seek through stdin with `fseek()` (usually not possible if stdin is just terminal input), and determine length of string, but this would never be an ideal solution. Realistically you could make your own function to read in a line and store it in heap memory for arbitrary sized lines, or if you don't mind using a max length use `fgets()`. – Veltas Aug 12 '15 at 00:22
-
1The way to prevent buffer overflows when calling `gets()` is not to call `gets()`. Ever. It's inherently unsafe, and has been removed from the C standard as of 2011. – Keith Thompson Aug 12 '15 at 00:52
-
See also [Why is the `gets()` function so dangerous that it should not be used?](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Jonathan Leffler Aug 12 '15 at 00:56