Skip to content

[BoundsSafety] easier external bounds annotation tracking #10721

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
mat-c opened this issue May 21, 2025 · 0 comments
Open

[BoundsSafety] easier external bounds annotation tracking #10721

mat-c opened this issue May 21, 2025 · 0 comments

Comments

@mat-c
Copy link

mat-c commented May 21, 2025

Hi,

I am using stable/20250402

While testing -fbounds-safety on some project I found some error forcing some annoying conversion.

For example

int count(char *__counted_by(len) p, int len)
{
        int i = 0;
        while(len--) {
                i+= *p++;
        }
        return i;
}

while produce errors

hello3.c:8:23: error: assignment to 'char *__single __counted_by(len)' (aka 'char *__single') 'p' requires corresponding assignment to 'len'; add self assignment 'len = len' if the value has not changed
    8 |                 i+= *p++;
      |                       ^
hello3.c:7:18: error: assignment to 'len' requires corresponding assignment to 'char *__single __counted_by(len)' (aka 'char *__single') 'p'; add self assignment 'p = p' if the value has not changed
    7 |         while(len--) {
      |                  ^
2 errors generated.

But the following code doesn't produce any error

int count2(char *__counted_by(len2) p2, int len2)
{
        int len = len2;
        char *p = p2;
        int i = 0;
        while(len--) {
                i+= *p++;
        }
        return i;
}

parameter in C are local variable.
Isn't possible that function parameters require annotation to pointer, but that they are treated like local variable ?

In function entry, the compiler know the size of the buffer, and auto track pointer update.
May be a knew __counted_by keyword is need to express that we don't want the pointer p to be always size len, but that we want only to provide buffer size in function entry.
For example a something based of __ended_by (do not work as p is a single pointer)

int count(char *__ended_by(p+len) p, int len)
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

No branches or pull requests

1 participant