title: How to catch unintentional function interpositioning?
body: <p>Reading through my book <em>Expert C Programming</em>, I came across the chapter on function interpositioning and how it can lead to some serious hard to find bugs if done unintentionally.</p>
  
  <p>The example given in the book is the following:</p>
  
  <h3>my_source.c</h3>
  
  <pre><code>mktemp() { ... }
  
  main() {
    mktemp();
    getwd();
  }
  </code></pre>
  
  <h3>libc</h3>
  
  <pre><code>mktemp(){ ... }
  getwd(){ ...; mktemp(); ... }
  </code></pre>
  
  <p>According to the book, what happens in <code>main()</code> is that <code>mktemp()</code> (a standard C library function) is interposed by the implementation in <strong>my_source.c</strong>.  Although having <code>main()</code> call my implementation of <code>mktemp()</code> is intended behavior, having <code>getwd()</code> (another C library function) <strong><em>also</em></strong> call my implementation of <code>mktemp()</code> is not.</p>
  
  <p>Apparently, this example was a real life bug that existed in SunOS 4.0.3's version of <code>lpr</code>.  The book goes on to explain the fix was to add the keyword <code>static</code> to the definition of <code>mktemp()</code> in <strong>my_source.c</strong>; although changing the name altogether should have fixed this problem as well.  </p>
  
  <p>This chapter leaves me with some unresolved questions that I hope you guys could answer:</p>
  
  <ol>
  <li>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.</li>
  <li>Should our software group adopt the practice of putting the keyword <code>static</code> in front of all functions that we don't want to be exposed?</li>
  <li>Can interposition happen with functions introduced by static libraries?</li>
  </ol>
  
  <p>Thanks for the help.</p>
  
  <h3>EDIT</h3>
  
  <p>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 <strong><em>any</em></strong> instance of interpositioning regardless of where the interposed function resides.</p>