Reading through my book Expert C Programming, I came across the chapter on function interpositioning and how it can lead to some serious hard to find bugs if done unintentionally.
The example given in the book is the following:
my_source.c
mktemp() { ... }
main() {
mktemp();
getwd();
}
libc
mktemp(){ ... }
getwd(){ ...; mktemp(); ... }
According to the book, what happens in main()
is that mktemp()
(a standard C library function) is interposed by the implementation in my_source.c. Although having main()
call my implementation of mktemp()
is intended behavior, having getwd()
(another C library function) also call my implementation of mktemp()
is not.
Apparently, this example was a real life bug that existed in SunOS 4.0.3's version of lpr
. The book goes on to explain the fix was to add the keyword static
to the definition of mktemp()
in my_source.c; although changing the name altogether should have fixed this problem as well.
This chapter leaves me with some unresolved questions that I hope you guys could answer:
- Does GCC have a way to warn about function interposition? We certainly don't ever intend on this happening and I'd like to know about it if it does.
- Should our software group adopt the practice of putting the keyword
static
in front of all functions that we don't want to be exposed? - Can interposition happen with functions introduced by static libraries?
Thanks for the help.
EDIT
I should note that my question is not just aimed at interposing over standard C library functions, but also functions contained in other libraries, perhaps 3rd party, perhaps ones created in-house. Essentially, I want to catch any instance of interpositioning regardless of where the interposed function resides.