[BoundsSafety] warn when assigning uninitialized memory to a `single` pointer with a flexible array member
For the program below the assignment to p will incur a bounds check, including checking that p->fam + p->len is not greater than the upper bound of the implicit bidi we're assigning from. This involves reading p->len, which LLVM will trivially detect as uninitialized, invoking UB and ruining your day.
We should warn on all assignments from malloc() to foo * __single, for any foo containing a flexible array member. Bonus points if we can detect this for uninitialized stack memory as well without too much extra hassle, but stack memory really shouldn't be used for FAM structs, so that's not as high priority. The diagnostic should include a suggestion to use __bidi_indexable instead, and a fix-it to do the transformation.
#include <ptrcheck.h>
#include <stdlib.h>
struct A {
int len;
char fam[__counted_by(len)];
};
void foo(int len) {
struct A * __single p = malloc(sizeof(struct A) + len);
}
I would be interested in working on this issue!
I'm going on vacation for a few weeks, so I might not be as responsive during that time. You can also reach out to @rapidsna and @delcypher if you have questions.
Sounds good, will do!
What would be a preferable location for this check and resulting diagnostic to be emitted? I am uncertain if it would be more fitting in BoundsSafetySuggestions.cpp or DynamicCountPointerAssignmentAnalysis.cpp.