summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-05-19 07:20:14 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-05-19 07:20:14 -0700
commit417e7672bcb9a9cd0dd13d10bc6df07743615a63 (patch)
tree9c24447d574b5fcf1a1b73bbec850033310b9b24
parentc8d0b0cd48275e0e0182c0a3f6af1e020f5c7c29 (diff)
downloadtxr-417e7672bcb9a9cd0dd13d10bc6df07743615a63.tar.gz
txr-417e7672bcb9a9cd0dd13d10bc6df07743615a63.tar.bz2
txr-417e7672bcb9a9cd0dd13d10bc6df07743615a63.zip
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.
-rw-r--r--ffi.c12
1 files 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;