@@ -1016,7 +1016,7 @@ Check the size argument passed into C string functions for common erroneous patt
10161016 .. _unix-cstring-NullArg :
10171017
10181018unix.cstring .NullArg (C)
1019- """""""""""""""""""""""""
1019+ """"""""""""""""""""""""
10201020Check for null pointers being passed as arguments to C string functions:
10211021``strlen, strnlen, strcpy, strncpy, strcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp, wcslen, wcsnlen ``.
10221022
@@ -1026,6 +1026,99 @@ Check for null pointers being passed as arguments to C string functions:
10261026 return strlen(0); // warn
10271027 }
10281028
1029+ .. _unix-StdCLibraryFunctions :
1030+
1031+ unix.StdCLibraryFunctions (C)
1032+ """""""""""""""""""""""""""""
1033+ Check for calls of standard library functions that violate predefined argument
1034+ constraints. For example, according to the C standard the behavior of function
1035+ ``int isalnum(int ch) `` is undefined if the value of ``ch `` is not representable
1036+ as ``unsigned char `` and is not equal to ``EOF ``.
1037+
1038+ You can think of this checker as defining restrictions (pre- and postconditions)
1039+ on standard library functions. Preconditions are checked, and when they are
1040+ violated, a warning is emitted. Postconditions are added to the analysis, e.g .
1041+ that the return value of a function is not greater than 255. Preconditions are
1042+ added to the analysis too, in the case when the affected values are not known
1043+ before the call.
1044+
1045+ For example, if an argument to a function must be in between 0 and 255, but the
1046+ value of the argument is unknown, the analyzer will assume that it is in this
1047+ interval. Similarly, if a function mustn't be called with a null pointer and the
1048+ analyzer cannot prove that it is null, then it will assume that it is non-null.
1049+
1050+ These are the possible checks on the values passed as function arguments:
1051+ - The argument has an allowed range (or multiple ranges) of values. The checker
1052+ can detect if a passed value is outside of the allowed range and show the
1053+ actual and allowed values.
1054+ - The argument has pointer type and is not allowed to be null pointer. Many
1055+ (but not all) standard functions can produce undefined behavior if a null
1056+ pointer is passed, these cases can be detected by the checker.
1057+ - The argument is a pointer to a memory block and the minimal size of this
1058+ buffer is determined by another argument to the function, or by
1059+ multiplication of two arguments (like at function ``fread ``), or is a fixed
1060+ value (for example ``asctime_r `` requires at least a buffer of size 26). The
1061+ checker can detect if the buffer size is too small and in optimal case show
1062+ the size of the buffer and the values of the corresponding arguments.
1063+
1064+ .. code-block :: c
1065+
1066+ #define EOF -1
1067+ void test_alnum_concrete(int v) {
1068+ int ret = isalnum(256); // \
1069+ // warning: Function argument outside of allowed range
1070+ (void)ret;
1071+ }
1072+
1073+ void buffer_size_violation(FILE *file) {
1074+ enum { BUFFER_SIZE = 1024 };
1075+ wchar_t wbuf[BUFFER_SIZE];
1076+
1077+ const size_t size = sizeof(*wbuf); // 4
1078+ const size_t nitems = sizeof(wbuf); // 4096
1079+
1080+ // Below we receive a warning because the 3rd parameter should be the
1081+ // number of elements to read, not the size in bytes. This case is a known
1082+ // vulnerability described by the ARR38-C SEI-CERT rule.
1083+ fread(wbuf, size, nitems, file);
1084+ }
1085+
1086+ int test_alnum_symbolic(int x) {
1087+ int ret = isalnum(x);
1088+ // after the call, ret is assumed to be in the range [-1, 255]
1089+
1090+ if (ret > 255) // impossible (infeasible branch)
1091+ if (x == 0)
1092+ return ret / x; // division by zero is not reported
1093+ return ret;
1094+ }
1095+
1096+ Additionally to the argument and return value conditions, this checker also adds
1097+ state of the value ``errno `` if applicable to the analysis. Many system
1098+ functions set the ``errno `` value only if an error occurs (together with a
1099+ specific return value of the function), otherwise it becomes undefined. This
1100+ checker changes the analysis state to contain such information. This data is
1101+ used by other checkers, for example :ref: `alpha-unix-Errno `.
1102+
1103+ **Limitations **
1104+
1105+ The checker can not always provide notes about the values of the arguments.
1106+ Without this information it is hard to confirm if the constraint is indeed
1107+ violated. The argument values are shown if they are known constants or the value
1108+ is determined by previous (not too complicated) assumptions.
1109+
1110+ The checker can produce false positives in cases such as if the program has
1111+ invariants not known to the analyzer engine or the bug report path contains
1112+ calls to unknown functions. In these cases the analyzer fails to detect the real
1113+ range of the argument.
1114+
1115+ **Parameters **
1116+
1117+ The checker models functions (and emits diagnostics) from the C standard by
1118+ default. The ``ModelPOSIX `` option enables modeling (and emit diagnostics) of
1119+ additional functions that are defined in the POSIX standard. This option is
1120+ disabled by default.
1121+
10291122.. _osx-checkers :
10301123
10311124osx
@@ -2677,101 +2770,7 @@ For a more detailed description of configuration options, please see the
26772770 file. This causes potential true positive findings to be lost.
26782771
26792772alpha.unix
2680- ^^^^^^^^^^^
2681-
2682- .. _alpha-unix-StdCLibraryFunctions :
2683-
2684- alpha.unix .StdCLibraryFunctions (C)
2685- """""""""""""""""""""""""""""""""""
2686- Check for calls of standard library functions that violate predefined argument
2687- constraints. For example, it is stated in the C standard that for the ``int
2688- isalnum(int ch) `` function the behavior is undefined if the value of ``ch `` is
2689- not representable as unsigned char and is not equal to ``EOF ``.
2690-
2691- .. code-block :: c
2692-
2693- #define EOF -1
2694- void test_alnum_concrete(int v) {
2695- int ret = isalnum(256); // \
2696- // warning: Function argument outside of allowed range
2697- (void)ret;
2698- }
2699-
2700- void buffer_size_violation(FILE *file) {
2701- enum { BUFFER_SIZE = 1024 };
2702- wchar_t wbuf[BUFFER_SIZE];
2703-
2704- const size_t size = sizeof(*wbuf); // 4
2705- const size_t nitems = sizeof(wbuf); // 4096
2706-
2707- // Below we receive a warning because the 3rd parameter should be the
2708- // number of elements to read, not the size in bytes. This case is a known
2709- // vulnerability described by the ARR38-C SEI-CERT rule.
2710- fread(wbuf, size, nitems, file);
2711- }
2712-
2713- You can think of this checker as defining restrictions (pre- and postconditions)
2714- on standard library functions. Preconditions are checked, and when they are
2715- violated, a warning is emitted. Post conditions are added to the analysis, e.g .
2716- that the return value must be no greater than 255.
2717-
2718- For example if an argument to a function must be in between 0 and 255, but the
2719- value of the argument is unknown, the analyzer will conservatively assume that
2720- it is in this interval. Similarly, if a function mustn't be called with a null
2721- pointer and the null value of the argument can not be proven, the analyzer will
2722- assume that it is non-null.
2723-
2724- These are the possible checks on the values passed as function arguments:
2725- - The argument has an allowed range (or multiple ranges) of values. The checker
2726- can detect if a passed value is outside of the allowed range and show the
2727- actual and allowed values.
2728- - The argument has pointer type and is not allowed to be null pointer. Many
2729- (but not all) standard functions can produce undefined behavior if a null
2730- pointer is passed, these cases can be detected by the checker.
2731- - The argument is a pointer to a memory block and the minimal size of this
2732- buffer is determined by another argument to the function, or by
2733- multiplication of two arguments (like at function ``fread ``), or is a fixed
2734- value (for example ``asctime_r `` requires at least a buffer of size 26). The
2735- checker can detect if the buffer size is too small and in optimal case show
2736- the size of the buffer and the values of the corresponding arguments.
2737-
2738- .. code-block :: c
2739-
2740- int test_alnum_symbolic(int x) {
2741- int ret = isalnum(x);
2742- // after the call, ret is assumed to be in the range [-1, 255]
2743-
2744- if (ret > 255) // impossible (infeasible branch)
2745- if (x == 0)
2746- return ret / x; // division by zero is not reported
2747- return ret;
2748- }
2749-
2750- Additionally to the argument and return value conditions, this checker also adds
2751- state of the value ``errno `` if applicable to the analysis. Many system
2752- functions set the ``errno `` value only if an error occurs (together with a
2753- specific return value of the function), otherwise it becomes undefined. This
2754- checker changes the analysis state to contain such information. This data is
2755- used by other checkers, for example :ref: `alpha-unix-Errno `.
2756-
2757- **Limitations **
2758-
2759- The checker can not always provide notes about the values of the arguments.
2760- Without this information it is hard to confirm if the constraint is indeed
2761- violated. The argument values are shown if they are known constants or the value
2762- is determined by previous (not too complicated) assumptions.
2763-
2764- The checker can produce false positives in cases such as if the program has
2765- invariants not known to the analyzer engine or the bug report path contains
2766- calls to unknown functions. In these cases the analyzer fails to detect the real
2767- range of the argument.
2768-
2769- **Parameters **
2770-
2771- The checker models functions (and emits diagnostics) from the C standard by
2772- default. The ``ModelPOSIX `` option enables modeling (and emit diagnostics) of
2773- additional functions that are defined in the POSIX standard. This option is
2774- disabled by default.
2773+ ^^^^^^^^^^
27752774
27762775.. _alpha-unix-BlockInCriticalSection :
27772776
@@ -2840,9 +2839,9 @@ pages of the functions and in the `POSIX standard <https://pubs.opengroup.org/on
28402839 return 1;
28412840 }
28422841
2843- The checker :ref: `alpha- unix-StdCLibraryFunctions ` must be turned on to get the
2842+ The checker :ref: `unix-StdCLibraryFunctions ` must be turned on to get the
28442843warnings from this checker. The supported functions are the same as by
2845- :ref: `alpha- unix-StdCLibraryFunctions `. The ``ModelPOSIX `` option of that
2844+ :ref: `unix-StdCLibraryFunctions `. The ``ModelPOSIX `` option of that
28462845checker affects the set of checked functions.
28472846
28482847**Parameters **
0 commit comments