summaryrefslogtreecommitdiffstats
path: root/tools/clang-tidy/test/bugprone-suspicious-memset-usage.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /tools/clang-tidy/test/bugprone-suspicious-memset-usage.cpp
parentInitial commit. (diff)
downloadfirefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz
firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/clang-tidy/test/bugprone-suspicious-memset-usage.cpp')
-rw-r--r--tools/clang-tidy/test/bugprone-suspicious-memset-usage.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/tools/clang-tidy/test/bugprone-suspicious-memset-usage.cpp b/tools/clang-tidy/test/bugprone-suspicious-memset-usage.cpp
new file mode 100644
index 0000000000..71fe7239a1
--- /dev/null
+++ b/tools/clang-tidy/test/bugprone-suspicious-memset-usage.cpp
@@ -0,0 +1,22 @@
+// https://clang.llvm.org/extra/clang-tidy/checks/bugprone-suspicious-memset-usage.html
+
+#include "structures.h"
+
+void test(int* ip, char* cp)
+{
+ // Case 1: Fill value is a character '0' instead of NUL '\0'.
+ memset(ip, '0', 1); // WARNING: suspicious for non-char pointers
+ memset(cp, '0', 1); // OK for char pointers
+
+ // Case 2: Fill value is truncated.
+ memset(ip, 0xabcd, 1); // WARNING: fill value gets truncated
+ memset(ip, 0x00cd, 1); // OK because value 0xcd is not truncated.
+ memset(ip, 0x00, 1); // OK because value is not truncated.
+
+ // Case 3: Byte count is zero.
+ memset(ip, sizeof(int), 0); // WARNING: zero length, potentially swapped
+ memset(ip, sizeof(int), 1); // OK with non-zero length
+
+ // See clang bug https://bugs.llvm.org/show_bug.cgi?id=38098
+ memset(ip, 8, 0); // OK with zero length without sizeof
+}