Bug #31326 No compile check of order of initializations
Submitted: 1 Oct 2007 20:52 Modified: 16 Mar 2008 10:41
Reporter: Lars Thalmann Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:5.0,5.1 OS:Any
Assigned to: Georgi Kodinov CPU Architecture:Any

[1 Oct 2007 20:52] Lars Thalmann
Description:
The order of initialization of fields in constructors can 
potentially could cause crashes. The flag to enable reorder 
checking for GCC is -Wreorder and it is not enabled in
our current build.

Attached is an example of a a program, and here is the result of
executing it with the correct order of the fields (first), and with the
wrong order of the fields (second).

   mats@romeo:~/lang/cc/tests/samples$ ./init_1
   Allocating 3 bytes
   Copying 2 bytes
   Hi
   Allocating 35 bytes
   Copying 34 bytes
   Supercalifragilisticexpialidoceous
   mats@romeo:~/lang/cc/tests/samples$ ./init_1
   Allocating 3086915505 bytes
   Copying 2 bytes
   Segmentation fault (core dumped)

#include <cstdlib>
#include <cstdio>
#include <cstring>

void *x_malloc(size_t size) {
  printf("Allocating %u bytes\n", size);
  return malloc(size);
}

class string {
public:
  /*
    Observe that the order of the initializers in the initializer list
    is not the order in which they are executed. The order is decided
    by the order in which the fields are given in the class
    definition.

    With -Wreorder on, you will get a warning here, without it, it
    will not work.
   */
  string(char const* str, size_t len = 0)
  : m_len(len > 0 ? len : strlen(str)),
    m_str((char*) x_malloc(m_len + 1))
  {
    printf("Copying %u bytes\n", m_len);
    strncpy(m_str, str, m_len);
  }

  void print() const {
    puts(m_str);
  }

private:
  /*
    These two fields are in the wrong order, so if you're lucky, it
    might work (but allocate the wrong number of bytes for the
    string), and if you're unlucky, it will crash.

    It crashes for me.
   */
  char *m_str;
  size_t m_len;
};

int main()
{
  {
    string str("Hi");
    str.print();
  }
  {
    string str("Supercalifragilisticexpialidoceous");
    str.print();
  }
}

How to repeat:
Read code

Suggested fix:
Fix code so that it is in correct order.
Enable flag to check this in all builds.
[13 Dec 2007 10:30] Georgi Kodinov
Lars,

How do you compile the server ?
Quick check shows that -Wreorder is present in BUILD/SETUP.sh (used by most of the BUILD/compile-* scripts) for 5.0 and 5.1.
So If you compile with (e.g.) BUILD/compile-pentium-debug-max you will get these warnings.
[13 Dec 2007 15:40] Sergei Golubchik
existing code still needs to be fixed to use correct initialization order
[14 Dec 2007 13:22] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/39989

ChangeSet@1.2643, 2007-12-14 15:21:37+02:00, gkodinov@macbook.gmz +5 -0
  Bug #31326: No compile check of order of initializations
   fixed -Wreorder warnings
[18 Feb 2008 11:35] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/42477

ChangeSet@1.2560, 2008-02-18 14:35:44+03:00, kaa@kaamos.(none) +1 -0
  Initialization order cleanups to get rid of warnings from the 
  -Wreorder option added by the patch for bug#31326.
[18 Feb 2008 11:39] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/42480

ChangeSet@1.2572, 2008-02-18 14:38:59+03:00, kaa@kaamos.(none) +1 -0
  Initialization order cleanups to get rid of warnings from the 
  -Wreorder option added by the patch for bug#31326.
[13 Mar 2008 19:27] Bugs System
Pushed into 6.0.5-alpha
[13 Mar 2008 19:35] Bugs System
Pushed into 5.1.24-rc
[16 Mar 2008 10:41] MC Brown
No user-visible changes, no documentation needed.