util/nvmutil: much safer rhex()

n could be zero under weird regression cases

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-09 17:00:58 +00:00
parent f34d020a20
commit 61015dbc6c
+5 -1
View File
@@ -930,8 +930,9 @@ rhex(void)
{ {
static size_t n = 0; static size_t n = 0;
static uint8_t rnum[12]; static uint8_t rnum[12];
int max_retries;
if (!n) { for (max_retries = 0; max_retries < 50 && !n; max_retries++) {
n = sizeof(rnum); n = sizeof(rnum);
#ifdef NVMUTIL_ARC4RANDOM_BUF #ifdef NVMUTIL_ARC4RANDOM_BUF
arc4random_buf(rnum, n); arc4random_buf(rnum, n);
@@ -941,6 +942,9 @@ rhex(void)
#endif #endif
} }
if (!n)
err(ECANCELED, "Randomisation failure");
return (uint16_t)(rnum[--n] & 0xf); return (uint16_t)(rnum[--n] & 0xf);
} }