From 79d6622d1f9c160d250d886b5fcef0ded9283443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 08:29:03 +0500 Subject: [PATCH 01/60] [clang-tidy] add `ctime` and `localtime` to `clang-tidy` Closes #107445 --- .../StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp | 4 ++++ clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc | 4 ++++ clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc | 6 ++++++ clang/test/Analysis/cert/env34-c.c | 9 ++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp index 7d465eee7cc0b..72d20b6beb08f 100644 --- a/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp @@ -76,6 +76,10 @@ class InvalidPtrChecker &InvalidPtrChecker::postPreviousReturnInvalidatingCall}, {{CDM::CLibrary, {"asctime"}, 1}, &InvalidPtrChecker::postPreviousReturnInvalidatingCall}, + {{CDM::CLibrary, {"ctime"}, 1}, + &InvalidPtrChecker::postPreviousReturnInvalidatingCall}, + {{CDM::CLibrary, {"localtime"}, 1}, + &InvalidPtrChecker::postPreviousReturnInvalidatingCall}, }; // The private members of this checker corresponding to commandline options diff --git a/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc index 463ce921f0672..aca22f869b529 100644 --- a/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc +++ b/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc @@ -220,6 +220,10 @@ SYMBOL(and, None, ) SYMBOL(and_eq, None, ) SYMBOL(asctime, None, ) SYMBOL(asctime_s, None, ) +SYMBOL(ctime, None, ) +SYMBOL(ctime_s, None, ) +SYMBOL(localtime, None, ) +SYMBOL(localtime_s, None, ) SYMBOL(asin, None, ) SYMBOL(asinf, None, ) SYMBOL(asinh, None, ) diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc index c1927180d3397..7a08a5d3beee7 100644 --- a/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc +++ b/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc @@ -618,6 +618,12 @@ SYMBOL(as_writable_bytes, std::, ) SYMBOL(asctime, std::, ) SYMBOL(asctime, None, ) SYMBOL(asctime, None, ) +SYMBOL(ctime, std::, ) +SYMBOL(ctime, None, ) +SYMBOL(ctime, None, ) +SYMBOL(localtime, std::, ) +SYMBOL(localtime, None, ) +SYMBOL(localtime, None, ) SYMBOL(asin, std::, ) SYMBOL(asin, None, ) SYMBOL(asin, None, ) diff --git a/clang/test/Analysis/cert/env34-c.c b/clang/test/Analysis/cert/env34-c.c index d307f0d8f4bb0..66ba0be4a67bb 100644 --- a/clang/test/Analysis/cert/env34-c.c +++ b/clang/test/Analysis/cert/env34-c.c @@ -15,7 +15,14 @@ lconv *localeconv(void); typedef struct { } tm; -char *asctime(const tm *timeptr); +char *asctime(const tm *timeptr) +; +typedef struct { +} tm; +char *ctime(const tm *timeptr); +typedef struct { +} tm; +struct tm *localtime(struct tm *tm); int strcmp(const char*, const char*); extern void foo(char *e); From f90a0911ca0bf89a26fec3f7fbf7da81ed924419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Sat, 28 Sep 2024 17:20:00 +0200 Subject: [PATCH 02/60] [clang-tidy] add times to clang-tidy add `ctime` and `localtime` to unsafe functions check function --- clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp index a45949314a4ca..214d0ce3623ec 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp @@ -50,6 +50,8 @@ static StringRef getReplacementFor(StringRef FunctionName, StringRef AnnexKReplacementFunction = StringSwitch(FunctionName) .Cases("asctime", "asctime_r", "asctime_s") + .Cases("ctime", "ctime_r") + .Cases("localtime", "localtime_r") .Case("gets", "gets_s") .Default({}); if (!AnnexKReplacementFunction.empty()) From d5d985aae1702b83244959e5e7a4319c2d8b3268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 08:30:17 +0500 Subject: [PATCH 03/60] [clang-tidy] add `ctime` and `localtime` to `clang-tidy` add changes to release notes --- clang-tools-extra/docs/ReleaseNotes.rst | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8f61839af2c80..3e9402d22bdad 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -91,14 +91,12 @@ Improvements to clang-query Improvements to clang-tidy -------------------------- -- Changed the :program:`check_clang_tidy.py` tool to use FileCheck's - ``--match-full-lines`` instead of ``strict-whitespace`` for ``CHECK-FIXES`` - clauses. Added a ``--match-partial-fixes`` option to keep previous behavior on - specific tests. This may break tests for users with custom out-of-tree checks - who use :program:`check_clang_tidy.py` as-is. - -- Improved :program:`clang-tidy-diff.py` script. Add the `-warnings-as-errors` - argument to treat warnings as errors. +- Improved :program:`clang-tidy`'s `--verify-config` flag by adding support for + the configuration options of the `Clang Static Analyzer Checks + `_. + +- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise + happening on certain platforms when interrupting the script. - Fixed bug in :program:`clang-tidy` by which `HeaderFilterRegex` did not take effect when passed via the `.clang-tidy` file. From 32908341010122c465407e725f3066d2dc45ad62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 16:42:07 +0200 Subject: [PATCH 04/60] [clang-tidy] add `ctime` and `localtime` to `clang-tidy` fix: tests --- clang/test/Analysis/cert/env34-c.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/clang/test/Analysis/cert/env34-c.c b/clang/test/Analysis/cert/env34-c.c index 66ba0be4a67bb..ae344a815679e 100644 --- a/clang/test/Analysis/cert/env34-c.c +++ b/clang/test/Analysis/cert/env34-c.c @@ -15,13 +15,8 @@ lconv *localeconv(void); typedef struct { } tm; -char *asctime(const tm *timeptr) -; -typedef struct { -} tm; +char *asctime(const tm *timeptr); char *ctime(const tm *timeptr); -typedef struct { -} tm; struct tm *localtime(struct tm *tm); int strcmp(const char*, const char*); From cc3fa575a43a1b209c73b15124b6c0010560e98d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 16:58:06 +0200 Subject: [PATCH 05/60] undo symbols, since these should be automatically generated --- clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc | 4 ---- clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc | 6 ------ 2 files changed, 10 deletions(-) diff --git a/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc index aca22f869b529..463ce921f0672 100644 --- a/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc +++ b/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc @@ -220,10 +220,6 @@ SYMBOL(and, None, ) SYMBOL(and_eq, None, ) SYMBOL(asctime, None, ) SYMBOL(asctime_s, None, ) -SYMBOL(ctime, None, ) -SYMBOL(ctime_s, None, ) -SYMBOL(localtime, None, ) -SYMBOL(localtime_s, None, ) SYMBOL(asin, None, ) SYMBOL(asinf, None, ) SYMBOL(asinh, None, ) diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc index 7a08a5d3beee7..c1927180d3397 100644 --- a/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc +++ b/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc @@ -618,12 +618,6 @@ SYMBOL(as_writable_bytes, std::, ) SYMBOL(asctime, std::, ) SYMBOL(asctime, None, ) SYMBOL(asctime, None, ) -SYMBOL(ctime, std::, ) -SYMBOL(ctime, None, ) -SYMBOL(ctime, None, ) -SYMBOL(localtime, std::, ) -SYMBOL(localtime, None, ) -SYMBOL(localtime, None, ) SYMBOL(asin, std::, ) SYMBOL(asin, None, ) SYMBOL(asin, None, ) From d372eaf161492ec210954497252dc8b25339e7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 08:31:14 +0500 Subject: [PATCH 06/60] moved release notes for `ctime` and `localtime` --- clang-tools-extra/docs/ReleaseNotes.rst | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 3e9402d22bdad..3debd20e12ebe 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -95,6 +95,8 @@ Improvements to clang-tidy the configuration options of the `Clang Static Analyzer Checks `_. +- Added `ctime` and `localtime` to clang-tidy. + - Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise happening on certain platforms when interrupting the script. @@ -132,6 +134,60 @@ New check aliases Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ +<<<<<<< HEAD +||||||| parent of c62bbb1d2bf2 (moved release notes for `ctime` and `localtime`) +- Improved :doc:`altera-id-dependent-backward-branch + ` check by fixing + crashes from invalid code. + +- Improved :doc:`bugprone-branch-clone + ` check to improve detection of + branch clones by now detecting duplicate inner and outer if statements. + +- Improved :doc:`bugprone-casting-through-void + ` check to suggest replacing + the offending code with ``reinterpret_cast``, to more clearly express intent. + +- Improved :doc:`bugprone-dangling-handle + ` check to treat `std::span` as a + handle class. + +- Improved :doc:`bugprone-exception-escape + ` by fixing false positives + when a consteval function with throw statements. + +- Improved :doc:`bugprone-forwarding-reference-overload + ` check by fixing + a crash when determining if an ``enable_if[_t]`` was found. + +======= +- Improved :doc:`altera-id-dependent-backward-branch + ` check by fixing + crashes from invalid code. + +- Improved :doc:`bugprone-branch-clone + ` check to improve detection of + branch clones by now detecting duplicate inner and outer if statements. + +- Added `ctime` and `localtime` to clang-tidy. + +- Improved :doc:`bugprone-casting-through-void + ` check to suggest replacing + the offending code with ``reinterpret_cast``, to more clearly express intent. + +- Improved :doc:`bugprone-dangling-handle + ` check to treat `std::span` as a + handle class. + +- Improved :doc:`bugprone-exception-escape + ` by fixing false positives + when a consteval function with throw statements. + +- Improved :doc:`bugprone-forwarding-reference-overload + ` check by fixing + a crash when determining if an ``enable_if[_t]`` was found. + +>>>>>>> c62bbb1d2bf2 (moved release notes for `ctime` and `localtime`) - Improved :doc:`bugprone-optional-value-conversion ` check to detect conversion in argument of ``std::make_optional``. From 9eeca774df88a27d14df0f4123ad131fbadf6f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 08:31:51 +0500 Subject: [PATCH 07/60] updated release notes for `ctime` and `localtime` --- clang-tools-extra/docs/ReleaseNotes.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 3debd20e12ebe..b5d28532e54db 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -171,6 +171,9 @@ Changes in existing checks - Added `ctime` and `localtime` to clang-tidy. +- New unsafe functions checks :doc:`bugprone-unsafe-functions-check` + were added to clang-tidy. + - Improved :doc:`bugprone-casting-through-void ` check to suggest replacing the offending code with ``reinterpret_cast``, to more clearly express intent. From 1d2e5ad208722a78a860c54975099a29b9632fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 17:45:33 +0200 Subject: [PATCH 08/60] added `ctime_r` and `localtime_r` to documentation --- .../docs/clang-tidy/checks/bugprone/unsafe-functions.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst index 317db9c5564e2..17c5f9e66c74a 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst @@ -34,7 +34,8 @@ following functions: ``vsnprintf``, ``vsprintf``, ``vsscanf``, ``vswprintf``, ``vswscanf``, ``vwprintf``, ``vwscanf``, ``wcrtomb``, ``wcscat``, ``wcscpy``, ``wcslen``, ``wcsncat``, ``wcsncpy``, ``wcsrtombs``, ``wcstok``, ``wcstombs``, -``wctomb``, ``wmemcpy``, ``wmemmove``, ``wprintf``, ``wscanf``. +``wctomb``, ``wmemcpy``, ``wmemmove``, ``wprintf``, ``wscanf``. ``ctime_r``, +``localtime_r`` If *Annex K.* is not available, replacements are suggested only for the following functions from the previous list: From 668bcf7065b6b1d9fa7abb048b635163cecf4ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 08:32:23 +0500 Subject: [PATCH 09/60] updated release notes for `ctime` and `localtime` --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index b5d28532e54db..db37e75a16733 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -174,6 +174,10 @@ Changes in existing checks - New unsafe functions checks :doc:`bugprone-unsafe-functions-check` were added to clang-tidy. +- Improved :doc:`bugprone-unsafe-functions-check` + ``, added `ctime` and `localtime` + to unsafe functions check in clang-tidy. + - Improved :doc:`bugprone-casting-through-void ` check to suggest replacing the offending code with ``reinterpret_cast``, to more clearly express intent. From 2c2e3db94189652945f015335ee28f3fd617ae3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 08:34:24 +0500 Subject: [PATCH 10/60] fix: release notes for `ctime` and `localtime` --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index db37e75a16733..b57d827f6e6c5 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -174,8 +174,8 @@ Changes in existing checks - New unsafe functions checks :doc:`bugprone-unsafe-functions-check` were added to clang-tidy. -- Improved :doc:`bugprone-unsafe-functions-check` - ``, added `ctime` and `localtime` +- Improved :doc:`bugprone-unsafe-functions + `, added `ctime` and `localtime` to unsafe functions check in clang-tidy. - Improved :doc:`bugprone-casting-through-void From bdf5f2eee258c4d73cef76c318789dff6afe7910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 08:34:57 +0500 Subject: [PATCH 11/60] release notes should be in alphabetical order --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index b57d827f6e6c5..e40e1e9980694 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -211,6 +211,10 @@ Changes in existing checks no longer be needed and will be removed. Also fixing false positive from const reference accessors to objects containing optional member. +- Improved :doc:`bugprone-unsafe-functions + `, added `ctime` and `localtime` + to unsafe functions check in clang-tidy. + - Improved :doc:`bugprone-unsafe-functions ` check to allow specifying additional C++ member functions to match. From 56005566777573d8ad6332d1fdd240c9cd57ff5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 18:00:54 +0200 Subject: [PATCH 12/60] updated release notes --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index e40e1e9980694..660c96e638992 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -212,8 +212,8 @@ Changes in existing checks const reference accessors to objects containing optional member. - Improved :doc:`bugprone-unsafe-functions - `, added `ctime` and `localtime` - to unsafe functions check in clang-tidy. + ` by adding ``ctime`` and + ``localtime`` functions. - Improved :doc:`bugprone-unsafe-functions ` check to allow specifying From b33e2c969eea52d217acbfe7c342d95005340e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 18:29:12 +0200 Subject: [PATCH 13/60] fix: function for unsafe functions check --- .../clang-tidy/bugprone/UnsafeFunctionsCheck.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp index 214d0ce3623ec..0538b5751ad1d 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp @@ -50,8 +50,8 @@ static StringRef getReplacementFor(StringRef FunctionName, StringRef AnnexKReplacementFunction = StringSwitch(FunctionName) .Cases("asctime", "asctime_r", "asctime_s") - .Cases("ctime", "ctime_r") - .Cases("localtime", "localtime_r") + .Case("ctime", "ctime_r") + .Case("localtime", "localtime_r") .Case("gets", "gets_s") .Default({}); if (!AnnexKReplacementFunction.empty()) From 8afbbc4fca3cfdb0c2e05b8717b41d306c8356f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 18:40:23 +0200 Subject: [PATCH 14/60] add `ctime` and `localtime` to standard library checks --- clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp index 0538b5751ad1d..f3a33213df35f 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp @@ -62,6 +62,8 @@ static StringRef getReplacementFor(StringRef FunctionName, // should be matched and suggested. return StringSwitch(FunctionName) .Cases("asctime", "asctime_r", "strftime") + .Case("ctime", "ctime_r") + .Case("localtime", "localtime_r") .Case("gets", "fgets") .Case("rewind", "fseek") .Case("setbuf", "setvbuf"); From f8c70bef66e57cc2055455be541c56df78e7bd4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 18:42:33 +0200 Subject: [PATCH 15/60] added `ctime` and `localtime` to bounds checking checks --- .../clang-tidy/bugprone/UnsafeFunctionsCheck.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp index f3a33213df35f..1baf914488d99 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp @@ -94,8 +94,8 @@ static StringRef getReplacementForAdditional(StringRef FunctionName, /// safer alternative. static StringRef getRationaleFor(StringRef FunctionName) { return StringSwitch(FunctionName) - .Cases("asctime", "asctime_r", "ctime", - "is not bounds-checking and non-reentrant") + .Cases("asctime", "asctime_r", "ctime", "ctime_r", "localtime", + "localtime_r", "is not bounds-checking and non-reentrant") .Cases("bcmp", "bcopy", "bzero", "is deprecated") .Cases("fopen", "freopen", "has no exclusive access to the opened file") .Case("gets", "is insecure, was deprecated and removed in C11 and C++14") From da376205352bb2ba849a7e46a81a72c4792f031c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 18:45:04 +0200 Subject: [PATCH 16/60] added ctime and localtime to functions list --- .../clang-tidy/bugprone/UnsafeFunctionsCheck.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp index 1baf914488d99..e855ae5c3ac9e 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp @@ -228,7 +228,9 @@ void UnsafeFunctionsCheck::registerMatchers(MatchFinder *Finder) { // Matching functions with replacements without Annex K. auto FunctionNamesMatcher = - hasAnyName("::asctime", "asctime_r", "::gets", "::rewind", "::setbuf"); + hasAnyName("::asctime", "asctime_r", "::ctime", "ctime_r", + "::localtime", "localtime_r", "::gets", "::rewind", + "::setbuf"); Finder->addMatcher( declRefExpr( to(functionDecl(FunctionNamesMatcher).bind(FunctionNamesId))) From a120c01997ea8e969f6e66db9f1fa6f77a4d1f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 18:51:15 +0200 Subject: [PATCH 17/60] format code with clang-format --- .../clang-tidy/bugprone/UnsafeFunctionsCheck.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp index e855ae5c3ac9e..d74719e0ef1e0 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp @@ -227,10 +227,9 @@ void UnsafeFunctionsCheck::registerMatchers(MatchFinder *Finder) { } // Matching functions with replacements without Annex K. - auto FunctionNamesMatcher = - hasAnyName("::asctime", "asctime_r", "::ctime", "ctime_r", - "::localtime", "localtime_r", "::gets", "::rewind", - "::setbuf"); + auto FunctionNamesMatcher = hasAnyName( + "::asctime", "asctime_r", "::ctime", "ctime_r", "::localtime", + "localtime_r", "::gets", "::rewind", "::setbuf"); Finder->addMatcher( declRefExpr( to(functionDecl(FunctionNamesMatcher).bind(FunctionNamesId))) From e491609ef719f2596349da0405f16d83eea65597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 19:23:14 +0200 Subject: [PATCH 18/60] fix: tests --- .../clang-tidy/checkers/bugprone/unsafe-functions.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 0409dd6bfcaa3..71e1db7672b24 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -50,6 +50,8 @@ void f1w(wchar_t *S) { struct tm; char *asctime(const struct tm *TimePtr); +char *ctime(const struct tm *TimePtr); +char *localtime(const struct tm *tm); void f2(const struct tm *Time) { asctime(Time); @@ -57,6 +59,16 @@ void f2(const struct tm *Time) { // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + ctime(Time); + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + + localtime(Time); + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + char *(*F1)(const struct tm *) = asctime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead From 8dada6dbdba61c8c592e2863af4f8e4c6608cc47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 19:26:41 +0200 Subject: [PATCH 19/60] fix: tests --- .../checkers/bugprone/unsafe-functions.c | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 71e1db7672b24..635bf62667717 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -60,12 +60,12 @@ void f2(const struct tm *Time) { // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead ctime(Time); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead localtime(Time); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead @@ -78,6 +78,26 @@ void f2(const struct tm *Time) { // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'asctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + + struct tm *(*F1)(const struct tm *) = ctime; + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + + struct tm *(*F2)(const struct tm *) = &ctime; + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + + struct tm *(*F1)(const struct tm *) = localtime; + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + + struct tm *(*F2)(const struct tm *) = &localtime; + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead } typedef void *FILE; From bf8dd0c94ffb5df8b0c63b9c4b6be65f91c0c961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 19:30:34 +0200 Subject: [PATCH 20/60] fix: tests for `ctime` --- .../test/clang-tidy/checkers/bugprone/unsafe-functions.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 635bf62667717..6bd70a672b7ea 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -79,12 +79,12 @@ void f2(const struct tm *Time) { // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'asctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - struct tm *(*F1)(const struct tm *) = ctime; + char *(*F1)(const struct tm *) = ctime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - struct tm *(*F2)(const struct tm *) = &ctime; + char *(*F2)(const struct tm *) = &ctime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead From dfad2fc5d3200aeebb22e38135466f01ead53abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 19:32:16 +0200 Subject: [PATCH 21/60] fix: message for localtime --- .../test/clang-tidy/checkers/bugprone/unsafe-functions.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 6bd70a672b7ea..8e3478d365a35 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -90,9 +90,9 @@ void f2(const struct tm *Time) { // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead struct tm *(*F1)(const struct tm *) = localtime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead struct tm *(*F2)(const struct tm *) = &localtime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead From c6b30f1431baaea7aa34eb64b182639b0a036c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 20:06:42 +0200 Subject: [PATCH 22/60] fix: tests for ctime and localtime --- .../checkers/bugprone/unsafe-functions.c | 42 ++++--------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 8e3478d365a35..9d61836468b24 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -50,8 +50,6 @@ void f1w(wchar_t *S) { struct tm; char *asctime(const struct tm *TimePtr); -char *ctime(const struct tm *TimePtr); -char *localtime(const struct tm *tm); void f2(const struct tm *Time) { asctime(Time); @@ -59,16 +57,6 @@ void f2(const struct tm *Time) { // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'asctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - ctime(Time); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - localtime(Time); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - char *(*F1)(const struct tm *) = asctime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead @@ -78,26 +66,6 @@ void f2(const struct tm *Time) { // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'asctime' is not bounds-checking and non-reentrant; 'asctime_s' should be used instead // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'asctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - char *(*F1)(const struct tm *) = ctime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - char *(*F2)(const struct tm *) = &ctime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - struct tm *(*F1)(const struct tm *) = localtime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - struct tm *(*F2)(const struct tm *) = &localtime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead } typedef void *FILE; @@ -137,11 +105,17 @@ void f3(char *S, FILE *F) { typedef int time_t; char *ctime(const time_t *Timer); +struct tm *localtime(const struct tm *tm); void f4(const time_t *Timer) { ctime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // no-warning WITHOUT-ANNEX-K + + localtime(Time); + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead // no-warning WITHOUT-ANNEX-K } From 638e6ef6b2d4512af130a23e17a2fee0f0ffc756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 20:08:57 +0200 Subject: [PATCH 23/60] add ctime and localtime safe functions --- .../clang-tidy/checkers/bugprone/unsafe-functions.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 9d61836468b24..040d1b9aeef3c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -161,6 +161,8 @@ void fOptional() { typedef int errno_t; typedef size_t rsize_t; errno_t asctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); +errno_t ctime_r(char *S, rsize_t Maxsize, const struct tm *TimePtr); +errno_t localtime_r(char *S, rsize_t Maxsize, const struct tm *tm); errno_t strcat_s(char *S1, rsize_t S1Max, const char *S2); void fUsingSafeFunctions(const struct tm *Time, FILE *F) { @@ -170,6 +172,14 @@ void fUsingSafeFunctions(const struct tm *Time, FILE *F) { if (asctime_s(Buf, BUFSIZ, Time) != 0) return; + // no-warning, safe function from annex K is used + if (ctime_s(Buf, BUFSIZ, Time) != 0) + return; + + // no-warning, safe function from annex K is used + if (localtime_s(Buf, BUFSIZ, Time) != 0) + return; + // no-warning, safe function from annex K is used if (strcat_s(Buf, BUFSIZ, "something") != 0) return; From 7999af61518157cb188c2563e030e810f7026d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 20:10:19 +0200 Subject: [PATCH 24/60] fix: localtime_r argument --- .../test/clang-tidy/checkers/bugprone/unsafe-functions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 040d1b9aeef3c..3e77af2a8a514 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -162,7 +162,7 @@ typedef int errno_t; typedef size_t rsize_t; errno_t asctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); errno_t ctime_r(char *S, rsize_t Maxsize, const struct tm *TimePtr); -errno_t localtime_r(char *S, rsize_t Maxsize, const struct tm *tm); +errno_t localtime_r(char *S, rsize_t Maxsize, const struct tm *TimePtr); errno_t strcat_s(char *S1, rsize_t S1Max, const char *S2); void fUsingSafeFunctions(const struct tm *Time, FILE *F) { From d389e436a0a07f4fb47b44c2bf2f25e740e02fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 20:11:14 +0200 Subject: [PATCH 25/60] use `_r` functions, since `_s` functions are not implemented --- .../test/clang-tidy/checkers/bugprone/unsafe-functions.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 3e77af2a8a514..86b00b504513b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -173,11 +173,11 @@ void fUsingSafeFunctions(const struct tm *Time, FILE *F) { return; // no-warning, safe function from annex K is used - if (ctime_s(Buf, BUFSIZ, Time) != 0) + if (ctime_r(Buf, BUFSIZ, Time) != 0) return; // no-warning, safe function from annex K is used - if (localtime_s(Buf, BUFSIZ, Time) != 0) + if (localtime_r(Buf, BUFSIZ, Time) != 0) return; // no-warning, safe function from annex K is used From 8bbe8965bf8eb32102ff49498ab88822a3730915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 20:19:40 +0200 Subject: [PATCH 26/60] safe functions should be used --- .../checkers/bugprone/unsafe-functions.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 86b00b504513b..d5a14c3b16630 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -109,13 +109,13 @@ struct tm *localtime(const struct tm *tm); void f4(const time_t *Timer) { ctime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead // no-warning WITHOUT-ANNEX-K localtime(Time); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead // no-warning WITHOUT-ANNEX-K } @@ -161,8 +161,8 @@ void fOptional() { typedef int errno_t; typedef size_t rsize_t; errno_t asctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); -errno_t ctime_r(char *S, rsize_t Maxsize, const struct tm *TimePtr); -errno_t localtime_r(char *S, rsize_t Maxsize, const struct tm *TimePtr); +errno_t ctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); +errno_t localtime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); errno_t strcat_s(char *S1, rsize_t S1Max, const char *S2); void fUsingSafeFunctions(const struct tm *Time, FILE *F) { @@ -173,11 +173,11 @@ void fUsingSafeFunctions(const struct tm *Time, FILE *F) { return; // no-warning, safe function from annex K is used - if (ctime_r(Buf, BUFSIZ, Time) != 0) + if (ctime_s(Buf, BUFSIZ, Time) != 0) return; // no-warning, safe function from annex K is used - if (localtime_r(Buf, BUFSIZ, Time) != 0) + if (localtime_s(Buf, BUFSIZ, Time) != 0) return; // no-warning, safe function from annex K is used From 1e7e3bab49bc5161bbdb823332d40367e31b6fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 20:41:18 +0200 Subject: [PATCH 27/60] use safe functions --- .../clang-tidy/bugprone/UnsafeFunctionsCheck.cpp | 8 ++++---- .../test/clang-tidy/checkers/bugprone/unsafe-functions.c | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp index d74719e0ef1e0..8f97fbf5b3178 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp @@ -50,8 +50,8 @@ static StringRef getReplacementFor(StringRef FunctionName, StringRef AnnexKReplacementFunction = StringSwitch(FunctionName) .Cases("asctime", "asctime_r", "asctime_s") - .Case("ctime", "ctime_r") - .Case("localtime", "localtime_r") + .Cases("ctime", "ctime_r", "ctime_s") + .Cases("localtime", "localtime_r", "localtime_s") .Case("gets", "gets_s") .Default({}); if (!AnnexKReplacementFunction.empty()) @@ -62,8 +62,8 @@ static StringRef getReplacementFor(StringRef FunctionName, // should be matched and suggested. return StringSwitch(FunctionName) .Cases("asctime", "asctime_r", "strftime") - .Case("ctime", "ctime_r") - .Case("localtime", "localtime_r") + .Cases("ctime", "ctime_r", "ctime_s") + .Cases("localtime", "localtime_r", "localtime_s") .Case("gets", "fgets") .Case("rewind", "fseek") .Case("setbuf", "setvbuf"); diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index d5a14c3b16630..95eb361325845 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -162,10 +162,10 @@ typedef int errno_t; typedef size_t rsize_t; errno_t asctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); errno_t ctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); -errno_t localtime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); +errno_t localtime_s(struct tm *TimePtr, time_t *Timep); errno_t strcat_s(char *S1, rsize_t S1Max, const char *S2); -void fUsingSafeFunctions(const struct tm *Time, FILE *F) { +void fUsingSafeFunctions(const struct tm *Time, FILE *F, time_t *Timep) { char Buf[BUFSIZ] = {0}; // no-warning, safe function from annex K is used @@ -177,7 +177,7 @@ void fUsingSafeFunctions(const struct tm *Time, FILE *F) { return; // no-warning, safe function from annex K is used - if (localtime_s(Buf, BUFSIZ, Time) != 0) + if (localtime_s(Time, Timep) != 0) return; // no-warning, safe function from annex K is used From 244879c21ea41229a773e404f2f40fd03b671003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 21:24:19 +0200 Subject: [PATCH 28/60] fix: localtime argument --- .../test/clang-tidy/checkers/bugprone/unsafe-functions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 95eb361325845..0a321f3981be5 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -113,7 +113,7 @@ void f4(const time_t *Timer) { // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead // no-warning WITHOUT-ANNEX-K - localtime(Time); + localtime(Timer); // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead // no-warning WITHOUT-ANNEX-K From 815a4729851b3b34c8ae69dd77c9fcd73feb9f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Mon, 30 Sep 2024 22:14:51 +0200 Subject: [PATCH 29/60] temporarily use `_r` functions --- .../test/clang-tidy/checkers/bugprone/unsafe-functions.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 0a321f3981be5..56f31232c37e3 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -109,13 +109,13 @@ struct tm *localtime(const struct tm *tm); void f4(const time_t *Timer) { ctime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead // no-warning WITHOUT-ANNEX-K localtime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead // no-warning WITHOUT-ANNEX-K } From 1d72f2bf61b7b2cf5472dec83ead26c4190897d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Tue, 1 Oct 2024 18:41:56 +0200 Subject: [PATCH 30/60] use `_s` functions and add functions for `ctime` and `localtime` --- .../checkers/bugprone/unsafe-functions.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 56f31232c37e3..12e042347b9eb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -109,13 +109,23 @@ struct tm *localtime(const struct tm *tm); void f4(const time_t *Timer) { ctime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead // no-warning WITHOUT-ANNEX-K localtime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_r' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // no-warning WITHOUT-ANNEX-K + + char *(*F4)(const struct tm *) = &ctime; + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // no-warning WITHOUT-ANNEX-K + + struct tm *(*F4)(const struct tm *) = &localtime; + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead // no-warning WITHOUT-ANNEX-K } From 0284ec7d4f834e168fde971844ec1c4fb13f3537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Tue, 1 Oct 2024 19:33:20 +0200 Subject: [PATCH 31/60] fix: function names --- .../test/clang-tidy/checkers/bugprone/unsafe-functions.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index 12e042347b9eb..aeab8ad69d4e6 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -118,12 +118,12 @@ void f4(const time_t *Timer) { // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead // no-warning WITHOUT-ANNEX-K - char *(*F4)(const struct tm *) = &ctime; + char *(*F1)(const struct tm *) = &ctime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead // no-warning WITHOUT-ANNEX-K - struct tm *(*F4)(const struct tm *) = &localtime; + struct tm *(*F2)(const struct tm *) = &localtime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead // no-warning WITHOUT-ANNEX-K From 59974dba0adc87e657ba3e44a0c1085432de067f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Tue, 1 Oct 2024 20:10:25 +0200 Subject: [PATCH 32/60] fix: tests for clang-tidy --- .../checkers/bugprone/unsafe-functions.c | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index aeab8ad69d4e6..cd607f783259d 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -105,28 +105,38 @@ void f3(char *S, FILE *F) { typedef int time_t; char *ctime(const time_t *Timer); -struct tm *localtime(const struct tm *tm); +struct tm *localtime(const time_t *Timer); void f4(const time_t *Timer) { ctime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // no-warning WITHOUT-ANNEX-K + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:116: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:116: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:116: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead localtime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // no-warning WITHOUT-ANNEX-K + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:116: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:116: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:116: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + + char *(*F1)(const time_t *) = ctime; + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - char *(*F1)(const struct tm *) = &ctime; + char *(*F2)(const time_t *) = &ctime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // no-warning WITHOUT-ANNEX-K + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + + struct tm *(*F4)(const time_t *) = localtime; + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - struct tm *(*F2)(const struct tm *) = &localtime; + struct tm *(*F5)(const time_t *) = &localtime; // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // no-warning WITHOUT-ANNEX-K + // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead } #define BUFSIZ 128 @@ -171,8 +181,8 @@ void fOptional() { typedef int errno_t; typedef size_t rsize_t; errno_t asctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); -errno_t ctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); -errno_t localtime_s(struct tm *TimePtr, time_t *Timep); +errno_t ctime_s(char *S, rsize_t Maxsize, const time_t *Timep); +errno_t localtime_s(const time_t *Timep, rsize_t Maxsize, const struct tm *TimePtr); errno_t strcat_s(char *S1, rsize_t S1Max, const char *S2); void fUsingSafeFunctions(const struct tm *Time, FILE *F, time_t *Timep) { @@ -187,7 +197,7 @@ void fUsingSafeFunctions(const struct tm *Time, FILE *F, time_t *Timep) { return; // no-warning, safe function from annex K is used - if (localtime_s(Time, Timep) != 0) + if (localtime_s(Timep, BUFSIZ, Time) != 0) return; // no-warning, safe function from annex K is used From 780fae6f7167a75596b7af3e23fa676e463a7ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Tue, 1 Oct 2024 21:03:38 +0200 Subject: [PATCH 33/60] temporarily remove localtime --- .../checkers/bugprone/unsafe-functions.c | 31 ++----------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index cd607f783259d..bb44297842097 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -109,34 +109,9 @@ struct tm *localtime(const time_t *Timer); void f4(const time_t *Timer) { ctime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:116: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:116: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:116: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - localtime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:116: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:116: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:116: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - char *(*F1)(const time_t *) = ctime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - char *(*F2)(const time_t *) = &ctime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'ctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - struct tm *(*F4)(const time_t *) = localtime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:36: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead - - struct tm *(*F5)(const time_t *) = &localtime; - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'localtime_s' should be used instead - // CHECK-MESSAGES-WITHOUT-ANNEX-K: :[[@LINE-3]]:37: warning: function 'localtime' is not bounds-checking and non-reentrant; 'strftime' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead + // no-warning WITHOUT-ANNEX-K } #define BUFSIZ 128 From 412654e8f9e84754b6ec0d26f47376c6aae8e8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Tue, 1 Oct 2024 21:04:05 +0200 Subject: [PATCH 34/60] add tests for ctime and localtime --- .../Inputs/std-c-library-functions-POSIX.h | 4 ++- clang/test/Analysis/cert/env34-c.c | 32 +++++++++++++++++-- .../Analysis/std-c-library-functions-POSIX.c | 4 ++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h b/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h index b146068eedb08..16941434ab78e 100644 --- a/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h +++ b/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h @@ -174,9 +174,11 @@ int utimensat(int dirfd, const char *pathname, const struct timespec times[2], i int utimes(const char *filename, const struct timeval times[2]); int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); struct tm *localtime(const time_t *tp); -struct tm *localtime_r(const time_t *restrict timer, struct tm *restrict result); +struct tm *localtime_r(const time_t *timer, struct tm *result); +struct tm *localtime_s(const time_t *restrict timer, struct tm *restrict result); char *asctime_r(const struct tm *restrict tm, char *restrict buf); char *ctime_r(const time_t *timep, char *buf); +char *ctime_s(char *buf, rsize_t buf_size, const time_t *timep); struct tm *gmtime_r(const time_t *restrict timer, struct tm *restrict result); struct tm *gmtime(const time_t *tp); int clock_gettime(clockid_t clock_id, struct timespec *tp); diff --git a/clang/test/Analysis/cert/env34-c.c b/clang/test/Analysis/cert/env34-c.c index ae344a815679e..0f1dd8707f98f 100644 --- a/clang/test/Analysis/cert/env34-c.c +++ b/clang/test/Analysis/cert/env34-c.c @@ -16,8 +16,8 @@ lconv *localeconv(void); typedef struct { } tm; char *asctime(const tm *timeptr); -char *ctime(const tm *timeptr); -struct tm *localtime(struct tm *tm); +char *ctime(const time_t *time); +struct tm *localtime(const time_t *time); int strcmp(const char*, const char*); extern void foo(char *e); @@ -315,6 +315,34 @@ void asctime_test(void) { // expected-note@-2{{dereferencing an invalid pointer}} } +void ctime_test(void) { + const time_t *t; + const time_t *tt; + + char* p = ctime(t); + // expected-note@-1{{previous function call was here}} + char* pp = ctime(tt); + // expected-note@-1{{'ctime' call may invalidate the result of the previous 'ctime'}} + + *p; + // expected-warning@-1{{dereferencing an invalid pointer}} + // expected-note@-2{{dereferencing an invalid pointer}} +} + +void time_test(void) { + const time_t *t; + const time_t *tt; + + struct tm* p = localtime(t); + // expected-note@-1{{previous function call was here}} + struct tm* pp = localtime(tt); + // expected-note@-1{{'localtime' call may invalidate the result of the previous 'localtime'}} + + *p; + // expected-warning@-1{{dereferencing an invalid pointer}} + // expected-note@-2{{dereferencing an invalid pointer}} +} + void localeconv_test1(void) { lconv *lc1 = localeconv(); // expected-note@-1{{previous function call was here}} diff --git a/clang/test/Analysis/std-c-library-functions-POSIX.c b/clang/test/Analysis/std-c-library-functions-POSIX.c index f6d88e6c1502d..ed85392e9391f 100644 --- a/clang/test/Analysis/std-c-library-functions-POSIX.c +++ b/clang/test/Analysis/std-c-library-functions-POSIX.c @@ -129,9 +129,11 @@ // CHECK: Loaded summary for: int utimes(const char *filename, const struct timeval times[2]) // CHECK: Loaded summary for: int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) // CHECK: Loaded summary for: struct tm *localtime(const time_t *tp) -// CHECK: Loaded summary for: struct tm *localtime_r(const time_t *restrict timer, struct tm *restrict result) +// CHECK: Loaded summary for: struct tm *localtime_r(const time_t *timer, struct tm *result) +// CHECK: Loaded summary for: struct tm *localtime_s(const time_t *restrict timer, struct tm *restrict result) // CHECK: Loaded summary for: char *asctime_r(const struct tm *restrict tm, char *restrict buf) // CHECK: Loaded summary for: char *ctime_r(const time_t *timep, char *buf) +// CHECK: Loaded summary for: char *ctime_s(char *buf, rsize_t buf_size, const time_t *timep) // CHECK: Loaded summary for: struct tm *gmtime_r(const time_t *restrict timer, struct tm *restrict result) // CHECK: Loaded summary for: struct tm *gmtime(const time_t *tp) // CHECK: Loaded summary for: int clock_gettime(clockid_t clock_id, struct timespec *tp) From 1475515effdf1c70b2cb253c0381bb36d4326b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Fri, 4 Oct 2024 20:18:55 +0200 Subject: [PATCH 35/60] update tests for ctime and localtime --- .../checks/bugprone/unsafe-functions.rst | 2 ++ .../checkers/bugprone/unsafe-functions.c | 1 - .../Checkers/StdLibraryFunctionsChecker.cpp | 25 +++++++++++++++++-- .../Inclusions/Stdlib/StdSymbolMap.inc | 4 +-- clang/test/Analysis/cert/env34-c.c | 2 +- .../lib/dfsan/libc_ubuntu1404_abilist.txt | 2 ++ .../sanitizer_common_interceptors.inc | 23 +++++++++++++++++ 7 files changed, 53 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst index 17c5f9e66c74a..6abf4f8a7032c 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst @@ -41,6 +41,8 @@ If *Annex K.* is not available, replacements are suggested only for the following functions from the previous list: - ``asctime``, ``asctime_r``, suggested replacement: ``strftime`` + - ``ctime``, ``ctime_r``, suggested replacement: ``ctime_s`` + - ``localtime``, ``localtime_r``, suggested replacement: ``localtime_s`` - ``gets``, suggested replacement: ``fgets`` The following functions are always checked, regardless of *Annex K* availability: diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index bb44297842097..bc1e6007201bb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -105,7 +105,6 @@ void f3(char *S, FILE *F) { typedef int time_t; char *ctime(const time_t *Timer); -struct tm *localtime(const time_t *Timer); void f4(const time_t *Timer) { ctime(Timer); diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index 17227a23571ab..c3e9b93c55367 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3533,10 +3533,20 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( Signature(ArgTypes{ConstTime_tPtrTy}, RetType{StructTmPtrTy}), Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0)))); - // struct tm *localtime_r(const time_t *restrict timer, - // struct tm *restrict result); + // struct tm *localtime_r(const time_t *timer, + // struct tm *result); addToFunctionSummaryMap( "localtime_r", + Signature(ArgTypes{ConstTime_tPtrTy, StructTmPtrTy}, + RetType{StructTmPtrTy}), + Summary(NoEvalCall) + .ArgConstraint(NotNull(ArgNo(0))) + .ArgConstraint(NotNull(ArgNo(1)))); + + // struct tm *localtime_s(const time_t *restrict timer, + // struct tm *restrict result); + addToFunctionSummaryMap( + "localtime_s", Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy}, RetType{StructTmPtrTy}), Summary(NoEvalCall) @@ -3565,6 +3575,17 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( /*Buffer=*/ArgNo(1), /*MinBufSize=*/BVF.getValue(26, IntTy)))); + // char *ctime_r(char *buf, rsize_t buf_size, const time_t *timep); + addToFunctionSummaryMap( + "ctime_s", + Signature(ArgTypes{CharPtrTy, BufferSize(ArgNo(1), BVF.getValue(26, IntTy)), ConstTime_tPtrTy}, RetType{CharPtrTy}), + Summary(NoEvalCall) + .ArgConstraint(NotNull(ArgNo(0))) + .ArgConstraint(NotNull(ArgNo(1))) + .ArgConstraint(BufferSize( + /*Buffer=*/ArgNo(1), + /*MinBufSize=*/BVF.getValue(26, IntTy)))); + // struct tm *gmtime_r(const time_t *restrict timer, // struct tm *restrict result); addToFunctionSummaryMap( diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc index c1927180d3397..65e3573282712 100644 --- a/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc +++ b/clang/lib/Tooling/Inclusions/Stdlib/StdSymbolMap.inc @@ -2032,8 +2032,8 @@ SYMBOL(locale, std::, ) SYMBOL(localeconv, std::, ) SYMBOL(localeconv, None, ) SYMBOL(localeconv, None, ) -SYMBOL(localtime, std::, ) -SYMBOL(localtime, None, ) +SYMBOL(localtime, std::, ) +SYMBOL(localtime, None, ) SYMBOL(localtime, None, ) SYMBOL(lock, std::, ) SYMBOL(lock_guard, std::, ) diff --git a/clang/test/Analysis/cert/env34-c.c b/clang/test/Analysis/cert/env34-c.c index 0f1dd8707f98f..3e764543b7ed0 100644 --- a/clang/test/Analysis/cert/env34-c.c +++ b/clang/test/Analysis/cert/env34-c.c @@ -329,7 +329,7 @@ void ctime_test(void) { // expected-note@-2{{dereferencing an invalid pointer}} } -void time_test(void) { +void localtime_test(void) { const time_t *t; const time_t *tt; diff --git a/compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt b/compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt index 9ffa56a238185..d0d6e74743d32 100644 --- a/compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt +++ b/compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt @@ -1547,6 +1547,7 @@ fun:ctanl=uninstrumented fun:ctermid=uninstrumented fun:ctime=uninstrumented fun:ctime_r=uninstrumented +fun:ctime_s=uninstrumented fun:cuserid=uninstrumented fun:daemon=uninstrumented fun:dcgettext=uninstrumented @@ -2205,6 +2206,7 @@ fun:llseek=uninstrumented fun:localeconv=uninstrumented fun:localtime=uninstrumented fun:localtime_r=uninstrumented +fun:localtime_s=uninstrumented fun:lockf=uninstrumented fun:lockf64=uninstrumented fun:log=uninstrumented diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 5a15d75f0c86a..f0ef5e9c5c229 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -1375,6 +1375,16 @@ INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) { } return res; } +INTERCEPTOR(__sanitizer_tm *, localtime_s, unsigned long *timep, void *result) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result); + __sanitizer_tm *res = REAL(localtime_r)(timep, result); + if (res) { + COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); + unpoison_tm(ctx, res); + } + return res; +} INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep); @@ -1421,6 +1431,19 @@ INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) { } return res; } +INTERCEPTOR(char *, ctime_s, char* result, size_t result_size, unsigned long *timep) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, ctime_s, result, result_size, timep); + // FIXME: under ASan the call below may write to freed memory and corrupt + // its metadata. See + // https://github.com/google/sanitizers/issues/321. + char *res = REAL(ctime_r)(result, result_size, timep); + if (res) { + COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1); + } + return res; +} INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm); From fdf42457aa25695b156c73d3a2ac8a9eb0d5fd52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 08:27:03 +0500 Subject: [PATCH 36/60] format code with clang-format --- .../Checkers/StdLibraryFunctionsChecker.cpp | 18 ++++++++++-------- .../sanitizer_common_interceptors.inc | 3 ++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index c3e9b93c55367..640dae3b9405b 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3535,13 +3535,12 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( // struct tm *localtime_r(const time_t *timer, // struct tm *result); - addToFunctionSummaryMap( - "localtime_r", - Signature(ArgTypes{ConstTime_tPtrTy, StructTmPtrTy}, - RetType{StructTmPtrTy}), - Summary(NoEvalCall) - .ArgConstraint(NotNull(ArgNo(0))) - .ArgConstraint(NotNull(ArgNo(1)))); + addToFunctionSummaryMap("localtime_r", + Signature(ArgTypes{ConstTime_tPtrTy, StructTmPtrTy}, + RetType{StructTmPtrTy}), + Summary(NoEvalCall) + .ArgConstraint(NotNull(ArgNo(0))) + .ArgConstraint(NotNull(ArgNo(1)))); // struct tm *localtime_s(const time_t *restrict timer, // struct tm *restrict result); @@ -3578,7 +3577,10 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( // char *ctime_r(char *buf, rsize_t buf_size, const time_t *timep); addToFunctionSummaryMap( "ctime_s", - Signature(ArgTypes{CharPtrTy, BufferSize(ArgNo(1), BVF.getValue(26, IntTy)), ConstTime_tPtrTy}, RetType{CharPtrTy}), + Signature(ArgTypes{CharPtrTy, + BufferSize(ArgNo(1), BVF.getValue(26, IntTy)), + ConstTime_tPtrTy}, + RetType{CharPtrTy}), Summary(NoEvalCall) .ArgConstraint(NotNull(ArgNo(0))) .ArgConstraint(NotNull(ArgNo(1))) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index f0ef5e9c5c229..35216c3bccf8e 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -1431,7 +1431,8 @@ INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) { } return res; } -INTERCEPTOR(char *, ctime_s, char* result, size_t result_size, unsigned long *timep) { +INTERCEPTOR(char *, ctime_s, char *result, size_t result_size, + unsigned long *timep) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, ctime_s, result, result_size, timep); // FIXME: under ASan the call below may write to freed memory and corrupt From 45074caf871a248346a3ed61fea5fd98ce8f6a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 09:24:55 +0500 Subject: [PATCH 37/60] fix: document name --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 660c96e638992..73a880f76efcf 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -171,7 +171,7 @@ Changes in existing checks - Added `ctime` and `localtime` to clang-tidy. -- New unsafe functions checks :doc:`bugprone-unsafe-functions-check` +- New unsafe functions checks :doc:`bugprone-unsafe-functions` were added to clang-tidy. - Improved :doc:`bugprone-unsafe-functions From 95342b90b9e774e82ff1ef564f32ff530180f4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 09:38:43 +0500 Subject: [PATCH 38/60] fix: path for unsafe-functions --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 73a880f76efcf..29c3f6877fe98 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -175,7 +175,7 @@ Changes in existing checks were added to clang-tidy. - Improved :doc:`bugprone-unsafe-functions - `, added `ctime` and `localtime` + `, added `ctime` and `localtime` to unsafe functions check in clang-tidy. - Improved :doc:`bugprone-casting-through-void From 92cda2bbebbebe444c39332c743d5c29f7aa385a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 09:43:34 +0500 Subject: [PATCH 39/60] fix path --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 29c3f6877fe98..73a880f76efcf 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -175,7 +175,7 @@ Changes in existing checks were added to clang-tidy. - Improved :doc:`bugprone-unsafe-functions - `, added `ctime` and `localtime` + `, added `ctime` and `localtime` to unsafe functions check in clang-tidy. - Improved :doc:`bugprone-casting-through-void From 460ccf93514b7a2b3b009fee8ea89e6c006c9181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 09:49:43 +0500 Subject: [PATCH 40/60] fix: release notes for clang-tidy --- clang-tools-extra/docs/ReleaseNotes.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 73a880f76efcf..5a1626a8efbbb 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -169,10 +169,8 @@ Changes in existing checks ` check to improve detection of branch clones by now detecting duplicate inner and outer if statements. -- Added `ctime` and `localtime` to clang-tidy. - -- New unsafe functions checks :doc:`bugprone-unsafe-functions` - were added to clang-tidy. +- New unsafe functions checks :doc:`bugprone-unsafe-functions + ` were added to clang-tidy. - Improved :doc:`bugprone-unsafe-functions `, added `ctime` and `localtime` From b70a529d2e4997a40eda5a0f549d89eacacdcabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 09:56:44 +0500 Subject: [PATCH 41/60] fix: path for unsafe functions in release notes --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 5a1626a8efbbb..051e811ea53be 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -170,7 +170,7 @@ Changes in existing checks branch clones by now detecting duplicate inner and outer if statements. - New unsafe functions checks :doc:`bugprone-unsafe-functions - ` were added to clang-tidy. + ` were added to clang-tidy. - Improved :doc:`bugprone-unsafe-functions `, added `ctime` and `localtime` From b4d5208fe133c85184c483952eaeb4aa7f46fede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 10:02:35 +0500 Subject: [PATCH 42/60] fix path for unsafe functions in release notes --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 051e811ea53be..e924933aecd32 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -170,7 +170,7 @@ Changes in existing checks branch clones by now detecting duplicate inner and outer if statements. - New unsafe functions checks :doc:`bugprone-unsafe-functions - ` were added to clang-tidy. + ` were added to clang-tidy. - Improved :doc:`bugprone-unsafe-functions `, added `ctime` and `localtime` From 18e882e6b0ab28d496ed422d06d763a1e5c01f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 10:27:33 +0500 Subject: [PATCH 43/60] fix: static analyzer for ctime_s --- .../StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index 640dae3b9405b..f857f09005e17 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3574,7 +3574,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( /*Buffer=*/ArgNo(1), /*MinBufSize=*/BVF.getValue(26, IntTy)))); - // char *ctime_r(char *buf, rsize_t buf_size, const time_t *timep); + // char *ctime_s(char *buf, rsize_t buf_size, const time_t *timep); addToFunctionSummaryMap( "ctime_s", Signature(ArgTypes{CharPtrTy, @@ -3583,10 +3583,10 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( RetType{CharPtrTy}), Summary(NoEvalCall) .ArgConstraint(NotNull(ArgNo(0))) - .ArgConstraint(NotNull(ArgNo(1))) .ArgConstraint(BufferSize( /*Buffer=*/ArgNo(1), /*MinBufSize=*/BVF.getValue(26, IntTy)))); + .ArgConstraint(NotNull(ArgNo(1))) // struct tm *gmtime_r(const time_t *restrict timer, // struct tm *restrict result); From f1540c50ad8d68a0abe4171b0aa997be5b812a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 10:32:36 +0500 Subject: [PATCH 44/60] fix argument number --- .../lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index f857f09005e17..6237644e5a30c 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3586,7 +3586,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .ArgConstraint(BufferSize( /*Buffer=*/ArgNo(1), /*MinBufSize=*/BVF.getValue(26, IntTy)))); - .ArgConstraint(NotNull(ArgNo(1))) + .ArgConstraint(NotNull(ArgNo(2))) // struct tm *gmtime_r(const time_t *restrict timer, // struct tm *restrict result); From f28b90af745b3eac96d8666689844e0203ea4cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 10:32:58 +0500 Subject: [PATCH 45/60] format code with clang-format --- .../Checkers/StdLibraryFunctionsChecker.cpp | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index 6237644e5a30c..acd64b4592f7b 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3586,17 +3586,17 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .ArgConstraint(BufferSize( /*Buffer=*/ArgNo(1), /*MinBufSize=*/BVF.getValue(26, IntTy)))); - .ArgConstraint(NotNull(ArgNo(2))) - - // struct tm *gmtime_r(const time_t *restrict timer, - // struct tm *restrict result); - addToFunctionSummaryMap( - "gmtime_r", - Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy}, - RetType{StructTmPtrTy}), - Summary(NoEvalCall) - .ArgConstraint(NotNull(ArgNo(0))) - .ArgConstraint(NotNull(ArgNo(1)))); + .ArgConstraint(NotNull(ArgNo(2))) + + // struct tm *gmtime_r(const time_t *restrict timer, + // struct tm *restrict result); + addToFunctionSummaryMap( + "gmtime_r", + Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy}, + RetType{StructTmPtrTy}), + Summary(NoEvalCall) + .ArgConstraint(NotNull(ArgNo(0))) + .ArgConstraint(NotNull(ArgNo(1)))); // struct tm * gmtime(const time_t *tp); addToFunctionSummaryMap( From 9d5fa7374ca56a58d769515a09644de18c28c633 Mon Sep 17 00:00:00 2001 From: Zishan Mirza Date: Sat, 4 Jan 2025 16:11:54 +0500 Subject: [PATCH 46/60] include header file for size_t --- .../lib/sanitizer_common/sanitizer_common_interceptors.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 35216c3bccf8e..0d31c5b08ca97 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -34,6 +34,7 @@ //===----------------------------------------------------------------------===// #include +#include #include "interception/interception.h" #include "sanitizer_addrhashmap.h" From 2513ed9beac4d58ac5747ecd512a82ef08e97435 Mon Sep 17 00:00:00 2001 From: Zishan Mirza Date: Sat, 4 Jan 2025 16:27:37 +0500 Subject: [PATCH 47/60] fix clang static analyzer and functions checker --- .../StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp | 4 ++-- .../lib/sanitizer_common/sanitizer_common_interceptors.inc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index acd64b4592f7b..b2f2cb8b4541c 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3578,13 +3578,13 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( addToFunctionSummaryMap( "ctime_s", Signature(ArgTypes{CharPtrTy, - BufferSize(ArgNo(1), BVF.getValue(26, IntTy)), + BufferSize(ArgNo(0), BVF.getValue(26, IntTy)), ConstTime_tPtrTy}, RetType{CharPtrTy}), Summary(NoEvalCall) .ArgConstraint(NotNull(ArgNo(0))) .ArgConstraint(BufferSize( - /*Buffer=*/ArgNo(1), + /*Buffer=*/ArgNo(0), /*MinBufSize=*/BVF.getValue(26, IntTy)))); .ArgConstraint(NotNull(ArgNo(2))) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 0d31c5b08ca97..565870769e1ed 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -1439,7 +1439,7 @@ INTERCEPTOR(char *, ctime_s, char *result, size_t result_size, // FIXME: under ASan the call below may write to freed memory and corrupt // its metadata. See // https://github.com/google/sanitizers/issues/321. - char *res = REAL(ctime_r)(result, result_size, timep); + char *res = REAL(ctime_s)(result, result_size, timep); if (res) { COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1); From 36f296887db4c3f3b2b5ecc1df5c76459a442d0c Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 16:34:32 +0200 Subject: [PATCH 48/60] combine `bugprone-unsafe-functions` release notes --- clang-tools-extra/docs/ReleaseNotes.rst | 32 +++---------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index e924933aecd32..77c39d1cfe403 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -134,8 +134,6 @@ New check aliases Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ -<<<<<<< HEAD -||||||| parent of c62bbb1d2bf2 (moved release notes for `ctime` and `localtime`) - Improved :doc:`altera-id-dependent-backward-branch ` check by fixing crashes from invalid code. @@ -144,33 +142,10 @@ Changes in existing checks ` check to improve detection of branch clones by now detecting duplicate inner and outer if statements. -- Improved :doc:`bugprone-casting-through-void - ` check to suggest replacing - the offending code with ``reinterpret_cast``, to more clearly express intent. - -- Improved :doc:`bugprone-dangling-handle - ` check to treat `std::span` as a - handle class. - -- Improved :doc:`bugprone-exception-escape - ` by fixing false positives - when a consteval function with throw statements. - -- Improved :doc:`bugprone-forwarding-reference-overload - ` check by fixing - a crash when determining if an ``enable_if[_t]`` was found. - -======= -- Improved :doc:`altera-id-dependent-backward-branch - ` check by fixing - crashes from invalid code. - -- Improved :doc:`bugprone-branch-clone - ` check to improve detection of - branch clones by now detecting duplicate inner and outer if statements. +- Added `ctime` and `localtime` to clang-tidy. -- New unsafe functions checks :doc:`bugprone-unsafe-functions - ` were added to clang-tidy. +- New unsafe functions checks :doc:`bugprone-unsafe-functions-check` + were added to clang-tidy. - Improved :doc:`bugprone-unsafe-functions `, added `ctime` and `localtime` @@ -192,7 +167,6 @@ Changes in existing checks ` check by fixing a crash when determining if an ``enable_if[_t]`` was found. ->>>>>>> c62bbb1d2bf2 (moved release notes for `ctime` and `localtime`) - Improved :doc:`bugprone-optional-value-conversion ` check to detect conversion in argument of ``std::make_optional``. From 7685ec0c13a23c206e47e10fa7feedbb58a21933 Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 16:48:44 +0200 Subject: [PATCH 49/60] fix: rebase from main branch --- clang-tools-extra/docs/ReleaseNotes.rst | 37 ++++--------------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 77c39d1cfe403..acb2e22cf9b71 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -91,15 +91,16 @@ Improvements to clang-query Improvements to clang-tidy -------------------------- +- Changed the :program:`check_clang_tidy.py` tool to use FileCheck's + ``--match-full-lines`` instead of ``strict-whitespace`` for ``CHECK-FIXES`` + clauses. Added a ``--match-partial-fixes`` option to keep previous behavior on + specific tests. This may break tests for users with custom out-of-tree checks + who use :program:`check_clang_tidy.py` as-is. + - Improved :program:`clang-tidy`'s `--verify-config` flag by adding support for the configuration options of the `Clang Static Analyzer Checks `_. -- Added `ctime` and `localtime` to clang-tidy. - -- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise - happening on certain platforms when interrupting the script. - - Fixed bug in :program:`clang-tidy` by which `HeaderFilterRegex` did not take effect when passed via the `.clang-tidy` file. @@ -134,16 +135,6 @@ New check aliases Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ -- Improved :doc:`altera-id-dependent-backward-branch - ` check by fixing - crashes from invalid code. - -- Improved :doc:`bugprone-branch-clone - ` check to improve detection of - branch clones by now detecting duplicate inner and outer if statements. - -- Added `ctime` and `localtime` to clang-tidy. - - New unsafe functions checks :doc:`bugprone-unsafe-functions-check` were added to clang-tidy. @@ -151,22 +142,6 @@ Changes in existing checks `, added `ctime` and `localtime` to unsafe functions check in clang-tidy. -- Improved :doc:`bugprone-casting-through-void - ` check to suggest replacing - the offending code with ``reinterpret_cast``, to more clearly express intent. - -- Improved :doc:`bugprone-dangling-handle - ` check to treat `std::span` as a - handle class. - -- Improved :doc:`bugprone-exception-escape - ` by fixing false positives - when a consteval function with throw statements. - -- Improved :doc:`bugprone-forwarding-reference-overload - ` check by fixing - a crash when determining if an ``enable_if[_t]`` was found. - - Improved :doc:`bugprone-optional-value-conversion ` check to detect conversion in argument of ``std::make_optional``. From 38a2135f1f2e1509b608036278e78d04de3ae670 Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 16:49:57 +0200 Subject: [PATCH 50/60] fix: rebase from main branch --- clang-tools-extra/docs/ReleaseNotes.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index acb2e22cf9b71..31dcce2ad5f9a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -97,9 +97,8 @@ Improvements to clang-tidy specific tests. This may break tests for users with custom out-of-tree checks who use :program:`check_clang_tidy.py` as-is. -- Improved :program:`clang-tidy`'s `--verify-config` flag by adding support for - the configuration options of the `Clang Static Analyzer Checks - `_. +- Improved :program:`clang-tidy-diff.py` script. Add the `-warnings-as-errors` + argument to treat warnings as errors. - Fixed bug in :program:`clang-tidy` by which `HeaderFilterRegex` did not take effect when passed via the `.clang-tidy` file. From 23d21cd524ce0688cfe66c8b1bc42b2515115c2e Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 16:51:21 +0200 Subject: [PATCH 51/60] fix: rebase from main branch --- clang-tools-extra/docs/ReleaseNotes.rst | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 31dcce2ad5f9a..c55d507166a6f 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,13 +157,10 @@ Changes in existing checks no longer be needed and will be removed. Also fixing false positive from const reference accessors to objects containing optional member. -- Improved :doc:`bugprone-unsafe-functions - ` by adding ``ctime`` and - ``localtime`` functions. - - Improved :doc:`bugprone-unsafe-functions ` check to allow specifying - additional C++ member functions to match. + additional C++ member functions to match and adding ``ctime`` and + ``localtime`` functions. - Improved :doc:`cert-err33-c ` check by fixing false positives when From 428c299fcdf9dae691bf7529cab9cb7eaf0bff6d Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 16:52:36 +0200 Subject: [PATCH 52/60] update release notes for clang-tidy --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index c55d507166a6f..08b9768715390 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -139,7 +139,7 @@ Changes in existing checks - Improved :doc:`bugprone-unsafe-functions `, added `ctime` and `localtime` - to unsafe functions check in clang-tidy. + to unsafe functions list. - Improved :doc:`bugprone-optional-value-conversion ` check to detect From 4e6605010f712f8d11c5b4767050c032d166c528 Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 16:54:56 +0200 Subject: [PATCH 53/60] update release notes for clang-tidy --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 08b9768715390..77e7aed9f2bc3 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -138,8 +138,8 @@ Changes in existing checks were added to clang-tidy. - Improved :doc:`bugprone-unsafe-functions - `, added `ctime` and `localtime` - to unsafe functions list. + `, by adding ``ctime`` and + ``localtime`` to unsafe functions list. - Improved :doc:`bugprone-optional-value-conversion ` check to detect From 3326ab2215eba23949926c9f47577dffeb04d297 Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 16:56:11 +0200 Subject: [PATCH 54/60] combine `bugprone-unsafe-functions` release notes --- clang-tools-extra/docs/ReleaseNotes.rst | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 77e7aed9f2bc3..051d3b416997a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -137,10 +137,6 @@ Changes in existing checks - New unsafe functions checks :doc:`bugprone-unsafe-functions-check` were added to clang-tidy. -- Improved :doc:`bugprone-unsafe-functions - `, by adding ``ctime`` and - ``localtime`` to unsafe functions list. - - Improved :doc:`bugprone-optional-value-conversion ` check to detect conversion in argument of ``std::make_optional``. @@ -159,8 +155,8 @@ Changes in existing checks - Improved :doc:`bugprone-unsafe-functions ` check to allow specifying - additional C++ member functions to match and adding ``ctime`` and - ``localtime`` functions. + additional C++ member functions to match and by adding ``ctime`` and + ``localtime`` to unsafe functions list. - Improved :doc:`cert-err33-c ` check by fixing false positives when From 2aabbd7820e411c9a258e8ef0192365e7d8cbcb6 Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 17:02:05 +0200 Subject: [PATCH 55/60] fix: release notes for unsafe functions --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 051d3b416997a..90b2380889ac5 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -134,7 +134,7 @@ New check aliases Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ -- New unsafe functions checks :doc:`bugprone-unsafe-functions-check` +- New unsafe functions checks :doc:`bugprone-unsafe-functions` were added to clang-tidy. - Improved :doc:`bugprone-optional-value-conversion From cf1d22d1435a4ad79686d89b2cc051ca9423aa5d Mon Sep 17 00:00:00 2001 From: zishan Date: Wed, 30 Apr 2025 17:08:48 +0200 Subject: [PATCH 56/60] revert: identifier for bugprone-unsafe-functions --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 90b2380889ac5..051d3b416997a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -134,7 +134,7 @@ New check aliases Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ -- New unsafe functions checks :doc:`bugprone-unsafe-functions` +- New unsafe functions checks :doc:`bugprone-unsafe-functions-check` were added to clang-tidy. - Improved :doc:`bugprone-optional-value-conversion From 8cd9bd7679175d9008226badd32dcf7e02888bd2 Mon Sep 17 00:00:00 2001 From: zishan Date: Fri, 2 May 2025 13:51:03 +0200 Subject: [PATCH 57/60] remove duplicate entry in release notes --- clang-tools-extra/docs/ReleaseNotes.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 051d3b416997a..34645ce7ee5d5 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -134,9 +134,6 @@ New check aliases Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ -- New unsafe functions checks :doc:`bugprone-unsafe-functions-check` - were added to clang-tidy. - - Improved :doc:`bugprone-optional-value-conversion ` check to detect conversion in argument of ``std::make_optional``. From 6d61cac8c2583280febf1551466a6f3b8a168f47 Mon Sep 17 00:00:00 2001 From: zishan Date: Fri, 2 May 2025 14:57:12 +0200 Subject: [PATCH 58/60] removed ctime_s and localtime_s for this pull request. these will be added to a new pull request. --- .../bugprone/UnsafeFunctionsCheck.cpp | 8 +++---- .../checks/bugprone/unsafe-functions.rst | 2 -- .../checkers/bugprone/unsafe-functions.c | 12 ---------- .../Checkers/StdLibraryFunctionsChecker.cpp | 24 ------------------- .../Inputs/std-c-library-functions-POSIX.h | 2 -- .../Analysis/std-c-library-functions-POSIX.c | 2 -- .../lib/dfsan/libc_ubuntu1404_abilist.txt | 2 -- .../sanitizer_common_interceptors.inc | 24 ------------------- 8 files changed, 4 insertions(+), 72 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp index 8f97fbf5b3178..b00cd81c36556 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp @@ -50,8 +50,8 @@ static StringRef getReplacementFor(StringRef FunctionName, StringRef AnnexKReplacementFunction = StringSwitch(FunctionName) .Cases("asctime", "asctime_r", "asctime_s") - .Cases("ctime", "ctime_r", "ctime_s") - .Cases("localtime", "localtime_r", "localtime_s") + .Cases("ctime", "ctime_r") + .Cases("localtime", "localtime_r") .Case("gets", "gets_s") .Default({}); if (!AnnexKReplacementFunction.empty()) @@ -62,8 +62,8 @@ static StringRef getReplacementFor(StringRef FunctionName, // should be matched and suggested. return StringSwitch(FunctionName) .Cases("asctime", "asctime_r", "strftime") - .Cases("ctime", "ctime_r", "ctime_s") - .Cases("localtime", "localtime_r", "localtime_s") + .Cases("ctime", "ctime_r") + .Cases("localtime", "localtime_r") .Case("gets", "fgets") .Case("rewind", "fseek") .Case("setbuf", "setvbuf"); diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst index 6abf4f8a7032c..17c5f9e66c74a 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst @@ -41,8 +41,6 @@ If *Annex K.* is not available, replacements are suggested only for the following functions from the previous list: - ``asctime``, ``asctime_r``, suggested replacement: ``strftime`` - - ``ctime``, ``ctime_r``, suggested replacement: ``ctime_s`` - - ``localtime``, ``localtime_r``, suggested replacement: ``localtime_s`` - ``gets``, suggested replacement: ``fgets`` The following functions are always checked, regardless of *Annex K* availability: diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c index bc1e6007201bb..031db4a81ffc3 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions.c @@ -108,8 +108,6 @@ char *ctime(const time_t *Timer); void f4(const time_t *Timer) { ctime(Timer); - // CHECK-MESSAGES-WITH-ANNEX-K: :[[@LINE-1]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead - // CHECK-MESSAGES-WITH-ANNEX-K-CERT-ONLY: :[[@LINE-2]]:3: warning: function 'ctime' is not bounds-checking and non-reentrant; 'ctime_s' should be used instead // no-warning WITHOUT-ANNEX-K } @@ -155,8 +153,6 @@ void fOptional() { typedef int errno_t; typedef size_t rsize_t; errno_t asctime_s(char *S, rsize_t Maxsize, const struct tm *TimePtr); -errno_t ctime_s(char *S, rsize_t Maxsize, const time_t *Timep); -errno_t localtime_s(const time_t *Timep, rsize_t Maxsize, const struct tm *TimePtr); errno_t strcat_s(char *S1, rsize_t S1Max, const char *S2); void fUsingSafeFunctions(const struct tm *Time, FILE *F, time_t *Timep) { @@ -166,14 +162,6 @@ void fUsingSafeFunctions(const struct tm *Time, FILE *F, time_t *Timep) { if (asctime_s(Buf, BUFSIZ, Time) != 0) return; - // no-warning, safe function from annex K is used - if (ctime_s(Buf, BUFSIZ, Time) != 0) - return; - - // no-warning, safe function from annex K is used - if (localtime_s(Timep, BUFSIZ, Time) != 0) - return; - // no-warning, safe function from annex K is used if (strcat_s(Buf, BUFSIZ, "something") != 0) return; diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index b2f2cb8b4541c..181a42e0ea3bc 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3542,16 +3542,6 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .ArgConstraint(NotNull(ArgNo(0))) .ArgConstraint(NotNull(ArgNo(1)))); - // struct tm *localtime_s(const time_t *restrict timer, - // struct tm *restrict result); - addToFunctionSummaryMap( - "localtime_s", - Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy}, - RetType{StructTmPtrTy}), - Summary(NoEvalCall) - .ArgConstraint(NotNull(ArgNo(0))) - .ArgConstraint(NotNull(ArgNo(1)))); - // char *asctime_r(const struct tm *restrict tm, char *restrict buf); addToFunctionSummaryMap( "asctime_r", @@ -3574,20 +3564,6 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( /*Buffer=*/ArgNo(1), /*MinBufSize=*/BVF.getValue(26, IntTy)))); - // char *ctime_s(char *buf, rsize_t buf_size, const time_t *timep); - addToFunctionSummaryMap( - "ctime_s", - Signature(ArgTypes{CharPtrTy, - BufferSize(ArgNo(0), BVF.getValue(26, IntTy)), - ConstTime_tPtrTy}, - RetType{CharPtrTy}), - Summary(NoEvalCall) - .ArgConstraint(NotNull(ArgNo(0))) - .ArgConstraint(BufferSize( - /*Buffer=*/ArgNo(0), - /*MinBufSize=*/BVF.getValue(26, IntTy)))); - .ArgConstraint(NotNull(ArgNo(2))) - // struct tm *gmtime_r(const time_t *restrict timer, // struct tm *restrict result); addToFunctionSummaryMap( diff --git a/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h b/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h index 16941434ab78e..63403bd1669b1 100644 --- a/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h +++ b/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h @@ -175,10 +175,8 @@ int utimes(const char *filename, const struct timeval times[2]); int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); struct tm *localtime(const time_t *tp); struct tm *localtime_r(const time_t *timer, struct tm *result); -struct tm *localtime_s(const time_t *restrict timer, struct tm *restrict result); char *asctime_r(const struct tm *restrict tm, char *restrict buf); char *ctime_r(const time_t *timep, char *buf); -char *ctime_s(char *buf, rsize_t buf_size, const time_t *timep); struct tm *gmtime_r(const time_t *restrict timer, struct tm *restrict result); struct tm *gmtime(const time_t *tp); int clock_gettime(clockid_t clock_id, struct timespec *tp); diff --git a/clang/test/Analysis/std-c-library-functions-POSIX.c b/clang/test/Analysis/std-c-library-functions-POSIX.c index ed85392e9391f..718076f779ba7 100644 --- a/clang/test/Analysis/std-c-library-functions-POSIX.c +++ b/clang/test/Analysis/std-c-library-functions-POSIX.c @@ -130,10 +130,8 @@ // CHECK: Loaded summary for: int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) // CHECK: Loaded summary for: struct tm *localtime(const time_t *tp) // CHECK: Loaded summary for: struct tm *localtime_r(const time_t *timer, struct tm *result) -// CHECK: Loaded summary for: struct tm *localtime_s(const time_t *restrict timer, struct tm *restrict result) // CHECK: Loaded summary for: char *asctime_r(const struct tm *restrict tm, char *restrict buf) // CHECK: Loaded summary for: char *ctime_r(const time_t *timep, char *buf) -// CHECK: Loaded summary for: char *ctime_s(char *buf, rsize_t buf_size, const time_t *timep) // CHECK: Loaded summary for: struct tm *gmtime_r(const time_t *restrict timer, struct tm *restrict result) // CHECK: Loaded summary for: struct tm *gmtime(const time_t *tp) // CHECK: Loaded summary for: int clock_gettime(clockid_t clock_id, struct timespec *tp) diff --git a/compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt b/compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt index d0d6e74743d32..9ffa56a238185 100644 --- a/compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt +++ b/compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt @@ -1547,7 +1547,6 @@ fun:ctanl=uninstrumented fun:ctermid=uninstrumented fun:ctime=uninstrumented fun:ctime_r=uninstrumented -fun:ctime_s=uninstrumented fun:cuserid=uninstrumented fun:daemon=uninstrumented fun:dcgettext=uninstrumented @@ -2206,7 +2205,6 @@ fun:llseek=uninstrumented fun:localeconv=uninstrumented fun:localtime=uninstrumented fun:localtime_r=uninstrumented -fun:localtime_s=uninstrumented fun:lockf=uninstrumented fun:lockf64=uninstrumented fun:log=uninstrumented diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 565870769e1ed..b47f46467b4b7 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -1376,16 +1376,6 @@ INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) { } return res; } -INTERCEPTOR(__sanitizer_tm *, localtime_s, unsigned long *timep, void *result) { - void *ctx; - COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result); - __sanitizer_tm *res = REAL(localtime_r)(timep, result); - if (res) { - COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); - unpoison_tm(ctx, res); - } - return res; -} INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep); @@ -1432,20 +1422,6 @@ INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) { } return res; } -INTERCEPTOR(char *, ctime_s, char *result, size_t result_size, - unsigned long *timep) { - void *ctx; - COMMON_INTERCEPTOR_ENTER(ctx, ctime_s, result, result_size, timep); - // FIXME: under ASan the call below may write to freed memory and corrupt - // its metadata. See - // https://github.com/google/sanitizers/issues/321. - char *res = REAL(ctime_s)(result, result_size, timep); - if (res) { - COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep)); - COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1); - } - return res; -} INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm); From e27e2332c08071e6c3cf3afdce8d103b95b1f01a Mon Sep 17 00:00:00 2001 From: zishan Date: Fri, 2 May 2025 15:01:45 +0200 Subject: [PATCH 59/60] format code with clang-format --- .../Checkers/StdLibraryFunctionsChecker.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index 181a42e0ea3bc..b01c2d3b5119f 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3564,15 +3564,15 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( /*Buffer=*/ArgNo(1), /*MinBufSize=*/BVF.getValue(26, IntTy)))); - // struct tm *gmtime_r(const time_t *restrict timer, - // struct tm *restrict result); - addToFunctionSummaryMap( - "gmtime_r", - Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy}, - RetType{StructTmPtrTy}), - Summary(NoEvalCall) - .ArgConstraint(NotNull(ArgNo(0))) - .ArgConstraint(NotNull(ArgNo(1)))); + // struct tm *gmtime_r(const time_t *restrict timer, + // struct tm *restrict result); + addToFunctionSummaryMap( + "gmtime_r", + Signature(ArgTypes{ConstTime_tPtrRestrictTy, StructTmPtrRestrictTy}, + RetType{StructTmPtrTy}), + Summary(NoEvalCall) + .ArgConstraint(NotNull(ArgNo(0))) + .ArgConstraint(NotNull(ArgNo(1)))); // struct tm * gmtime(const time_t *tp); addToFunctionSummaryMap( From 26b2ab32f0a8fb5771c1be52c5ce09ce6b7549ed Mon Sep 17 00:00:00 2001 From: zishan Date: Tue, 5 Aug 2025 20:25:27 +0200 Subject: [PATCH 60/60] remove stddef.h --- .../lib/sanitizer_common/sanitizer_common_interceptors.inc | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index b47f46467b4b7..5a15d75f0c86a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -34,7 +34,6 @@ //===----------------------------------------------------------------------===// #include -#include #include "interception/interception.h" #include "sanitizer_addrhashmap.h"