FFI: Limit index range for complex numbers.

This commit is contained in:
Mike Pall 2011-01-29 19:47:37 +01:00
parent 6fd721ce72
commit 992bc2caa3
2 changed files with 8 additions and 3 deletions

View File

@ -94,10 +94,12 @@ collect_attrib:
if (ctype_ispointer(ct->info)) { if (ctype_ispointer(ct->info)) {
CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */ CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */
if (sz != CTSIZE_INVALID) { if (sz != CTSIZE_INVALID) {
if (ctype_isptr(ct->info)) if (ctype_isptr(ct->info)) {
p = (uint8_t *)cdata_getptr(p, ct->size); p = (uint8_t *)cdata_getptr(p, ct->size);
else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) } else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) {
if ((ct->info & CTF_COMPLEX)) idx &= 1;
*qual |= CTF_CONST; /* Valarray elements are constant. */ *qual |= CTF_CONST; /* Valarray elements are constant. */
}
*pp = p + idx*(int32_t)sz; *pp = p + idx*(int32_t)sz;
return ct; return ct;
} }

View File

@ -483,7 +483,10 @@ void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
#endif #endif
integer_key: integer_key:
if (ctype_ispointer(ct->info)) { if (ctype_ispointer(ct->info)) {
CTSize sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info))); CTSize sz;
if ((ct->info & CTF_COMPLEX))
idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1));
sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info)));
idx = crec_reassoc_ofs(J, idx, &ofs, sz); idx = crec_reassoc_ofs(J, idx, &ofs, sz);
idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz)); idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));
ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr); ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);