Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: the second arg of strtoul() changed to NULL, instead of 0.

...

Code Block
bgColor#ffcccc
char** create_table(void) {
  const char* const lenstr = getenv("TABLE_SIZE");
  const size_t length = lenstr ? strtoul(lenstr, 0NULL, 10) : 0;

  if (length > SIZE_MAX / sizeof(char *))
    return NULL;   /* indicate error to caller */

  const size_t table_size = length * sizeof(char *);
  char** const table = (char **)malloc(table_size);

  if (table == NULL)
    return NULL;   /* indicate error to caller */

  /* initialize table... */
  return table;
}

...

Code Block
bgColor#ccccff
enum { MAX_TABLE_LENGTH = 256 };

char** create_table(void) {
  const char* const lenstr = getenv("TABLE_SIZE");
  const size_t length = lenstr ? strtoul(lenstr, 0NULL, 10) : 0;

  if (length == 0 || length > MAX_TABLE_LENGTH)
    return NULL;   /* indicate error to caller */

  const size_t table_size = length * sizeof(char *);
  char** const table = (char **)malloc(table_size);

  if (table == NULL)
    return NULL;   /* indicate error to caller */

  /* initialize table... */
  return table;
}

...