CWE-464: Addition of Data Structure Sentinel

Description

The accidental addition of a data-structure sentinel can cause serious programming logic problems.

Submission Date :

July 19, 2006, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Extended Description

Data-structure sentinels are often used to mark the structure of data. A common example of this is the null character at the end of strings or a special sentinel to mark the end of a linked list. It is dangerous to allow this type of control data to be easily accessible. Therefore, it is important to protect from the addition or modification of sentinels.

Example Vulnerable Codes

Example - 1

The following example assigns some character values to a list of characters and prints them each individually, and then as a string. The third character value is intended to be an integer taken from user input and converted to an int.


char *foo;foo=malloc(sizeof(char)*5);foo[0]='a';foo[1]='a';foo[2]=atoi(getc(stdin));foo[3]='c';foo[4]='\0'printf("%c %c %c %c %c \n",foo[0],foo[1],foo[2],foo[3],foo[4]);printf("%s\n",foo);

The first print statement will print each character separated by a space. However, if a non-integer is read from stdin by getc, then atoi will not make a conversion and return 0. When foo is printed as a string, the 0 at character foo[2] will act as a NULL terminator and foo[3] will never be printed.

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.