Skip to content

Commit f1db3a5

Browse files
committed
[Clang][CodeGen] Check isUnderlyingBasePointerConstantNull in emitPointerArithmetic
1 parent 78d95cc commit f1db3a5

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4238,7 +4238,8 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,
42384238
else
42394239
elemTy = CGF.ConvertTypeForMem(elementType);
42404240

4241-
if (CGF.getLangOpts().PointerOverflowDefined)
4241+
if (CGF.getLangOpts().PointerOverflowDefined ||
4242+
CGF.isUnderlyingBasePointerConstantNull(pointerOperand))
42424243
return CGF.Builder.CreateGEP(elemTy, pointer, index, "add.ptr");
42434244

42444245
return CGF.EmitCheckedInBoundsGEP(

clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,18 @@ char *void_ptr(void *base, unsigned long offset) {
431431
return base + offset;
432432
}
433433

434+
int *constant_null_add(long offset) {
435+
// CHECK: define{{.*}} ptr @constant_null_add(i64 noundef %[[OFFSET:.*]])
436+
// CHECK-NEXT: [[ENTRY:.*]]:
437+
// CHECK-NEXT: %[[OFFSET_ADDR:.*]] = alloca i64, align 8
438+
// CHECK-NEXT: store i64 %[[OFFSET]], ptr %[[OFFSET_ADDR]], align 8
439+
// CHECK-NEXT: %[[OFFSET_RELOADED:.*]] = load i64, ptr %[[OFFSET_ADDR]], align 8
440+
// CHECK-NEXT: %[[ADD_PTR:.*]] = getelementptr i32, ptr null, i64 %[[OFFSET_RELOADED]]
441+
// CHECK-NEXT: ret ptr %[[ADD_PTR]]
442+
#line 1800
443+
return (int *)0 + offset;
444+
}
445+
434446
#ifdef __cplusplus
435447
}
436448
#endif

0 commit comments

Comments
 (0)