diff options
author | Leah Rowe <leah@libreboot.org> | 2022-12-03 12:11:15 +0000 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2022-12-03 12:11:15 +0000 |
commit | 0bf3f1ed61bb04ede8f158505b480272a40bd82e (patch) | |
tree | 863ce50897b317420c7a2462253208f778aa3546 /util/nvmutil/nvmutil.c | |
parent | e5a46b464d10f746c8caf68c2e245c7e78b92b4c (diff) |
util/nvmutil: don't reallocate memory in hextonum
Diffstat (limited to 'util/nvmutil/nvmutil.c')
-rw-r--r-- | util/nvmutil/nvmutil.c | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index b5f4e864..fa92717a 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -217,24 +217,26 @@ uint8_t hextonum(char chs) { uint8_t val8, ch; - static int macfd; + static int macfd = -1; static uint8_t *rbuf = NULL; - static size_t rindex; - if (rindex == BUFSIZ) { - close(macfd); - free(rbuf); - rbuf = NULL; - } - if (rbuf == NULL) { + static size_t rindex = BUFSIZ; + + if ((rindex == BUFSIZ)) { rindex = 0; - if ((rbuf = (uint8_t *) malloc(BUFSIZ)) == NULL) - err(1, NULL); + if (rbuf == NULL) + if ((rbuf = (uint8_t *) malloc(BUFSIZ)) == NULL) + err(1, NULL); + if ((macfd != -1)) { + close(macfd); + macfd = -1; + } if (readFromFile(&macfd, rbuf, "/dev/urandom", O_RDONLY, BUFSIZ) != BUFSIZ) { warn("%s", "/dev/urandom"); return 16; } } + ch = (uint8_t) chs; if ((ch >= '0') && ch <= '9') { |