[clang] [analyzer] Demonstrate superfluous unsigned >= 0 assumption (PR #78442)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 04:59:02 PST 2024


https://github.com/NagyDonat created https://github.com/llvm/llvm-project/pull/78442

This commit adds a testcase which highlights the current incorrect behavior of the CSA diagnostic generation: it produces a note which says "Assuming 'arg' is >= 0" in a situation where this is not a fresh assumption because 'arg' is an unsigned integer.

I also created ticket #78440 to track this bug.

>From d76fb380052b6de7e47f4d7499717c6df8b4af93 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Wed, 17 Jan 2024 13:28:20 +0100
Subject: [PATCH] [analyzer] Demonstrate superfluous unsigned >= 0 assumption

This commit adds a testcase which highlights the current incorrect
behavior of the CSA diagnostic generation: it produces a note which says
"Assuming 'arg' is >= 0" in a situation where this is not a fresh
assumption because 'arg' is an unsigned integer.
---
 clang/test/Analysis/assuming-unsigned-ge-0.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 clang/test/Analysis/assuming-unsigned-ge-0.c

diff --git a/clang/test/Analysis/assuming-unsigned-ge-0.c b/clang/test/Analysis/assuming-unsigned-ge-0.c
new file mode 100644
index 000000000000000..553e68cb96c6bd3
--- /dev/null
+++ b/clang/test/Analysis/assuming-unsigned-ge-0.c
@@ -0,0 +1,19 @@
+// RUN: %clang_analyze_cc1 -analyzer-output=text        \
+// RUN:     -analyzer-checker=core -verify %s
+
+int assuming_unsigned_ge_0(unsigned arg) {
+  // TODO This testcase demonstrates the current incorrect behavior of Clang
+  // Static Analyzer: here 'arg' is unsigned, so "arg >= 0" is not a fresh
+  // assumption, but it still appears in the diagnostics as if it's fresh:
+  // expected-note at +2 {{Assuming 'arg' is >= 0}}
+  // expected-note at +1 {{Taking false branch}}
+  if (arg < 0)
+    return 0;
+  // expected-note at +2 {{Assuming 'arg' is <= 0}}
+  // expected-note at +1 {{Taking false branch}}
+  if (arg > 0)
+    return 0;
+  // expected-note at +2 {{Division by zero}}
+  // expected-warning at +1 {{Division by zero}}
+  return 100 / arg;
+}



More information about the cfe-commits mailing list