Skip to content

Commit fba6c88

Browse files
authored
[analyzer] Fix wrong builtin_*_overflow return type (#111253)
`builtin_*_overflow` functions return `_Bool` according to [1]. `BuiltinFunctionChecker` was using `makeTruthVal` w/o specifying explicit type, which creates an `int` value, since it's the type of any compassion according to C standard. Fix it by directly passing `BoolTy` to `makeTruthVal` Closes: #111147 [1] https://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins
1 parent 1789534 commit fba6c88

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ void BuiltinFunctionChecker::handleOverflowBuiltin(const CallEvent &Call,
183183
ProgramStateRef State = C.getState();
184184
SValBuilder &SVB = C.getSValBuilder();
185185
const Expr *CE = Call.getOriginExpr();
186+
auto BoolTy = C.getASTContext().BoolTy;
186187

187188
SVal Arg1 = Call.getArgSVal(0);
188189
SVal Arg2 = Call.getArgSVal(1);
@@ -193,8 +194,8 @@ void BuiltinFunctionChecker::handleOverflowBuiltin(const CallEvent &Call,
193194

194195
auto [Overflow, NotOverflow] = checkOverflow(C, RetValMax, ResultType);
195196
if (NotOverflow) {
196-
ProgramStateRef StateNoOverflow =
197-
State->BindExpr(CE, C.getLocationContext(), SVB.makeTruthVal(false));
197+
ProgramStateRef StateNoOverflow = State->BindExpr(
198+
CE, C.getLocationContext(), SVB.makeTruthVal(false, BoolTy));
198199

199200
if (auto L = Call.getArgSVal(2).getAs<Loc>()) {
200201
StateNoOverflow =
@@ -212,9 +213,9 @@ void BuiltinFunctionChecker::handleOverflowBuiltin(const CallEvent &Call,
212213
}
213214

214215
if (Overflow) {
215-
C.addTransition(
216-
State->BindExpr(CE, C.getLocationContext(), SVB.makeTruthVal(true)),
217-
createBuiltinOverflowNoteTag(C));
216+
C.addTransition(State->BindExpr(CE, C.getLocationContext(),
217+
SVB.makeTruthVal(true, BoolTy)),
218+
createBuiltinOverflowNoteTag(C));
218219
}
219220
}
220221

clang/test/Analysis/builtin_overflow.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang_analyze_cc1 -triple x86_64-unknown-unknown -verify %s \
2-
// RUN: -analyzer-checker=core,debug.ExprInspection
2+
// RUN: -analyzer-checker=core,debug.ExprInspection,alpha.core.BoolAssignment
33

44
#define __UINT_MAX__ (__INT_MAX__ * 2U + 1U)
55
#define __INT_MIN__ (-__INT_MAX__ - 1)
@@ -155,3 +155,12 @@ void test_uadd_overflow_contraints(unsigned a, unsigned b)
155155
return;
156156
}
157157
}
158+
159+
void test_bool_assign(void)
160+
{
161+
int res;
162+
163+
// Reproduce issue from GH#111147. __builtin_*_overflow funcions
164+
// should return _Bool, but not int.
165+
_Bool ret = __builtin_mul_overflow(10, 20, &res); // no crash
166+
}

0 commit comments

Comments
 (0)