Skip to content

Commit 8967fbd

Browse files
[-Wunsafe-buffer-usage] Handle more null pointer constants
In addition to `nullptr`, handle more cases of null pointer constants, such as `0`. rdar://156006053
1 parent 9b2c608 commit 8967fbd

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,8 @@ static bool isCountAttributedPointerArgumentSafeImpl(
10341034
PtrArgNoImp = DAE->getExpr()->IgnoreParenImpCasts();
10351035

10361036
// check form 0:
1037-
if (PtrArgNoImp->getType()->isNullPtrType()) {
1037+
if (PtrArgNoImp->isNullPointerConstant(Context,
1038+
Expr::NPC_ValueDependentIsNotNull)) {
10381039
if (isOrNull)
10391040
return true;
10401041
if (CountArg)

clang/test/SemaCXX/warn-unsafe-buffer-usage-count-attributed-pointer-argument.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void cb_cint_42(const int *__counted_by(42) p);
8181
// expected-note@+1 6{{consider using 'std::span' and passing '.first(...).data()' to the parameter 'p'}}
8282
void cb_cint_multi(const int *__counted_by((a + b) * (c - d)) p, int a, int b, int c, int d);
8383

84-
// expected-note@+1 3{{consider using a safe container and passing '.data()' to the parameter 'p' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'p'}}
84+
// expected-note@+1 +{{consider using a safe container and passing '.data()' to the parameter 'p' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'p'}}
8585
void sb_void(void *__sized_by(size) p, size_t size);
8686

8787
// expected-note@+1 13{{consider using a safe container and passing '.data()' to the parameter 'p' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'p'}}
@@ -416,6 +416,24 @@ void nullptr_as_arg(size_t n) {
416416
sbn_void(nullptr, n);
417417
}
418418

419+
void zero_as_arg(size_t n) {
420+
cb_int(0, 0);
421+
cb_int(0, 42); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
422+
cb_int(0, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
423+
424+
sb_void(0, 0);
425+
sb_void(0, 42); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
426+
sb_void(0, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
427+
428+
cbn_int(0, 0);
429+
cbn_int(0, 42);
430+
cbn_int(0, n);
431+
432+
sbn_void(0, 0);
433+
sbn_void(0, 42);
434+
sbn_void(0, n);
435+
}
436+
419437
void single_variable() {
420438
int Var;
421439
int Arr[10];

0 commit comments

Comments
 (0)