[cfe-dev] The size expression of constant arrays.

Paolo Bolzoni bolzoni at cs.unipr.it
Thu Dec 11 03:07:39 PST 2008


On Mon, 24 Nov 2008 10:23:12 -0800
Chris Lattner <clattner at apple.com> wrote:
> > 2) need to reason on the textual representation that was used in the
> >    program also for integer literals (for example, there are coding
> >    rules that forbid the use of octal constants: the analyzer should
> >    flag their use in the source program).
> 
> Sure, Clang can handle this sort of thing with no problem.

The clang's AST does not seem to remember the real expression that initialized
the size of constant arrays. The clang::ConstantArrayType class does not have
method like getSizeExpr() of clang::VariableArrayType.

Is there a way to knows the exact expression even in case of constant arrays?


I'd like to check about how the constant has been obtained. Moreover I'd like
to see if there are some kind of overflow.

E.g., in my machine:

source_file.c:
int main() {
    int b[10000000 * 1000000];
}

$ clang -ast-print source_file.c
typedef struct __va_list_tag __builtin_va_list[1];

int main() {
  int b[1316134912];
}


or even more intriguing:

source_file2.c:
int main() {
    int b[1000000 * 1000000];
}

$ clang -ast-print source_file2.c 
typedef struct __va_list_tag __builtin_va_list[1];
te.c:3:11: error: array size is negative
    int b[1000000 * 1000000];
          ^~~~~~~~~~~~~~~~~

int main() {
  int b[3567587328];
}

1 diagnostic generated.


Just checking if the expression really gives the AST result would warn
against huge programming like this ones.

pb




More information about the cfe-dev mailing list