You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[-Wunsafe-buffer-usage] Relax passing to __counted_by_or_null()
Relax restrictions when passing to
__counted_by_or_null()/__sized_by_or_null() parameters:
- Allow the dependent count var to be anything.
- Allow passing from non-null variant to *_or_null(), but do warn for
passing *_or_null() to non-null variant.
rdar://156006635
// expected-note@+1 3{{consider using 'std::span' and passing '.first(...).data()' to the parameter 's'}}
70
70
voidcb_cchar_42(constchar *__counted_by(42) s);
71
71
72
-
// expected-note@+1 19{{consider using a safe container and passing '.data()' to the parameter 'p' and '.size()' to its dependent parameter 'count' or 'std::span' and passing '.first(...).data()' to the parameter 'p'}}
72
+
// expected-note@+1 22{{consider using a safe container and passing '.data()' to the parameter 'p' and '.size()' to its dependent parameter 'count' or 'std::span' and passing '.first(...).data()' to the parameter 'p'}}
// expected-note@+1 34{{consider using a safe container and passing '.data()' to the parameter 'p' and '.size()' to its dependent parameter 'count' or 'std::span' and passing '.first(...).data()' to the parameter 'p'}}
@@ -81,6 +81,9 @@ void cb_cint_42(const int *__counted_by(42) p);
81
81
// expected-note@+1 6{{consider using 'std::span' and passing '.first(...).data()' to the parameter 'p'}}
82
82
voidcb_cint_multi(constint *__counted_by((a + b) * (c - d)) p, int a, int b, int c, int d);
83
83
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'}}
// 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'}}
@@ -389,9 +396,22 @@ void from_cb_int_multi(int *__counted_by((a + b) * (c - d)) p, int a, int b, int
389
396
cb_int(p, 42); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
390
397
}
391
398
392
-
voidnullptr_as_arg(void * __sized_by(size) p, unsigned size) { //expected-note{{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'}}
393
-
nullptr_as_arg(nullptr, 0);
394
-
nullptr_as_arg(nullptr, size); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
399
+
voidnullptr_as_arg(size_t n) {
400
+
cb_int(nullptr, 0);
401
+
cb_int(nullptr, 42); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
402
+
cb_int(nullptr, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
403
+
404
+
sb_void(nullptr, 0);
405
+
sb_void(nullptr, 42); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
406
+
sb_void(nullptr, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
407
+
408
+
cbn_int(nullptr, 0);
409
+
cbn_int(nullptr, 42);
410
+
cbn_int(nullptr, n);
411
+
412
+
sbn_void(nullptr, 0);
413
+
sbn_void(nullptr, 42);
414
+
sbn_void(nullptr, n);
395
415
}
396
416
397
417
voidsingle_variable() {
@@ -664,3 +684,23 @@ static void previous_infinite_loop3(int * __counted_by(n + n * m) p, size_t n,
664
684
previous_infinite_loop3(p, n, q, r, m, o);
665
685
previous_infinite_loop3(p, n, q, r, m, o + 1); // expected-warning 2{{unsafe assignment to function parameter of count-attributed type}}
666
686
}
687
+
688
+
// Check nullable variants.
689
+
690
+
voidnonnull_tofrom_nullable(size_t n,
691
+
int *__counted_by(n) cb,
692
+
int *__counted_by_or_null(n) cbn,
693
+
void *__sized_by(n) sb,
694
+
void *__sized_by_or_null(n) sbn) {
695
+
cb_int(cb, n);
696
+
cb_int(cbn, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
697
+
698
+
cbn_int(cb, n);
699
+
cbn_int(cbn, n);
700
+
701
+
sb_void(sb, n);
702
+
sb_void(sbn, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
0 commit comments