correctcomputation / checkedc-clang

This is the primary development repository for 3C, a tool for automatically converting legacy C code to the Checked C extension of C, which aims to enforce spatial memory safety. This repository is a fork of Checked C's.
14 stars 5 forks source link

Initializer are added for global variables on every declaration #713

Open john-h-kastner opened 3 years ago

john-h-kastner commented 3 years ago
int *a;
int *a;

is converted to

_Ptr<int> a = ((void *)0);
_Ptr<int> a = ((void *)0);

which is an error because a is initialized twice.

In fact, we don't need to initialize a at all. Global variables are initialized to 0 by default. Checked C agrees with this, emitting no error when compiling _Ptr<int> a; without an initializer.

mattmccutchen-cci commented 3 years ago

In fact, we don't need to initialize a at all. Global variables are initialized to 0 by default. Checked C agrees with this, emitting no error when compiling _Ptr<int> a; without an initializer.

Yes! I've noticed this in writing regression tests, and it's a bit ugly; I was planning to eventually file an issue, and you've saved me the trouble. A fix may just be a matter of copying this code revised in #657: https://github.com/correctcomputation/checkedc-clang/blob/2073f3318e9d350223e8b0befb384f4e218621c6/clang/lib/3C/StructInit.cpp#L62-L63 to here: https://github.com/correctcomputation/checkedc-clang/blob/2073f3318e9d350223e8b0befb384f4e218621c6/clang/lib/3C/DeclRewriter.cpp#L440 But we might want to wait and clean up all of the initializer addition code to fix #645 too.