util/nvmutil: cast inside check_bound, not callers

the purpose of the cast is to check whether a given
integer would underflow under any circumstance.

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-06 13:33:24 +00:00
parent a31236b1f8
commit f96a119523
+8 -6
View File
@@ -44,7 +44,7 @@ static int good_checksum(int);
static uint16_t word(size_t, int); static uint16_t word(size_t, int);
static void set_word(size_t, int, uint16_t); static void set_word(size_t, int, uint16_t);
static size_t get_word_pos(size_t, int); static size_t get_word_pos(size_t, int);
static void check_bound(ssize_t, int); static void check_bound(size_t, int);
static void write_gbe(void); static void write_gbe(void);
static void write_gbe_part(int); static void write_gbe_part(int);
static void usage(void); static void usage(void);
@@ -619,7 +619,7 @@ word(size_t pos16, int p)
{ {
size_t pos; size_t pos;
check_bound((ssize_t)pos16, p); check_bound(pos16, p);
pos = get_word_pos(pos16, p); pos = get_word_pos(pos16, p);
return (uint16_t)buf[pos] | ((uint16_t)buf[pos + 1] << 8); return (uint16_t)buf[pos] | ((uint16_t)buf[pos + 1] << 8);
@@ -630,7 +630,7 @@ set_word(size_t pos16, int p, uint16_t val16)
{ {
size_t pos; size_t pos;
check_bound((ssize_t)pos16, p); check_bound(pos16, p);
pos = get_word_pos(pos16, p); pos = get_word_pos(pos16, p);
buf[pos] = (uint8_t)(val16 & 0xff); buf[pos] = (uint8_t)(val16 & 0xff);
@@ -649,8 +649,10 @@ get_word_pos(size_t pos16, int p)
} }
static void static void
check_bound(ssize_t c, int p) check_bound(size_t c, int p)
{ {
ssize_t a = (ssize_t) c;
/* /*
* NVM_SIZE assumed as the limit, because the * NVM_SIZE assumed as the limit, because the
* current design assumes that we will only * current design assumes that we will only
@@ -667,8 +669,8 @@ check_bound(ssize_t c, int p)
if ((p != 0) && (p != 1)) if ((p != 0) && (p != 1))
err(EINVAL, "check_bound: invalid partnum %d", p); err(EINVAL, "check_bound: invalid partnum %d", p);
if ((c < 0) || (c >= (NVM_SIZE >> 1))) if ((a < 0) || (a >= (NVM_SIZE >> 1)))
err(EINVAL, "check_bound: out of bounds %zd", c); err(EINVAL, "check_bound: out of bounds %zd", a);
} }
static void static void