diff --git a/src/core/ffi.c b/src/core/ffi.c index 7cc1ae2f..c95642ad 100644 --- a/src/core/ffi.c +++ b/src/core/ffi.c @@ -400,7 +400,7 @@ static JanetFFIStruct *build_struct_type(int32_t argc, const Janet *argv) { JanetFFIStruct *st = janet_abstract(&janet_struct_type, sizeof(JanetFFIStruct) + argc * sizeof(JanetFFIStructMember)); - st->field_count = member_count; + st->field_count = 0; st->size = 0; st->align = 1; if (argc == 0) { @@ -418,13 +418,13 @@ static JanetFFIStruct *build_struct_type(int32_t argc, const Janet *argv) { st->fields[i].type = decode_ffi_type(argv[j]); size_t el_size = type_size(st->fields[i].type); size_t el_align = type_align(st->fields[i].type); + if (el_align <= 0) janet_panicf("bad field type %V", argv[j]); if (all_packed || pack_one) { if (st->size % el_align != 0) is_aligned = 0; st->fields[i].offset = st->size; st->size += (uint32_t) el_size; } else { if (el_align > st->align) st->align = (uint32_t) el_align; - if (el_align <= 0) el_align = 1; st->fields[i].offset = (uint32_t)(((st->size + el_align - 1) / el_align) * el_align); st->size = (uint32_t)(el_size + st->fields[i].offset); } @@ -434,6 +434,7 @@ static JanetFFIStruct *build_struct_type(int32_t argc, const Janet *argv) { st->size += (st->align - 1); st->size /= st->align; st->size *= st->align; + st->field_count = member_count; return st; } diff --git a/test/suite-ffi.janet b/test/suite-ffi.janet index fb8d288c..6305651e 100644 --- a/test/suite-ffi.janet +++ b/test/suite-ffi.janet @@ -53,9 +53,6 @@ "array struct size")) (compwhen has-ffi - (assert-no-error "bad struct issue #1512" (ffi/struct :void)) - (def s (ffi/struct :void)) - (assert (= 0 (ffi/size s))) - (assert (= 1 (ffi/align s)))) + (assert-error "bad struct issue #1512" (ffi/struct :void))) (end-suite)