Skip to content

Commit d6974c0

Browse files
rotaterighttstellar
authored andcommitted
[Analysis] improve function signature checking for snprintf
The check for size_t parameter 1 was already here for snprintf_chk, but it wasn't applied to regular snprintf. This could lead to mismatching and eventually crashing as shown in: https://llvm.org/PR50885 (cherry picked from commit 7f55557)
1 parent 60c388a commit d6974c0

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

llvm/lib/Analysis/TargetLibraryInfo.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,10 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
893893
FTy.getReturnType()->isIntegerTy(32);
894894

895895
case LibFunc_snprintf:
896-
return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
897-
FTy.getParamType(2)->isPointerTy() &&
898-
FTy.getReturnType()->isIntegerTy(32));
896+
return NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
897+
IsSizeTTy(FTy.getParamType(1)) &&
898+
FTy.getParamType(2)->isPointerTy() &&
899+
FTy.getReturnType()->isIntegerTy(32);
899900

900901
case LibFunc_snprintf_chk:
901902
return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&

llvm/test/Transforms/InstCombine/simplify-libcalls.ll

+12
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ define double @fake_ldexp_16(i16 %x) {
217217
ret double %z
218218
}
219219

220+
; PR50885 - this would crash in ValueTracking.
221+
222+
declare i32 @snprintf(i8*, double, i32*)
223+
224+
define i32 @fake_snprintf(i32 %buf, double %len, i32 * %str) {
225+
; CHECK-LABEL: @fake_snprintf(
226+
; CHECK-NEXT: [[CALL:%.*]] = call i32 @snprintf(i8* undef, double [[LEN:%.*]], i32* [[STR:%.*]])
227+
; CHECK-NEXT: ret i32 [[CALL]]
228+
;
229+
%call = call i32 @snprintf(i8* undef, double %len, i32* %str)
230+
ret i32 %call
231+
}
220232

221233
attributes #0 = { nobuiltin }
222234
attributes #1 = { builtin }

0 commit comments

Comments
 (0)