From 417e7672bcb9a9cd0dd13d10bc6df07743615a63 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 19 May 2022 07:20:14 -0700 Subject: ffi: diagnose bitfields wider than type. * ffi.c (ffi_type_compile): Show the original bitfield syntax in the invalid size diagnostics. In the case of the (bit n type) syntax, restrict the maximum bits to the width of the type, not just to 32 or 64. I realize the 8 * tft->size calculation can overflow for huge array types, but it makes no sense to use them as bitfields. --- ffi.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ffi.c b/ffi.c index 26bd52d2..fcfea96e 100644 --- a/ffi.c +++ b/ffi.c @@ -4181,9 +4181,9 @@ val ffi_type_compile(val syntax) if (cddr(syntax)) goto excess; if (nb < 0 || nb > bits_int) - uw_throwf(error_s, lit("~a: invalid bitfield size ~s; " + uw_throwf(error_s, lit("~a: invalid bitfield size ~s in ~s: " "must be 0 to ~s"), - self, nbits, num_fast(bits_int), nao); + self, nbits, syntax, num_fast(bits_int), nao); tft->nelem = c_num(nbits, self); tft->bitfield = 1; if (nb == bits_int) @@ -4203,9 +4203,9 @@ val ffi_type_compile(val syntax) const int bits_int = 8 * sizeof(int); #if HAVE_I64 const int bits_llint = 8 * sizeof(u64_t); - const int bits_lim = bits_llint; + const int bits_lim = min(8 * tft->size, bits_llint); #else - const int bits_lim = bits_int; + const int bits_lim = min(8 * tft->size, bits_int); #endif val type_copy = ffi_type_copy(type); struct txr_ffi_type *tft_cp = ffi_type_struct(type_copy); @@ -4229,9 +4229,9 @@ val ffi_type_compile(val syntax) } if (nb < 0 || nb > bits_lim) - uw_throwf(error_s, lit("~a: invalid bitfield size ~s; " + uw_throwf(error_s, lit("~a: bitfield size ~s in ~s: " "must be 0 to ~s"), - self, nbits, num_fast(bits_lim), nao); + self, nbits, syntax, num_fast(bits_lim), nao); tft_cp->syntax = xsyntax; tft_cp->nelem = nb; -- cgit v1.2.3