checkedc / checkedc-clang

This repo contains a version of clang that is being modified to support Checked C. Checked C is an extension to C that lets programmers write C code that is guaranteed by the compiler to be type-safe.
https://www.checkedc.org
502 stars 74 forks source link

Improper handling of itype in the case of return values #614

Open Machiry opened 5 years ago

Machiry commented 5 years ago

Problem:

The following program is supposed to compile file.

#include <string_checked.h>
#include <stdchecked.h>

int main(int argc, nt_array_ptr<char> argv checked[] : count(argc)) {
    nt_array_ptr<char> src1 = strstr(argv[0], argv[1]);
}

But we are getting the following error:

5:24: error: initializing '_Nt_array_ptr<char>' with an expression of incompatible type 'char *'
    nt_array_ptr<char> src1 = strstr(argv[0], argv[1]);
                       ^      ~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

I think this is because of the improper handling of itypes for return values.

hasantouma commented 5 years ago

I came across the same issue, but with literal strings, not just variables.

This code will produce an error:

#include <string_checked.h>

int main(int argc, nt_array_ptr<char> argv checked[] : count(argc)) {

  _Nt_array_ptr<char> foo = strdup("hello");

}

error output:

foo.c:8:23: error: initializing '_Nt_array_ptr<char>' with an expression of incompatible type 'char *'
  _Nt_array_ptr<char> foo = strdup("hello");
                      ^     ~~~~~~~~~~~~~~~
1 error generated.

But if you wrap the code in a CHECKED_SCOPE then the error is resolved, i.e.:

#include <string_checked.h>
#include <stdchecked.h>

#pragma CHECKED_SCOPE ON

int main(int argc, nt_array_ptr<char> argv checked[] : count(argc)) {

  _Nt_array_ptr<char> foo = strdup("hello");

}

#pragma CHECKED_SCOPE OFF

You can alternatively cast the return type of strdup() to _Nt_array_ptr<char> and that will also resolve the error, i.e.:

#include <string_checked.h>

int main() {

  _Nt_array_ptr<char> foo = (_Nt_array_ptr<char>)strdup("hello");

}
dtarditi commented 5 years ago

This is a compiler bug. It's likely related to an oversight when handling of _Nt_array_ptr<char>. The following code works as expected:


char *f(void) : itype(_Array_ptr<char>);

void g(void) {
    array_ptr<char> src1 = f();
}