forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 344
[-Wunsafe-buffer-usage] Relax passing to __counted_by_or_null() #11170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
patrykstefanski
wants to merge
1
commit into
swiftlang:next
Choose a base branch
from
patrykstefanski:eng/pstefanski/PR-156006635
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,7 @@ void cb_cchar(const char *__counted_by(len) s, size_t len); | |
// expected-note@+1 3{{consider using 'std::span' and passing '.first(...).data()' to the parameter 's'}} | ||
void cb_cchar_42(const char *__counted_by(42) s); | ||
|
||
// 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'}} | ||
// 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'}} | ||
void cb_int(int *__counted_by(count) p, size_t count); | ||
|
||
// 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); | |
// expected-note@+1 6{{consider using 'std::span' and passing '.first(...).data()' to the parameter 'p'}} | ||
void cb_cint_multi(const int *__counted_by((a + b) * (c - d)) p, int a, int b, int c, int d); | ||
|
||
// 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'}} | ||
void sb_void(void *__sized_by(size) p, size_t size); | ||
|
||
// 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'}} | ||
void sb_cvoid(const void *__sized_by(size) p, size_t size); | ||
|
||
|
@@ -98,6 +101,10 @@ void sb_cint_42(const int *__sized_by(42) p); | |
|
||
void cb_cint_array(const int (* __counted_by(size) p)[10], size_t size); | ||
|
||
void cbn_int(int *__counted_by_or_null(count) p, size_t count); | ||
|
||
void sbn_void(void *__sized_by_or_null(size) p, size_t size); | ||
|
||
} // extern "C" | ||
|
||
// Check allowed classes and method. | ||
|
@@ -389,9 +396,22 @@ void from_cb_int_multi(int *__counted_by((a + b) * (c - d)) p, int a, int b, int | |
cb_int(p, 42); // expected-warning{{unsafe assignment to function parameter of count-attributed type}} | ||
} | ||
|
||
void nullptr_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'}} | ||
nullptr_as_arg(nullptr, 0); | ||
nullptr_as_arg(nullptr, size); // expected-warning{{unsafe assignment to function parameter of count-attributed type}} | ||
void nullptr_as_arg(size_t n) { | ||
cb_int(nullptr, 0); | ||
cb_int(nullptr, 42); // expected-warning{{unsafe assignment to function parameter of count-attributed type}} | ||
cb_int(nullptr, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}} | ||
|
||
sb_void(nullptr, 0); | ||
sb_void(nullptr, 42); // expected-warning{{unsafe assignment to function parameter of count-attributed type}} | ||
sb_void(nullptr, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}} | ||
|
||
cbn_int(nullptr, 0); | ||
cbn_int(nullptr, 42); | ||
cbn_int(nullptr, n); | ||
|
||
sbn_void(nullptr, 0); | ||
sbn_void(nullptr, 42); | ||
sbn_void(nullptr, n); | ||
} | ||
|
||
void single_variable() { | ||
|
@@ -665,6 +685,30 @@ static void previous_infinite_loop3(int * __counted_by(n + n * m) p, size_t n, | |
previous_infinite_loop3(p, n, q, r, m, o + 1); // expected-warning 2{{unsafe assignment to function parameter of count-attributed type}} | ||
} | ||
|
||
// Check nullable variants. | ||
|
||
void nonnull_tofrom_nullable(size_t n, | ||
int *__counted_by(n) cb, | ||
int *__counted_by_or_null(n) cbn, | ||
void *__sized_by(n) sb, | ||
void *__sized_by_or_null(n) sbn) { | ||
cb_int(cb, n); | ||
// __counted_by_or_null(count) can have arbitrary count if the pointer is | ||
// null. Since __counted_by() does not allow this, we diagnose | ||
// __counted_by_or_null() -> __counted_by(). | ||
cb_int(cbn, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}} | ||
patrykstefanski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
cbn_int(cb, n); | ||
// __counted_by() -> __counted_by_or_null() is fine. | ||
cbn_int(cbn, n); | ||
|
||
sb_void(sb, n); | ||
sb_void(sbn, n); // expected-warning{{unsafe assignment to function parameter of count-attributed type}} | ||
|
||
sbn_void(sb, n); | ||
sbn_void(sbn, n); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add more tests:
|
||
|
||
// Check default args. | ||
|
||
void cb_def_arg_null_0(int *__counted_by(count) p = nullptr, size_t count = 0); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant as in emitting a note diagnostic