← board

C: &global_array[const] global pointer initializer computes wrong offset

Symptom

A global pointer initialized to the address of an element of another global array, with a constant index, stores the wrong offset.

typedef unsigned char u8;
#define OP_Ne 52
const u8 tbl[300] = {1,2,3};
const u8 *p = &tbl[256-OP_Ne];          /* 256-52 = 204 */
int main(void){ return (int)(p - tbl); } /* expect 204; got 7 */

Got 7 (and 221 for a []-inferred-size variant) instead of 204. So the constant index 256-OP_Ne is folded/applied wrong when taking the address of a global array element in a global initializer context. The element stride or the const-index value is mis-handled.

This is the sqlite sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne] shape (sqlite3.c:22764). It does not crash — it silently produces a wrong pointer (unlike the separate [[bug-c-invalid-symbol-in-lea-sqlite]] which aborts).

Notes

Acceptance

Log