Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
langc
static int yy_string_get() {
  register char *c_str;
  register int c;

  c_str = bash_input.location.string;
  c = EOF;

  /* If the string doesn't exist, or is empty, EOF found. */
  if (c_str && *c_str) {
    /* Cast to unsigned type. */
    c = (unsigned char)*c_str++;

    bash_input.location.string = c_str;
  }
  return (c);
}

...

In this noncompliant example, the cast of *s to unsigned int may result in a value in excess of UCHAR_MAX because of integer promotions, consequently causing the function to violate VOID Guarantee that array indices are within the valid range ARR30-C. Do not form or use out of bounds pointers or array subscripts, leading to undefined behavior:

Code Block
bgColor#ffcccc
langc
#include <limits.h>
#include <stddef.h>
 
static const char table[UCHAR_MAX] = { 'a' /* ... */* };

intptrdiff_t first_not_in_table(const char *c_str) {
  for (const char *s = c_str;
  for (; *s; ++s) {
    if (table[(unsigned)*s] != *s) {
      return s - c_str;
    }
  }
  return -1;
}

Compliant Solution

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
#include <stddef.h>
 
static const char table[UCHAR_MAX] = { 'a' /* ... */* };

ptrdiff_t first_not_in_table(const char *c_str) {
  for (const char *s = c_str;
  for (; *s; ++s) {
    if (table[(unsigned char)*s] != *s) {
      return s - c_str;
    }
  }
  return -1;
}

Risk Assessment

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

...