@@ -79,6 +79,9 @@ struct dasm_State {
7979/* The size of the core structure depends on the max. number of sections. */
8080#define DASM_PSZ (ms ) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
8181
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)))
8285
8386/* Initialize DynASM state. */
8487void dasm_init (Dst_DECL , int maxsection )
@@ -98,7 +101,7 @@ void dasm_init(Dst_DECL, int maxsection)
98101 D -> maxsection = maxsection ;
99102 for (i = 0 ; i < maxsection ; i ++ ) {
100103 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 ) );
102105 D -> sections [i ].bsize = 0 ;
103106 D -> sections [i ].epos = 0 ; /* Wrong, but is recalculated after resize. */
104107 }
@@ -377,7 +380,7 @@ int dasm_encode(Dst_DECL, void *buffer)
377380 for (secnum = 0 ; secnum < D -> maxsection ; secnum ++ ) {
378381 dasm_Section * sec = D -> sections + secnum ;
379382 int * b = sec -> buf ;
380- int * endb = sec -> rbuf + sec -> pos ;
383+ int * endb = DASM_PTR_ADD ( sec -> rbuf , sec -> pos ) ;
381384
382385 while (b != endb ) {
383386 dasm_ActList p = D -> actionlist + * b ++ ;
0 commit comments