Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You won't strike buffer overflow problems if you enter more characters than can be safely stored since <code>fgets</code> restricts the input. It also adds a null terminator (assuming buffer size is greater than 0, of course).</p> <p>However, you <em>will</em> have problems with information being left in the input buffer the next time you try to read something - this is something that users will find very annoying, entering something like <code>hello again</code> and having it treated as two separate inputs like <code>hello ag</code> and <code>ain</code>. And there's no indication given by <code>fgets</code> that it stopped retrieving input before the end of the line so, as far as your code is aware, everything is fine.</p> <p>The major things you need to look out for (re buffer overflows on input) are, at a minimum, <code>scanf</code> with an unbounded <code>%s</code> format string and <code>gets</code>, which has no limiting size argument, neither of which are in your code.</p> <p>If you're looking for a more robust input solution with size limiting, prompting and buffer clearing, check out this code, which provides all those features:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #define OK 0 #define NO_INPUT 1 #define TOO_LONG 2 static int getLine (char *prmpt, char *buff, size_t sz) { int ch, extra; // Get line with buffer overrun protection. if (prmpt != NULL) { printf ("%s", prmpt); fflush (stdout); } if (fgets (buff, sz, stdin) == NULL) return NO_INPUT; // If it was too long, there'll be no newline. In that case, we flush // to end of line so that excess doesn't affect the next call. if (buff[strlen(buff)-1] != '\n') { extra = 0; while (((ch = getchar()) != '\n') &amp;&amp; (ch != EOF)) extra = 1; return (extra == 1) ? TOO_LONG : OK; } // Otherwise remove newline and give string back to caller. buff[strlen(buff)-1] = '\0'; return OK; } </code></pre> <p>&nbsp;</p> <pre><code>// Test program for getLine(). int main (void) { int rc; char buff[10]; rc = getLine ("Enter string&gt; ", buff, sizeof(buff)); if (rc == NO_INPUT) { // Extra NL since my system doesn't output that on EOF. printf ("\nNo input\n"); return 1; } if (rc == TOO_LONG) { printf ("Input too long [%s]\n", buff); rc = getLine ("Hit ENTER to check remains&gt; ", buff, sizeof(buff)); printf ("Excess [%s]\n", buff); return 1; } printf ("OK [%s]\n", buff); return 0; } </code></pre> <p>And, doing some basic tests:</p> <pre><code>pax&gt; ./prog Enter string&gt; [CTRL-D] No input pax&gt; ./prog Enter string&gt; x OK [x] pax&gt; ./prog Enter string&gt; hello OK [hello] pax&gt; ./prog Enter string&gt; hello from earth Input too long [hello fro] Hit ENTER to check remains&gt; [ENTER] Excess [] pax&gt; ./prog Enter string&gt; i am pax OK [i am pax] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload