2

I would like to use gcc -fanalyzer across multiple c-files (*.c). E.g. given the short program

#include <string.h>

int get_buff(int id, int** buff)
{
    static int empty[128];
    switch(id)
    {
        case 0:
            *buff = empty;
            return 0;
        default:
            return -1;
    }
}

int main()
{
    int* from;
    get_buff(1, &from);

    int to[128];
    memcpy(to, from, sizeof(to));

    return 0;
}

will result in the following warning with gcc 14:

main.c:22:5: warning: use of uninitialized value ‘from’ [CWE-457] [-Wanalyzer-use-of-uninitialized-value]

But if I put get_buff in a separate c-file (get_buff.c) then I do not get this warning when compiled with gcc -fanalyzer main.c get_buff.c. How do I use -fanalyzer with multiple compilation units?

1 Answer 1

4

Is there a way to make gcc -fanalyzer work across compilation units?

Yes, use -flto:

-flto[=n]
This option runs the standard link-time optimizer. When invoked with source code, it generates GIMPLE (one of GCC’s internal representations) and writes it to special ELF sections in the object file. When the object files are linked together, all the function bodies are read from these ELF sections and instantiated as if they had been part of the same translation unit. [...]

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thank you. It even works with shared objects: gcc -fanalyzer -shared -fPIC main.c get_buff.c -flto -o lib.so . Currently if I compile them separately and and link afterwards with gcc then no output is generated. I will try to work it out with separate compilation and integrate into my cmake build.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.