Fix makesdna not checking alignment for a non-native platform
authorSergey Sharybin <sergey.vfx@gmail.com>
Mon, 9 May 2016 07:53:50 +0000 (09:53 +0200)
committerSergey Sharybin <sergey.vfx@gmail.com>
Mon, 9 May 2016 07:54:51 +0000 (09:54 +0200)
This was causing alignment issues which were only visible on a platform
of particular bitness, so it was easy to break stuff for 32bit when
working on 64bit platform and vice versa.

source/blender/makesdna/DNA_gpencil_types.h
source/blender/makesdna/intern/makesdna.c

index 43d42012b2c6077c30de07877e43b76eecec7267..41f53f9f51c17d89e5975e48f096943d380c1ea9 100644 (file)
@@ -80,7 +80,7 @@ typedef struct bGPDstroke {
        
        bGPDtriangle *triangles;/* tesselated triangles for GP Fill */
        int tot_triangles;      /* number of triangles in array */
-       short  pad2[2];         /* avoid compiler align error */
+       int pad1, *pad2;
 
        double inittime;                /* Init time of stroke */
 } bGPDstroke;
index 651794da50d5c492682839879e99aff8385fb3c0..fc94a8d9ff453bf445ea13a98a719a91249c449d 100644 (file)
@@ -720,6 +720,28 @@ static int arraysize(const char *str)
        return mul;
 }
 
+static bool check_field_alignment(int firststruct, int structtype, int type, int len,
+                                  const char *name, const char *detail)
+{
+       bool result = true;
+       if (type < firststruct && typelens_native[type] > 4 && (len % 8)) {
+               fprintf(stderr, "Align 8 error (%s) in struct: %s %s (add %d padding bytes)\n",
+                       detail, types[structtype], name, len % 8);
+               result = false;
+       }
+       if (typelens_native[type] > 3 && (len % 4) ) {
+               fprintf(stderr, "Align 4 error (%s) in struct: %s %s (add %d padding bytes)\n",
+                       detail, types[structtype], name, len % 4);
+               result = false;
+       }
+       if (typelens_native[type] == 2 && (len % 2) ) {
+               fprintf(stderr, "Align 2 error (%s) in struct: %s %s (add %d padding bytes)\n",
+                       detail, types[structtype], name, len % 2);
+               result = false;
+       }
+       return result;
+}
+
 static int calculate_structlens(int firststruct)
 {
        int unknown = nr_structs, lastunknown;
@@ -815,20 +837,11 @@ static int calculate_structlens(int firststruct)
                                                        }
                                                }
                                                
-                                               /* 2-4-8 aligned/ */
-                                               if (type < firststruct && typelens_native[type] > 4 && (len_native % 8)) {
-                                                       fprintf(stderr, "Align 8 error in struct: %s %s (add %d padding bytes)\n",
-                                                               types[structtype], cp, len_native % 8);
-                                                       dna_error = 1;
-                                               }
-                                               if (typelens_native[type] > 3 && (len_native % 4) ) {
-                                                       fprintf(stderr, "Align 4 error in struct: %s %s (add %d padding bytes)\n",
-                                                               types[structtype], cp, len_native % 4);
+                                               /* Check 2-4-8 aligned. */
+                                               if (!check_field_alignment(firststruct, structtype, type, len_32, cp, "32 bit")) {
                                                        dna_error = 1;
                                                }
-                                               else if (typelens_native[type] == 2 && (len_native % 2) ) {
-                                                       fprintf(stderr, "Align 2 error in struct: %s %s (add %d padding bytes)\n",
-                                                               types[structtype], cp, len_native % 2);
+                                               if (!check_field_alignment(firststruct, structtype, type, len_64, cp, "64 bit")) {
                                                        dna_error = 1;
                                                }