Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC or make flag to forbid particular standard library functions

Tags:

c

gcc

qmake

I'm working on a multithreaded project which suffered from a number of bugs due to use of the library function "strtok()" which is not thread-safe.

I'd love to find a way to forbid use of this function (and perhaps others) by defining something in the project file (Qt Creator / qmake) (e.g. redefining the symbol), to keep new developers or seasoned bugmakers from introducing it again.

Any ideas?

like image 703
Richard Arsenate Avatar asked Nov 10 '22 01:11

Richard Arsenate


1 Answers

this is what the GCC manual says about elimination of the use of certain library functions:

#pragma GCC poison
Sometimes, there is an identifier that you want to remove completely
from your program, and make sure that it never creeps back in. 
To enforce this, you can poison the identifier with this pragma. 
#pragma GCC poison is followed by a list of identifiers to poison. 
If any of those identifiers appears anywhere in the source after the directive, 
it is a hard error. For example,

          #pragma GCC poison printf sprintf fprintf
          sprintf(some_string, "hello");


will produce an error.

If a poisoned identifier appears 
as part of the expansion of a macro which was defined 
before the identifier was poisoned, it will not cause an error. 
This lets you poison an identifier 
without worrying about system headers defining macros that use it.

For example,

          #define strrchr rindex
          #pragma GCC poison rindex
          strrchr(some_string, 'h');


will not produce an error. 
like image 143
user3629249 Avatar answered Nov 14 '22 22:11

user3629249