Skip to content

Fix passing negative value as count to function returning counted_by - #13892

Open
pandaaured wants to merge 1 commit into
swiftlang:nextfrom
pandaaured:bugfix
Open

Fix passing negative value as count to function returning counted_by#13892
pandaaured wants to merge 1 commit into
swiftlang:nextfrom
pandaaured:bugfix

Conversation

@pandaaured

Copy link
Copy Markdown

Added check for value of return type's count expression such that no negative values are passed as arguments to
functions returning counted_by.

Changed tests to correspond with correct program behavior and removed rdar://80808704 and rdar://103368466 comments from the tests which errored.

Fixes: #13550

Added error to case where a function returning `counted_by` is passed
a negative value as a count to the function.

Changed tests to correspond with correct program behavior and
removed rdar://80808704 and rdar://103368466 comments from the tests
which errored.
@hnrklssn
hnrklssn requested review from delcypher, hnrklssn, ojhunt and rapidsna and removed request for rapidsna August 17, 2026 18:07
Expr *CountE = ReturnCount.get();
if (auto *OVE = dyn_cast<OpaqueValueExpr>(CountE))
CountE = OVE->getSourceExpr();
CountE = CountE->IgnoreParenCasts();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do paren casts prevent const eval? If so, this should probably be IgnoreParenImpCasts. Otherwise we should be able to skip it

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be worth trying to come up with a test that would show the behavioral change

@@ -71,6 +71,6 @@ void call_param_with_shared_size(void) {
void *__sized_by(count * size) return_with_count_size(int count, int size);

void call_return_with_count_size(void) {
// FIXME: rdar://103368466
// expected-error@+1{{negative size value of -1 for 'void *__single __sized_by(count * size)' (aka 'void *__single')}}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we add another option to the select in err_bounds_safety_dynamic_count_negative so that we can emit for return value of type ...

@ojhunt ojhunt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have code change feedback, I'm assuming the new error messages in the Sema tests correspond to the required codegen changes, but want to be sure that that is the case.

@@ -71,7 +71,8 @@ void TestUCountNegFail(void) {
// CHECK-NEXT: unreachable, {{!annotation ![0-9]+}}
//
void TestCountNegRetFail(void) {
int *local_p = my_alloc_int(-1); //rdar://80808704
int x = -1;
int *local_p = my_alloc_int(x);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removed the 80808704 rdar reference, but I can't see a similar reference in the Sema tests so I'm not sure which of the tests this matches.

Part of me actually just wants the original test case copied verbatim into the Sema tests to make it unambiguous that it fails in Sema. (this is not a strongly held opinion)

@hnrklssn thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in the entire file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BoundsSafety] passing negative value as count to function returning counted_by does not error

3 participants