shafik created this revision. shafik added reviewers: aaron.ballman, erichkeane, davide, rsmith. Herald added a project: All. shafik requested review of this revision.
The implementation of `__builtin_strncmp` and other related builtins function use `getExtValue()` to evaluate the size argument. This can cause a crash when the value does not fit into an `int64_t` value, which is can be expected since the type of the argument is `size_t`. The fix is to switch to using `getZExtValue()`. This fixes: https://github.com/llvm/llvm-project/issues/64876 https://reviews.llvm.org/D158557 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 @@ -676,3 +676,14 @@ } static_assert(test_address_of_incomplete_struct_type()); // expected-error {{constant}} expected-note {{in call}} } + +namespace GH64876 { +void f() { + __builtin_strncmp(0, 0, -511LL); + __builtin_memcmp(0, 0, -511LL); + __builtin_bcmp(0, 0, -511LL); + __builtin_wmemcmp(0, 0, -511LL); + __builtin_memchr((const void*)0, 1, -511LL); + __builtin_wmemchr((const wchar_t*)0, 1, -511LL); +} +} Index: clang/lib/AST/ExprConstant.cpp =================================================================== --- clang/lib/AST/ExprConstant.cpp +++ clang/lib/AST/ExprConstant.cpp @@ -9357,7 +9357,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) @@ -12381,7 +12381,7 @@ APSInt N; if (!EvaluateInteger(E->getArg(2), N, Info)) return false; - MaxLength = N.getExtValue(); + MaxLength = N.getZExtValue(); } // Empty substrings compare equal by definition.
Index: clang/test/SemaCXX/constexpr-string.cpp =================================================================== --- clang/test/SemaCXX/constexpr-string.cpp +++ clang/test/SemaCXX/constexpr-string.cpp @@ -676,3 +676,14 @@ } static_assert(test_address_of_incomplete_struct_type()); // expected-error {{constant}} expected-note {{in call}} } + +namespace GH64876 { +void f() { + __builtin_strncmp(0, 0, -511LL); + __builtin_memcmp(0, 0, -511LL); + __builtin_bcmp(0, 0, -511LL); + __builtin_wmemcmp(0, 0, -511LL); + __builtin_memchr((const void*)0, 1, -511LL); + __builtin_wmemchr((const wchar_t*)0, 1, -511LL); +} +} Index: clang/lib/AST/ExprConstant.cpp =================================================================== --- clang/lib/AST/ExprConstant.cpp +++ clang/lib/AST/ExprConstant.cpp @@ -9357,7 +9357,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) @@ -12381,7 +12381,7 @@ APSInt N; if (!EvaluateInteger(E->getArg(2), N, Info)) return false; - MaxLength = N.getExtValue(); + MaxLength = N.getZExtValue(); } // Empty substrings compare equal by definition.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits