title: Using LD_PRELOAD on Fedora 25 causes Segmentation Fault
body: <p>I have found a strange behavior while I was trying to use a library that I wrote a long time ago. The main problem is, when the program is executed on <strong>Fedora 25</strong> and linked to my library by using LD_PRELOAD, the system raises a Segmentation Fault. I've made a small sample of my old library to easily understand the problem.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;malloc.h&gt;
  
  extern void *__libc_malloc(size_t size);
  
  void *malloc(size_t size) 
  {
    void *ptr = __libc_malloc(size);
    fprintf(stdout, "Malloc(%d)-&gt;(%p)\n", (int) size, ptr);
    return ptr;
  }
  </code></pre>
  
  <p>This code was compiled by using these parameters:</p>
  
  <pre><code>gcc -c -fPIC -O3 -Wall -o libtest.o libtest.c
  gcc -shared -o libtest.so libtest.o
  </code></pre>
  
  <p>The program was executed as follows:</p>
  
  <pre><code>LD_PRELOAD=./libtest.so ./progtest
  </code></pre>
  
  <p>I found out that the "<strong>fprintf</strong>" line was causing the Segfault problem. So I've changed the "<strong>stdout</strong>" file descriptor to "<strong>stderr</strong>", and the problem was gone.</p>
  
  <p>Then I tested the same code using the "<strong>stdout</strong>" file descriptor as output for "<strong>fprintf</strong>" on another machine running <strong>CentOS 7</strong>, and it worked fine in both cases using "<strong>stdout</strong>" and "<strong>stderr</strong>".</p>
  
  <p>By observing these results, I wonder what I am missing and why this is happening.</p>
  
  <p>GLibc and GCC versions installed on Fedora 25:</p>
  
  <blockquote>
    <p>GNU ld version 2.26.1-1.fc25</p>
    
    <p>gcc (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)</p>
  </blockquote>
  
  <p>GLibc and GCC versions installed on CentOS 7:</p>
  
  <blockquote>
    <p>GNU ld version 2.25.1-22.base.el7</p>
    
    <p>gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)</p>
  </blockquote>