Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: off by one errors in our off by one error examples

...

Code Block
bgColor#FFCCCC
langc
#include <stddef.h>
 
enum { ARRAY_SIZE = 32 };
 
void func(void) {
  char dest[ARRAY_SIZE];
  char src[ARRAY_SIZE];
  size_t i;
 
  for (i = 0; src[i] && (i <= sizeof(dest)); ++i) {
    dest[i] = src[i];
  }
  dest[i] = '\0';
}

...

Code Block
bgColor#ccccff
langc
#include <stddef.h>
 
enum { ARRAY_SIZE = 32 };
 
void func(void) {
  char dest[ARRAY_SIZE];
  char src[ARRAY_SIZE];
  size_t i;

  for (i = 0; src[i] && (i < sizeof(dest) - 1); ++i) {
    dest[i] = src[i];
  }
  dest[i] = '\0';
}

...