Skip to content
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

Checked-pointer local variable can be initialized with itself (unsound) #1194

Open
mattmccutchen-cci opened this issue Oct 22, 2021 · 1 comment

Comments

@mattmccutchen-cci
Copy link
Member

While testing another issue, I made a typo and initialized a checked-pointer local variable with itself and was surprised to find that that compiles without error. An example:

#pragma CHECKED_SCOPE on

int main(void) {
  {
    // Put an invalid pointer in the memory that will be reused by `p`.
    long x = 1;
  }
  {
    _Ptr<char> p = p;
    (*p)++;  // SIGSEGV
  }
  return 0;
}

With -Wall, I get a compiler warning:

self_init.c:9:20: warning: variable 'p' is uninitialized when used within its own initialization [-Wuninitialized]
    _Ptr<char> p = p;
               ~   ^

Maybe this warning just needs to be made into an error when it occurs in the initializer of a checked-pointer variable?

@mattmccutchen-cci
Copy link
Member Author

Maybe this warning just needs to be made into an error when it occurs in the initializer of a checked-pointer variable?

Apparently that isn't good enough. The following produces no warning with -Wall:

#pragma CHECKED_SCOPE on

_Ptr<char> foo(_Ptr<_Ptr<char>> pp) {
  return *pp;
}

int main(void) {
  {
    // Put an invalid pointer in the memory that will be reused by `p`.
    long x = 1;
  }
  {
    _Ptr<char> p = foo(&p);
    (*p)++;  // SIGSEGV
  }
  return 0;
}

I guess we should disallow any use of p in its own initializer.

dtarditi added a commit that referenced this issue Sep 1, 2024
The test 3C/multiple_tu.c fails only on Windows. The problem is the test script. The script generates a command file in JSON format by piping a small generated Python program into the Python interpreter.  The program contains strings for file names. On Windows, the directory separator is a backslash character (`\`). The backslash is interpreted as a string escape character. This results in JSON with incorrect paths. Use raw Python strings for the file names so that Python leaves them alone.
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