Lichtblitz
Lichtblitz

Reputation: 213

Which tool can list writing access to a specific variable in C?

Unfortunately I'm not even sure how this sort of static analysis is called. It's not really control flow analysis because I'm not looking for function calls and I don't really need data flow analysis because I don't care about the actual values.

I just need a tool that lists the locations (file, function) where writing access to a specific variable takes place. I don't even care if that list contained lines that are unreachable. I could imagine that writing a simple parser could suffice for this task but I'm certain that there must be a tool out there that does this simple analysis.

As a poor student I would appreciate free or better yet open source tools and if someone could tell me how this type of static analysis is actually called, I would be equally grateful!

EDIT: I forgot to mention there's no pointer arithmetic in the code base.

Upvotes: 4

Views: 656

Answers (3)

Benj
Benj

Reputation: 32418

Why don't you make the variable const and then note down all the errors where your compiler bans write access?

Note: This won't catch errors where the memory underlying the variable is written to in some erroneous manner such as a buffer overrun.

EDIT: For example:

const int a = 1;
a = 2;
a = 3;

My compiler produces:

1>MyProg.c(46): error C3892: 'a' : you cannot assign to a variable that is const
1>MyProg.c(47): error C3892: 'a' : you cannot assign to a variable that is const

Upvotes: 4

Pascal Cuoq
Pascal Cuoq

Reputation: 80355

Do you mean something like this?

enter image description here

This works for C programs that you have made the effort to analyze with Frama-C's value analysis. It is Open Source and the dependency information is also available programmatically. As static analyzers go, it is rather on the “precise” side of the spectrum. It will work better if your target is embedded C code.

Upvotes: 1

Marcus Riemer
Marcus Riemer

Reputation: 7728

I am not sure such a tool could be written. Pointers can be used to change arbitary data in memory without having any reference to other variables pointing to that data. Think about functions like memset(), which change whole blocks of memory.

If you are not interested in these kind of mutations, you would still have to take transitive pointers into account. In C, you can have any number of pointers pointing to the same data, and you would have to analyze where copies of these pointers are made. And then these copies can be copied again, ...

So even in the "simple" case it would require quite a big amount of code analysis.

Upvotes: 0

Related Questions