According to the C Standard, Annex J, J.2 [ISO/IEC 9899:2024], the behavior of a program is undefined in the circumstances outlined in the following table. The "Guideline" column in the table identifies the coding practices that address the specific case of undefined behavior (UB). The descriptions of undefined behaviors in the "Description" column are direct quotes from the standard. The parenthesized numbers refer to the subclause of the C Standard (C23) that identifies the undefined behavior.
UB | Class | Description | Guideline |
---|---|---|---|
1 | A "shall" or "shall not" requirement that appears outside of a constraint is violated (clause 4). | ||
2 | A nonempty source file does not end in a new-line character which is not immediately preceded by a backslash character or ends in a partial preprocessing token or comment (5.1.1.2). | ||
3 | Token concatenation produces a character sequence matching the syntax of a universal character name (5.1.1.2). | ||
4 | A program in a hosted environment does not define a function named | ||
5 | The execution of a program contains a data race (5.1.2.5). | ||
6 | A character not in the basic source character set is encountered in a source file, except in an identifier, a character constant, a string literal, a header name, a comment, or a preprocessing token that is never converted to a token (5.2.1). | ||
7 | An identifier, comment, string literal, character constant, or header name contains an invalid multibyte character or does not begin and end in the initial shift state (5.2.2). | ||
8 | The same identifier has both internal and external linkage in the same translation unit (6.2.2). | ||
9 | An object is referred to outside of its lifetime (6.2.4). | ||
10 | The value of a pointer to an object whose lifetime has ended is used (6.2.4). | ||
11 | The value of an object with automatic storage duration is used while the object has an indeterminate representation (6.2.4, 6.7.11, 6.8). | ||
12 | A non-value representation is read by an lvalue expression that does not have character type (6.2.6.1). | ||
13 | A non-value representation is produced by a side effect that modifies any part of the object using an lvalue expression that does not have character type (6.2.6.1). | ||
14 | Two declarations of the same object or function specify types that are not compatible (6.2.7). | ||
15 | A program requires the formation of a composite type from a variable length array type whose size is specified by an expression that is not evaluated (6.2.7). | ||
16 | Conversion to or from an integer type produces a value outside the range that can be represented (6.3.1.4). | ||
17 | Demotion of one real floating type to another produces a value outside the range that can be represented (6.3.1.5). | ||
18 | An lvalue does not designate an object when evaluated (6.3.2.1). | ||
19 | A non-array lvalue with an incomplete type is used in a context that requires the value of the designated object (6.3.2.1). | ||
20 | An lvalue designation an object of automatic storage duration that could have been declared with the register storage class is used in a context that requires the value of the designated object, but the object is uninitialized (6.3.2.1). | ||
21 | An lvalue having array type is converted to a pointer to the initial element of the array, and the array object has | ||
22 | An attempt is made to use the value of a void expression, or an implicit or explicit conversion (except to | ||
23 | Conversion of a pointer to an integer type produces a value outside the range that can be represented (6.3.2.3). | ||
24 | Conversion between two pointer types produces a result that is incorrectly aligned (6.3.2.3). | ||
25 | A pointer is used to call a function whose type is not compatible with the referenced type (6.3.2.3). | EXP37-C | |
26 | An unmatched | ||
27 | A reserved keyword token is used in translation phase 7 or 8 (5.1.1.2) for some purpose other than as a keyword (6.4.1). | ||
28 | A universal character name in an identifier does not designate a character whose encoding falls into one of the specified ranges (6.4.2.1). | ||
29 | The initial character of an identifier is a universal character name designating a digit (6.4.2.1). | ||
30 | Two identifiers differ only in nonsignificant characters (6.4.2.1). | ||
31 | The identifier | ||
32 | The program attempts to modify a string literal (6.4.5). | ||
33 | The characters | ||
34 | A side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object (6.5.1). | ||
35 | An exceptional condition occurs during the evaluation of an expression (6.5.1). | ||
36 | An object has its stored value accessed other than by an lvalue of an allowable type (6.5.1). | ||
37 | A function is defined with a type that is not compatible with the type (of the expression) pointed to by the expression that denotes the called function (6.5.3.3). | ||
38 | A member of an atomic structure or union is accessed (6.5.3.4). | ||
39 | The operand of the unary | ||
40 | A pointer is converted to other than an integer or pointer type (6.5.5). | ||
41 | The value of the second operand of the | ||
42 | If the quotient a/b is not representable, the behavior of both a/b and a%b (6.5.6). | ||
43 | Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object (6.5.7). | ||
44 | Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that points just beyond the array object and is used as the operand of a unary | ||
45 | Pointers that do not point into, or just beyond, the same array object are subtracted (6.5.7). | ||
46 | An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression | ||
47 | The result of subtracting two pointers is not representable in an object of type | ||
48 | An expression is shifted by a negative number or by an amount greater than or equal to the width of the promoted expression (6.5.8). | ||
49 | An expression having signed promoted type is left-shifted and either the value of the expression is negative or the result of shifting would not be representable in the promoted type (6.5.8). | ||
50 | Pointers that do not point to the same aggregate or union (nor just beyond the same array object) are compared using relational operators (6.5.9). | ||
51 | An object is assigned to an inexactly overlapping object or to an exactly overlapping object with incompatible type (6.5.17.2). | ||
52 | An expression that is required to be an integer constant expression does not have an integer type; has operands that are not integer constants, named constants, compound literal constants, enumeration constants, character constants, predefined constants, sizeof expressions whose results are integer constants, alignof expressions, or immediately-cast floating constants; or contains casts (outside operands to sizeof and alignof operators) other than conversions of arithmetic types to integer types (6.6). | ||
53 | A constant expression in an initializer is not, or does not evaluate to, one of the following: a named constant, a compound literal constant, an arithmetic constant expression, a null pointer constant, an address constant, or an address constant for a complete object type plus or minus an integer constant expression (6.6). | ||
54 | An arithmetic constant expression does not have arithmetic type; has operands that are not integer constants, floating constants, named and compound literal constants of arithmetic type, character constants, predefined constants, sizeof expressions whose results are integer constants, or alignof expressions; or contains casts (outside operands to sizeof or alignof operators) other than conversions of arithmetic types to arithmetic types (6.6). | ||
55 | The value of an object is accessed by an array-subscript | ||
56 | An identifier for an object is declared with no linkage and the type of the object is incomplete after its declarator, or after its init-declarator if it has an initializer (6.7). | ||
57 | A function is declared at block scope with an explicit storage-class specifier other than | ||
58 | A structure or union is defined as containing no named members (including those specified indirectly via anonymous structures and unions) (6.7.3.2). | ||
59 | An attempt is made to access, or generate a pointer to just past, a flexible array member of a structure when the referenced object provides no elements for that array (6.7.3.2). | ||
60 | When the complete type is needed, an incomplete structure or union type is not completed in the same scope by another declaration of the tag that defines the content (6.7.3.4). | ||
61 | An attempt is made to modify an object defined with a | ||
62 | An attempt is made to refer to an object defined with a | ||
63 | The specification of a function type includes any type qualifiers (6.7.4). | ||
64 | Two qualified types that are required to be compatible do not have the identically qualified version of a compatible type (6.7.4). | ||
65 | An object which has been modified is accessed through a | ||
66 | A | ||
67 | A function with external linkage is declared with an | ||
68 | A function declared with a _Noreturn function specifier returns to its caller (6.7.5). | ||
69 | The definition of an object has an alignment specifier and another declaration of that object has a different alignment specifier (6.7.6). | ||
70 | Declarations of an object in different translation units have different alignment specifiers (6.7.6). | ||
71 | Two pointer types that are required to be compatible are not identically qualified, or are not pointers to compatible types (6.7.7.2). | ||
72 | The size expression in an array declaration is not a constant expression and evaluates at program execution time to a nonpositive value (6.7.7.3). | ||
73 | In a context requiring two array types to be compatible, they do not have compatible element types, or their size specifiers evaluate to unequal values (6.7.7.23). | EXP39-C | |
74 | A declaration of an array parameter includes the keyword | ||
75 | A storage-class specifier or type qualifier modifies the keyword | ||
76 | In a context requiring two function types to be compatible, they do not have compatible return types, or their parameters disagree in use of the ellipsis terminator or the number and type of parameters (after default argument promotion, when there is no parameter type list) (6.7.7.4). | ||
77 | A declaration for which a type is inferred contains a pointer, array, or function declarators (6.7.10). | ||
78 | A declaration for which a type is inferred contains no or more than one declarators (6.7.10). | ||
79 | The value of an unnamed member of a structure or union is used (6.7.11). | ||
80 | The initializer for a scalar is neither a single expression nor a single expression enclosed in braces (6.7.11). | ||
81 | The initializer for a structure or union object is neither an initializer list nor a single expression that has compatible structure or union type (6.7.11). | ||
82 | |||
83 | A function definition that does not have the asserted property is called by a function declaration or a function pointer with a type that has the unsequenced or reproducible attribute (6.7.13.8). | ||
84 | An identifier with external linkage is used, but in the program there does not exist exactly one external definition for the identifier, or the identifier is not used and there exist multiple external definitions for the identifier (6.9). | ||
85 | A function that accepts a variable number of arguments is defined without a parameter type list that ends with the ellipsis notation (6.9.2). | ||
86 | The | ||
87 | An identifier for an object with internal linkage and an incomplete type is declared with a tentative definition (6.9.3). | ||
88 | A non-directive preprocessing directive is executed (6.10). | ||
89 | The token defined is generated during the expansion of a | ||
90 | The | ||
91 | The character sequence in an | ||
92 | There are sequences of preprocessing tokens within the list of macro arguments that would otherwise act as preprocessing directives (6.10.5). | ||
93 | The result of the preprocessing operator | ||
94 | The result of the preprocessing operator | ||
95 | The | ||
96 | A non- | ||
97 | A | ||
98 | The name of a predefined macro, or the identifier defined, is the subject of a | ||
99 | An attempt is made to copy an object to an overlapping object by use of a library function, other than as explicitly allowed (e.g., | ||
100 | A file with the same name as one of the standard headers, not provided as part of the implementation, is placed in any of the standard places that are searched for included source files (7.1.2). | ||
101 | A header is included within an external declaration or definition (7.1.2). | ||
102 | A function, object, type, or macro that is specified as being declared or defined by some standard header is used before any header that declares or defines it is included (7.1.2). | ||
103 | A standard header is included while a macro is defined with the same name as a keyword (7.1.2). | ||
104 | The program attempts to declare a library function itself, rather than via a standard header, but the declaration does not have external linkage (7.1.2). | ||
105 | The program declares or defines a reserved identifier, other than as allowed by 7.1.4 (7.1.3). | ||
106 | The program removes the definition of a macro whose name begins with an underscore and either an uppercase letter or another underscore (7.1.3). | ||
107 | An argument to a library function has an invalid value or a type not expected by a function with a variable number of arguments (7.1.4). | ||
108 | The pointer passed to a library function array parameter does not have a value such that all address computations and object accesses are valid (7.1.4). | ||
109 | The macro definition of | ||
110 | The argument to the | ||
111 | The | ||
112 | The value of an argument to a character handling function is neither equal to the value of | ||
113 | A macro definition of | ||
114 | Part of the program tests floating-point status flags, sets floating-point control modes, or runs under non-default mode settings, but was translated with the state for the | ||
115 | The exception-mask argument for one of the functions that provide access to the floating-point status flags has a nonzero value not obtained by bitwise OR of the floating-point exception macros (7.6.2). | ||
116 | The | ||
117 | The argument to | ||
118 | The value of the result of an integer arithmetic or conversion function cannot be represented (7.8.2.1, 7.8.2.2, 7.8.2.3, 7.8.2.4, 7.24.6.1, 7.24.6.2, 7.24.1). | ||
119 | The program modifies the string pointed to by the value returned by the | ||
120 | A pointer returned by the setlocale function is used after a subsequent call to the function, or after the calling thread has exited (7.11.1.1). | ||
121 | The program modifies the structure pointed to by the value returned by the | ||
122 | The initializer for an aggregate or union, other than an array initialized by a string literal, is not a brace-enclosed list of initializers for its elements or members (6.7.11).A macro definition of | ||
123 | An argument to a floating-point classification or comparison macro is not of real floating type (7.12.3, 7.12.17). | ||
124 | A macro definition of | ||
125 | An invocation of the | ||
126 | The | ||
127 | After a | ||
128 | The program specifies an invalid pointer to a signal handler function (7.14.1.1). | ||
129 | A signal handler returns when the signal corresponded to a computational exception (7.14.1.1). | ||
130 | A signal handler called in response to SIGFPE , SIGILL , SIGSEGV , or any other implementation-defined value corresponding to a computational exception returns (7.14.1.1). | SIG35-C | |
131 | A signal occurs as the result of calling the | ||
132 | A signal occurs other than as the result of calling the abort or raise function, and the signal handler refers to an object with static or thread storage duration that is not a lock-free atomic object other than by assigning a value to an object declared as volatile sig_atomic_t, or calls any function in the standard library other than the abort function, the_Exit function, the quick_exit function, the functions in <stdatomic.h> (except where explicitly stated otherwise) when the atomic arguments are lock-free, the atomic_is_lock_free function with any atomic argument, or the signal function (for the same signal number) (7.14.1.1). | ||
133 | The value of | ||
134 | A signal is generated by an asynchronous signal handler (7.14.1.1). | ||
135 | The signal function is used in a multi-threaded program (7.14.1.1). | CON37-C | |
136 | A function with a variable number of arguments attempts to access its varying arguments other than through a properly declared and initialized | ||
137 | The macro | ||
138 | A macro definition of | ||
139 | The | ||
140 | The | ||
141 | The type parameter to the | ||
142 | Using a null pointer constant in form of an integer expression as an argument to a . . . function and then interpreting it as a void* or char* (7.16.1.1). | ||
143 | The | ||
144 | The va_start macro is invoked with additional arguments that include unbalanced parentheses, or unrecognized preprocessing tokens (7.16.1.4). | ||
145 | The macro definition of a generic function is suppressed to access an actual function (7.17.1, 7.18). | ||
146 | The type parameter of an offsetof macro defines a new type (7.21). | ||
147 | When program execution reaches an unreachable() macro invocation (7.21.1). | ||
148 | Arbitrarily copying or changing the bytes of or copying from a non-null pointer into a nullptr_t object and then reading that object (7.21.2). | ||
149 | The member designator parameter of an | ||
150 | The argument in an instance of one of the integer-constant macros is not a decimal, octal, or hexadecimal constant, or it has a value that exceeds the limits for the corresponding type (7.22.4). | ||
151 | A byte input/output function is applied to a wide-oriented stream, or a wide character input/output function is applied to a byte-oriented stream (7.23.2). | ||
152 | Use is made of any portion of a file beyond the most recent wide character written to a wide-oriented stream (7.23.2). | ||
153 | The value of a pointer to a | ||
154 | The stream for the | ||
155 | The string pointed to by the mode argument in a call to the | ||
156 | An output operation on an update stream is followed by an input operation without an intervening call to the | ||
157 | An attempt is made to use the contents of the array that was supplied in a call to the | ||
158 | There are insufficient arguments for the format in a call to one of the formatted input/output functions, or an argument does not have an appropriate type (7.23.6.1, 7.23.6.2, 7.31.2.1, 7.31.2.2). | ||
159 | The format in a call to one of the formatted input/output functions or to the | ||
160 | In a call to one of the formatted output functions, a precision appears with a conversion specifier other than those described (7.23.6.1, 7.31.2.1). | ||
161 | A conversion specification for a formatted output function uses an asterisk to denote an argument-supplied field width or precision, but the corresponding argument is not provided (7.23.6.1, 7.31.2.1). | ||
162 | A conversion specification for a formatted output function uses a | ||
163 | A conversion specification for one of the formatted input/output functions uses a length modifier with a conversion specifier other than those described (7.23.6.1, 7.23.6.2, 7.31.2.1,7.31.2.2). | ||
164 | An | ||
165 | An | ||
166 | A | ||
167 | An invalid conversion specification is found in the format for one of the formatted input/output functions, or the | ||
168 | The number of characters or wide characters transmitted by a formatted output function (or written to an array, or that would have been written to an array) is greater than INT_MAX (7.23.6.1, 7.31.2.1). | ||
169 | The number of input items assigned by a formatted input function is greater than | ||
170 | The result of a conversion by one of the formatted input functions cannot be represented in the corresponding object, or the receiving object does not have an appropriate type (7.23.6.2, 7.31.2.2). | ||
171 | A | ||
172 | A | ||
173 | The input item for a | ||
174 | The | ||
175 | The contents of the array supplied in a call to the | ||
176 | The n parameter is negative or zero for a call to fgets or fgetws. (7.23.7.2, 7.31.3.2). | ||
177 | The file position indicator for a binary stream is used after a call to the | ||
178 | The file position indicator for a stream is used after an error occurred during a call to the | ||
179 | A partial element read by a call to the | ||
180 | The | ||
181 | The | ||
182 | A non-null pointer returned by a call to the calloc, malloc, realloc, or aligned_alloc function with a zero requested size is used to access an object (7.24.3). | ||
183 | The value of a pointer that refers to space deallocated by a call to the | ||
184 | The pointer argument to the free or realloc function is unequal to a null pointer and does not match a pointer earlier returned by a memory management function, or the space has been deallocated by a call to free or realloc (7.24.3.3, 7.24.3.7). | ||
185 | The value of the object allocated by the | ||
186 | The values of any bytes in a new object allocated by the | ||
187 | The program calls the exit or quick_exit function more than once, or calls both functions (7.24.4.4, 7.24.4.7). | ERR04-C | |
188 | During the call to a function registered with the atexit or at_quick_exit function, a call is made to the longjmp function that would terminate the call to the registered function (7.24.4.4, 7.24.4.7). | ||
189 | The string set up by the | ||
190 | A signal is raised while the quick_exit function is executing (7.24.4.7). | ||
191 | A command is executed through the | ||
192 | A searching or sorting utility function is called with an invalid pointer argument, even if the number of elements is zero (7.24.5). | ||
193 | The comparison function called by a searching or sorting utility function alters the contents of the array being searched or sorted, or returns ordering values inconsistently (7.24.5). | ||
194 | The array being searched by the | ||
195 | The current conversion state is used by a multibyte/wide character conversion function after changing the | ||
196 | A string or wide string utility function is instructed to access an array beyond the end of an object (7.26.1, 7.31.4). | ||
197 | A string or wide string utility function is called with an invalid pointer argument, even if the length is zero (7.26.1, 7.31.4). | ||
198 | The contents of the destination array are used after a call to the | ||
199 | A sequence of calls of the strtok function is made from different threads (7.26.5.9). | ||
200 | The first argument in the very first call to the | ||
201 | A pointer returned by the strerror function is used after a subsequent call to the function, or after the calling thread has exited (7.26.6.3). | ||
202 | The type of an argument to a type-generic macro is not compatible with the type of the corresponding parameter of the selected function (7.27). | ||
203 | Arguments for generic parameters of a type-generic macro are such that some argument has a corresponding real type that is of standard floating type and another argument is of decimal floating type (7.27). | ||
204 | Arguments for generic parameters of a type-generic macro are such that neither <math.h> and <complex.h> define a function whose generic parameters have the determined corresponding real type (7.27). | ||
205 | A complex argument is supplied for a generic parameter of a type-generic macro that has no corresponding complex function (7.27). | ||
206 | A decimal floating argument is supplied for a generic parameter of a type-generic macro that expects a complex argument (7.27). | ||
207 | A standard floating or complex argument is supplied for a generic parameter of a type-generic macro that expects a decimal floating type argument (7.27). | ||
208 | A non-recursive mutex passed to mtx_lock is locked by the calling thread (7.28.4.3). | ||
209 | The mutex passed to mtx_timedlock does not support timeout (7.28.4.4). | ||
210 | The mutex passed to mtx_unlock is not locked by the calling thread (7.28.4.6). | ||
211 | The thread passed to thrd_detach or thrd_join was previously detached or joined with another thread (7.28.5.3, 7.28.5.6). | ||
212 | The tss_create function is called from within a destructor (7.28.6.1). | ||
213 | The key passed to tss_delete, tss_get, or tss_set was not returned by a call to tss_create before the thread commenced executing destructors (7.28.6.2, 7.28.6.3, 7.28.6.4). | ||
214 | An attempt is made to access the pointer returned by the time conversion functions after the thread that originally called the function to obtain it has exited (7.29.3). | ||
215 | At least one member of the broken-down time passed to asctime contains a value outside its normal range, or the calculated year exceeds four digits or is less than the year 1000 (7.29.3.1). | ||
216 | The argument corresponding to an | ||
217 | In a call to the | ||
218 | An | ||
219 | The value of an argument of type | ||
220 | The | ||
221 | The |
Graphical symbols used in the preceding table:
Symbol | C11 Classification |
---|---|
Critical Undefined Behavior | |
Bounded Undefined Behavior | |
Undefined Behavior (information/confirmation needed) | |
Possible Conforming Language Extension |