48 points · by panic · 3y ago · 49 comments
void foo(size_t elem_count, int elems[elem_count]);
Unfortunately this constraints the argument order. C23 fixes that with something like this: void foo(size_t elem_count; int elems[elem_count], elem_count);
(Might be slightly wrong, going from memory.)[1] With main() being the exception.
[2] Latest version being https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3121.pdf.
In both C and C++, it drives me absolutely bonkers that declaration order matters so much.
Point of personal preference, but they're the last languages I use with any regularity where you can't just reference a symbol that will be declared later in the file. In this case, the compiler should be able to figure it out without any of that hinting; the scope of potential valid bindings is in the parentheses. It's a very constrained scope!
https://godbolt.org/z/oc6MTWjYd
For your syntax we will probably just also add it is an option.
void f(char (*pa)[7]);
void g(int x, char (*pa)[x]);
void h(int x, char p[static x]);
char a[10];
then f(&a) is a constraint violation, and I believe g(7,&a) might actually be UB, whereas h(7,a) is fine. On the other hand, in the latter case void h(int x, char p[static x]) { p = (char *)&x; }
is legal, so extending that with bounds checks is also not without its problems.It's important to recognize that in plain C the above is simply an aid to someone looking at the header and is no different from
void foo(size_t elem_count, int elems[]);
As such int j;
foo(20, &j);
is fine. Under this RFC this is an error and the call site would trap at runtime (if not picked up a compile time).The RFC also comments on (and handles)
void foo(int elems[] __counted_by(elem_count), size_t elem_count);
Look for references to "delayed parsing". It also handles similar in structs.And compilers do this already to some degree: https://godbolt.org/z/c7xdazKrd
This RFC applies to all pointers in all cases, and bounds checking occurs on every pointer access at runtime. So while the annotations clearly overlap, the language semantics are vastly different.
The syntax this RFC uses is based on the standardized syntax for attributes in C.
If this patent is genuinely enforceable, surely the names of the keywords aren't relevant?
struct TerribleArray {
int count;
int *elements;
}
correctly bounds check that elements was at least count elements long? and that indexing into elements is restricted to 0..<count?This will fail like all the other annotation attempts have failed.
I like it and it is on my list to implement as a prototype for GCC.
And WG14 takes security seriously, but I think you overestimate the power WG14 has to simply change things. WG14 is supposed to standardize existing practice, not reinvent the language. So you should complain to compiler vendors. Or contribute to the development of open-source compilers.
Plenty of compiler vendors have extensions for safer C. Microsoft introduced similar annotation mechanism like the one being discussed here when Windows XP SP 2 came to be, in 2001.
Apparently 50 years weren't enough to make it happen.
Contrast this with how WG21 looks into security and code safety, enough papers on the subject, specially after the cybersecurity bills started to come about.
Without this code execution would just continue to whatever function is in memory, if there’s one.
And yet… it’s still considered c language.
[1] C17 standard 4.5