How to catch unintentional function interpositioning with GCC?

Posted by SiegeX on Stack Overflow See other posts from Stack Overflow or by SiegeX
Published on 2010-05-06T16:53:52Z Indexed on 2010/05/06 16:58 UTC
Read the original article Hit count: 201

Filed under:
|
|
|

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:

  1. 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?
  2. 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.
  3. Can interposition happen with functions introduced by static libraries?

Thanks for the help.

© Stack Overflow or respective owner

Related posts about gcc

Related posts about c