util/nvmutil: don't reallocate memory in hextonum

fsdg20230625
Leah Rowe 2022-12-03 12:11:15 +00:00
parent e5a46b464d
commit 0bf3f1ed61
1 changed files with 12 additions and 10 deletions

View File

@ -217,24 +217,26 @@ uint8_t
hextonum(char chs) hextonum(char chs)
{ {
uint8_t val8, ch; uint8_t val8, ch;
static int macfd; static int macfd = -1;
static uint8_t *rbuf = NULL; static uint8_t *rbuf = NULL;
static size_t rindex; static size_t rindex = BUFSIZ;
if (rindex == BUFSIZ) {
close(macfd); if ((rindex == BUFSIZ)) {
free(rbuf);
rbuf = NULL;
}
if (rbuf == NULL) {
rindex = 0; rindex = 0;
if (rbuf == NULL)
if ((rbuf = (uint8_t *) malloc(BUFSIZ)) == NULL) if ((rbuf = (uint8_t *) malloc(BUFSIZ)) == NULL)
err(1, NULL); err(1, NULL);
if ((macfd != -1)) {
close(macfd);
macfd = -1;
}
if (readFromFile(&macfd, rbuf, "/dev/urandom", O_RDONLY, if (readFromFile(&macfd, rbuf, "/dev/urandom", O_RDONLY,
BUFSIZ) != BUFSIZ) { BUFSIZ) != BUFSIZ) {
warn("%s", "/dev/urandom"); warn("%s", "/dev/urandom");
return 16; return 16;
} }
} }
ch = (uint8_t) chs; ch = (uint8_t) chs;
if ((ch >= '0') && ch <= '9') { if ((ch >= '0') && ch <= '9') {