Skip to content

Commit ecca53b

Browse files
committed
py: binary.c: Properly implement alignment for native unpacked structs.
1 parent 2831a8f commit ecca53b

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

py/binary.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdint.h>
2828
#include <stdlib.h>
29+
#include <stddef.h>
2930
#include <string.h>
3031
#include <assert.h>
3132

@@ -37,6 +38,10 @@
3738

3839
// Helpers to work with binary-encoded data
3940

41+
#ifndef alignof
42+
#define alignof(type) offsetof(struct { char c; type t; }, t)
43+
#endif
44+
4045
int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
4146
int size = 0;
4247
int align = 1;
@@ -68,16 +73,20 @@ int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
6873
case 'b': case 'B':
6974
align = size = 1; break;
7075
case 'h': case 'H':
71-
align = size = sizeof(short); break;
76+
align = alignof(short);
77+
size = sizeof(short); break;
7278
case 'i': case 'I':
73-
align = size = sizeof(int); break;
79+
align = alignof(int);
80+
size = sizeof(int); break;
7481
case 'l': case 'L':
75-
align = size = sizeof(long); break;
82+
align = alignof(long);
83+
size = sizeof(long); break;
7684
case 'q': case 'Q':
77-
// TODO: This is for x86
78-
align = sizeof(int); size = sizeof(long long); break;
85+
align = alignof(long long);
86+
size = sizeof(long long); break;
7987
case 'P': case 'O': case 'S':
80-
align = size = sizeof(void*); break;
88+
align = alignof(void*);
89+
size = sizeof(void*); break;
8190
}
8291
}
8392
}

0 commit comments

Comments
 (0)