Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-1028

bson-types.h / bson-macros.h set the aligned attribute for GCC incorrectly

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.6.0
    • Affects Version/s: 1.3.0-beta0
    • Component/s: libbson
    • None
    • Environment:
      GCC 5.2.1 / SUSE Linux

      In libbson/src/bson/bson-macros.h, it appears that for GCC, instead of:

      #  define BSON_ALIGNED_BEGIN(_N)
      #  define BSON_ALIGNED_END(_N) __attribute__((aligned (_N)))
      

      We should use:

      #  define BSON_ALIGNED_BEGIN(_N) __attribute__((aligned (_N)))
      #  define BSON_ALIGNED_END(_N)
      

      I noticed this because I was unable to declare the array

      bson_iter_t b[1];

      with compiler error "alignment of array elements is greater than element size". With the current code we sill have:

      // Checking size, expecting "bson_iter_t: 128"
      printf( "\nbson_iter_t: %zu\n", sizeof(bson_iter_t) );    // Output "bson_iter_t: 80"
      

      Example:

      #include "bson.h"
      
      // Example definition for bson_iter_t:
      //
      // BSON_ALIGNED_BEGIN (128)
      // typedef struct
      // {
      //    const uint8_t *raw;      /* The raw buffer being iterated. */
      //    uint32_t       len;      /* The length of raw. */
      //    uint32_t       off;      /* The offset within the buffer. */
      //    uint32_t       type;     /* The offset of the type byte. */
      //    uint32_t       key;      /* The offset of the key byte. */
      //    uint32_t       d1;       /* The offset of the first data byte. */
      //    uint32_t       d2;       /* The offset of the second data byte. */
      //    uint32_t       d3;       /* The offset of the third data byte. */
      //    uint32_t       d4;       /* The offset of the fourth data byte. */
      //    uint32_t       next_off; /* The offset of the next field. */
      //    uint32_t       err_off;  /* The offset of the error. */
      //    bson_value_t   value;    /* Internal value for various state. */
      // } bson_iter_t
      // BSON_ALIGNED_END (128);
      
      
      // Incorrectly defined type Bar
      typedef struct
      {
          char c;
          int  i;
      } Bar __attribute__((aligned(128)));
      
      
      // Correctly defined type Foo
      typedef struct __attribute__((aligned (128)))
      {
          char c;
          int  i;
      } Foo;
      
      int main (int argc, char *argv[])
      {
      
          printf( "bson_t: %zu\n",sizeof(bson_t    ) );   // Output:  "bson_t: 128"
      //     printf( "bson_t[1]: %zu\n",sizeof(bson_t[1] ) ); // GCC 5.2.1 compiler error: alignment of array elements is greater than element size
          
          printf( "\nbson_iter_t: %zu\n",sizeof(bson_iter_t   ) );    // Output "bson_iter_t: 80"
      //     printf( "bson_iter_t[1]: %zu\n",sizeof(bson_iter_t[1]) ); // GCC 5.2.1 compiler error: alignment of array elements is greater than element size
      
          printf( "\nBar: %zu\n",sizeof(Bar)    );   // Output:  "Bar: 8"
      //     printf( "Bar[1]: %zu\n",sizeof(Bar[1]) ); // GCC 5.2.1 compiler error: alignment of array elements is greater than element size
          
          printf( "\nFoo: %zu\n",sizeof(Foo)    );   // Output:  "Foo: 128"
          printf( "Foo[1]: %zu\n",sizeof(Foo[1]) );   // Output:  "Foo[1]: 128"
      
          return 0;
      }
      

      *****

      I would appreciate it if you could still include me in the bug hunt for 3.2 even though I'm technically 1 day past the deadline ... I tried to write it up nice to win you guys over!! Happy Hunting!!

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            ted Theodore B
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: