@@ -79,6 +79,9 @@ struct dasm_State {
79
79
/* The size of the core structure depends on the max. number of sections. */
80
80
#define DASM_PSZ (ms ) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
81
81
82
+ /* Perform potentially overflowing pointer operations in a way that avoids UB. */
83
+ #define DASM_PTR_SUB (p1 , off ) ((void *) ((uintptr_t) (p1) - sizeof(*p1) * (uintptr_t) (off)))
84
+ #define DASM_PTR_ADD (p1 , off ) ((void *) ((uintptr_t) (p1) + sizeof(*p1) * (uintptr_t) (off)))
82
85
83
86
/* Initialize DynASM state. */
84
87
void dasm_init (Dst_DECL , int maxsection )
@@ -98,7 +101,7 @@ void dasm_init(Dst_DECL, int maxsection)
98
101
D -> maxsection = maxsection ;
99
102
for (i = 0 ; i < maxsection ; i ++ ) {
100
103
D -> sections [i ].buf = NULL ; /* Need this for pass3. */
101
- D -> sections [i ].rbuf = D -> sections [i ].buf - DASM_SEC2POS (i );
104
+ D -> sections [i ].rbuf = DASM_PTR_SUB ( D -> sections [i ].buf , DASM_SEC2POS (i ) );
102
105
D -> sections [i ].bsize = 0 ;
103
106
D -> sections [i ].epos = 0 ; /* Wrong, but is recalculated after resize. */
104
107
}
@@ -377,7 +380,7 @@ int dasm_encode(Dst_DECL, void *buffer)
377
380
for (secnum = 0 ; secnum < D -> maxsection ; secnum ++ ) {
378
381
dasm_Section * sec = D -> sections + secnum ;
379
382
int * b = sec -> buf ;
380
- int * endb = sec -> rbuf + sec -> pos ;
383
+ int * endb = DASM_PTR_ADD ( sec -> rbuf , sec -> pos ) ;
381
384
382
385
while (b != endb ) {
383
386
dasm_ActList p = D -> actionlist + * b ++ ;
0 commit comments