I’m working on a project that’s heavily multi-threaded, and was wondering if there’s a way to have the compiler flag the use of non-reentrant calls to the C library (e.g. strtok intsead of strtok_r)? If not, is there a list of calls that are non-reentrant so I can grep through my code base periodically?
A related question is if there’s a way to flag 3d party library use of non-reentrant calls.
I’m assuming reentrancy implies thread-safety, but not necessarily the other way around. Is there a good reason to use non-reentrant calls in a threaded project?
For source, you could possibly insist that every source file contains the line:
after the C headers, and then the
beware.hheader file contains:or some other suitable set of names that are unlikely to be real functions. That will result in compilation and/or linker errors.
For libraries, it’s a bit more difficult. You can look into using
nmto extract all the unresolved names in each object file and ensure none of the unsafe ones are called.This wouldn’t be the compiler doing it but it would be easy enough to incorporate into the build scripts. See the following transcript:
You can see the unresolved symbols in that output with a
Umarker (andgcchas very sneakily decided to useputsinstead ofprintfsince I gave it a constant string with no formatting commands).