[PATCH] D156711: [clang][ExprConstant] Fix assertion failure in constant expression folding

antoine moynault via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 31 09:24:00 PDT 2023


antmo created this revision.
Herald added a project: All.
antmo requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Since D140059 <https://reviews.llvm.org/D140059>, clang asserts on constant calls of string functions
(memchr, strncmp, memcmp, ...) when the last argument (length) does not
fit in an UInt63:

  strncmp("11", "12", -1);

Fix this by directly calling getZExtValue, as an unsigned value is
needed here


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156711

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/test/SemaCXX/constexpr-string.cpp


Index: clang/test/SemaCXX/constexpr-string.cpp
===================================================================
--- clang/test/SemaCXX/constexpr-string.cpp
+++ clang/test/SemaCXX/constexpr-string.cpp
@@ -93,6 +93,7 @@
 
   static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 6) == -1);
   static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this?
+  static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, (size_t)-1) == -1); // Check that this does not assert
   static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 6) == 0);
   static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
 
@@ -377,6 +378,7 @@
 
   static_assert(__builtin_memchr(kStr, 'a', 0) == nullptr);
   static_assert(__builtin_memchr(kStr, 'a', 1) == kStr);
+  static_assert(__builtin_memchr(kStr, 'a', (size_t)-1) == kStr); // Check that this does not assert
   static_assert(__builtin_memchr(kStr, '\0', 5) == nullptr);
   static_assert(__builtin_memchr(kStr, '\0', 6) == kStr + 5);
   static_assert(__builtin_memchr(kStr, '\xff', 8) == kStr + 4);
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -9346,7 +9346,7 @@
       APSInt N;
       if (!EvaluateInteger(E->getArg(2), N, Info))
         return false;
-      MaxLength = N.getExtValue();
+      MaxLength = N.getZExtValue();
     }
     // We cannot find the value if there are no candidates to match against.
     if (MaxLength == 0u)
@@ -12356,7 +12356,7 @@
       APSInt N;
       if (!EvaluateInteger(E->getArg(2), N, Info))
         return false;
-      MaxLength = N.getExtValue();
+      MaxLength = N.getZExtValue();
     }
 
     // Empty substrings compare equal by definition.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156711.545703.patch
Type: text/x-patch
Size: 1912 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230731/de62015a/attachment.bin>


More information about the cfe-commits mailing list