You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[clang][analyzer] Move security.cert.env.InvalidPtr out of alpha (#71912)
Thanks to recent improvements in #67663, InvalidPtr checker does not
emit any false positives on the following OS projects: memcached, tmux,
curl, twin, vim, openssl, sqlite, ffmpeg, postgres, tinyxml2, libwebm,
xerces, bitcoin, protobuf, qtbase, contour, acid, openrct2. (Before the
changes mentioned above, there were 27 reports, catching the `getenv`
invalidates previous `getenv` results cases. That strict behaviour is
disabled by default)
Copy file name to clipboardExpand all lines: clang/docs/analyzer/checkers.rst
+69-69Lines changed: 69 additions & 69 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -755,6 +755,75 @@ security
755
755
756
756
Security related checkers.
757
757
758
+
.. _security-cert-env-InvalidPtr:
759
+
760
+
security.cert.env.InvalidPtr
761
+
""""""""""""""""""""""""""""""""""
762
+
763
+
Corresponds to SEI CERT Rules `ENV31-C <https://wiki.sei.cmu.edu/confluence/display/c/ENV31-C.+Do+not+rely+on+an+environment+pointer+following+an+operation+that+may+invalidate+it>`_ and `ENV34-C <https://wiki.sei.cmu.edu/confluence/display/c/ENV34-C.+Do+not+store+pointers+returned+by+certain+functions>`_.
764
+
765
+
* **ENV31-C**:
766
+
Rule is about the possible problem with ``main`` function's third argument, environment pointer,
767
+
"envp". When environment array is modified using some modification function
768
+
such as ``putenv``, ``setenv`` or others, It may happen that memory is reallocated,
769
+
however "envp" is not updated to reflect the changes and points to old memory
770
+
region.
771
+
772
+
* **ENV34-C**:
773
+
Some functions return a pointer to a statically allocated buffer.
774
+
Consequently, subsequent call of these functions will invalidate previous
775
+
pointer. These functions include: ``getenv``, ``localeconv``, ``asctime``, ``setlocale``, ``strerror``
776
+
777
+
.. code-block:: c
778
+
779
+
int main(int argc, const char *argv[], const char *envp[]) {
780
+
if (setenv("MY_NEW_VAR", "new_value", 1) != 0) {
781
+
// setenv call may invalidate 'envp'
782
+
/* Handle error */
783
+
}
784
+
if (envp != NULL) {
785
+
for (size_t i = 0; envp[i] != NULL; ++i) {
786
+
puts(envp[i]);
787
+
// envp may no longer point to the current environment
788
+
// this program has unanticipated behavior, since envp
789
+
// does not reflect changes made by setenv function.
790
+
}
791
+
}
792
+
return 0;
793
+
}
794
+
795
+
void previous_call_invalidation() {
796
+
char *p, *pp;
797
+
798
+
p = getenv("VAR");
799
+
setenv("SOMEVAR", "VALUE", /*overwrite = */1);
800
+
// call to 'setenv' may invalidate p
801
+
802
+
*p;
803
+
// dereferencing invalid pointer
804
+
}
805
+
806
+
807
+
The ``InvalidatingGetEnv`` option is available for treating ``getenv`` calls as
808
+
invalidating. When enabled, the checker issues a warning if ``getenv`` is called
809
+
multiple times and their results are used without first creating a copy.
810
+
This level of strictness might be considered overly pedantic for the commonly
0 commit comments