Commit 984cc7df
984cc7dfd23bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Fix ffi_read_u32/i32 byte offset being scaled by element size
The numeric ffi reader emitted the byte offset added to the typed pointer, e.g. (uint32_t )((char )p) + 8, so C pointer arithmetic scaled it to 8sizeof(uint32_t) = 32 bytes. Reads past offset 0 landed outside the buffer; the out-of-bounds read happened to be zero on Linux/macOS but returned garbage on Windows. Add the offset to the char (byte) pointer before the typed cast and deref.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 984cc7dfd23b
- Parent
- ddf824be6774
src/codegen_call.cmodifiedView file
@@ -2839,11 +2839,13 @@
28392839
buf_printf(b, " + %d)); _t%d ? sp_box_obj(_t%d, SP_BUILTIN_OBJECT) : sp_box_nil(); })", off, rt3, rt3);28402840
}28412841
else {2842-
buf_printf(b, "((mrb_int)(*(((%s *)((char *)(", ctype);2842+
/* `+ off` must apply to the char* (byte offset), not the typed2843+
pointer (which would scale it by sizeof(elem)). */2844+
buf_printf(b, "((mrb_int)(*(%s *)((char *)(", ctype);28432845
TyKind at = comp_ntype(c, argv[0]);28442846
if (at == TY_POLY) { emit_expr(c, argv[0], b); buf_puts(b, ").v.p"); }28452847
else emit_expr(c, argv[0], b);2846-
buf_printf(b, ") + %d))))", off);2848+
buf_printf(b, " + %d)))", off);28472849
}28482850
}28492851
return;