title: Intentional friend class redifinition and -fstack-protector
body: <p>I have been trying testing if multiple definition of a friend class in different <code>.cpp</code> files would work. To do that I defined a main class inside <code>main_class.hpp</code> file:</p>
  
  <pre><code>class main_class
  {
  private:
      int a;
      int b;
  
      int gimme_a()
      {
          return a;
      }
  
  public:
      main_class(int a, int b) : a(a), b(b) {}
  
      int gimme_b()
      {
          return b;
      }
  
  friend class main_class_friend;
  };
  </code></pre>
  
  <p>Then I defined <code>main_class_friend</code> two times, firstly within <code>main_friend_class1.cpp</code> file:</p>
  
  <pre><code>class main_class_friend
  {
      main_class c;
  public:
  
      main_class_friend() : c(10, 10) {}
  
      int gimme_a()
      {
          return c.gimme_a();
      }
  
      int gimme_b()
      {
          return c.gimme_b();
      }
  };
  </code></pre>
  
  <p>and a corresponding public test function:</p>
  
  <pre><code>void test1()
  {
      main_class_friend ff;
      std::cout &lt;&lt; "Gimme a: " &lt;&lt; ff.gimme_a() &lt;&lt; " and gimme b: " &lt;&lt;
          ff.gimme_b() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>and then I defined the second <code>main_class_friend</code> within <code>main_friend_class2.cpp</code> and a corresponding public test function:</p>
  
  <pre><code>class main_class_friend
  {
  private:
      main_class ca;
      int c;
  public:
  
      main_class_friend() : ca(9 ,9), c(11) {}
  
      int gimme_a()
      {
          return ca.gimme_a();
      }
  
      int gimme_b()
      {
          return ca.gimme_b();
      }
  
      int gimme_c()
      {
          return c;
      }
  };
  
  
  void test2()
  {
      main_class_friend ff;
      std::cout &lt;&lt; "Gimme a: " &lt;&lt; ff.gimme_a() &lt;&lt; " and gimme b: " &lt;&lt; ff.gimme_b() 
          &lt;&lt; " and gimme c: " &lt;&lt; ff.gimme_c() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Finally I called <code>test1</code> and <code>test2</code> function inside main:</p>
  
  <pre><code>int main()
  {
      test1();
      test2();
      return 0;
  }
  </code></pre>
  
  <p>compiled the program (no errors from <code>g++</code>), and run it. The output was:</p>
  
  <pre><code>Gimme a: 9 and gimme b: 9
  *** stack smashing detected ***: ./a.out terminated
  Aborted (core dumped)
  </code></pre>
  
  <p>What's really weird for me is that <code>a</code> and <code>b</code> were initialized by the constructor inside <code>main_friend_class2.cpp</code> and not the one from <code>main_friend_class1.cpp</code> where <code>test1</code> function is defined.</p>
  
  <p>Then I found that stack smashing can be easily debugged when a program is compiled with <code>-fstack-protector</code> flag. So I compiled it once again and the output from the program was then:</p>
  
  <pre><code>Gimme a: 9 and gimme b: 9
  Gimme a: 9 and gimme b: 9 and gimme c: 11
  </code></pre>
  
  <p>so no more stack smashing problems, but both <code>test1</code> and <code>test2</code> functions use constructor from <code>main_friend_class2.cpp</code> file. </p>
  
  <p>What's going on around here? I don't get it. Why there is a stack smashing error if there is no buffer overflow, because there isn't any buffer used?</p>
  
  <p>My second question is: is there a way to define multiple times <code>main_class_friend</code>, but to use them in different files to make them "private" for the file inside which the class is used?</p>