Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reviewed

...

Therefore, if fgets() returns a non-null pointer, we can assume it actually filled the array with data. However, assuming that it filled the array with a nonempty, null-terminated byte string (NTBS) is erroneous because the data it placed in the array may contain null characters.

...

Code Block
bgColor#FFCCCC
langc
#include <stdio.h>
#include <string.h>
 
void func(void) {
  char buf[BUFSIZ + 1];

  if (fgets(buf, sizeof(buf), stdin) == NULL) {
    /* Handle error */
  }
  buf[strlen(buf) - 1] = '\0';
}

...

Code Block
bgColor#ccccff
langc
#include <stdio.h>
#include <string.h>
 
void func(void) {
  char buf[BUFSIZ + 1];
  char *p;

  if (fgets(buf, sizeof(buf), stdin)) {
    p = strchr(buf, '\n');
    if (p) {
      *p = '\0';
    }
  } else {
    /* Handle error condition */
  }
}

Risk Assessment

Assuming character data has been read can result in an out-of-bounds memory write.

...