From 036d710776ba0065560f42f28d081047d552dd0e Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 3 Dec 2022 12:27:53 +0000 Subject: util/nvmutil: don't initialise rbuf unless needed previously, it was always initialised, but now it's only initialised if '?' is used on a mac address character in command `setmac` this is done by simply moving mac address character randomisation to a separate function --- util/nvmutil/nvmutil.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index bf06b4cd..b45e9064 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -36,6 +36,7 @@ ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size); void cmd_setmac(const char *strMac); uint8_t hextonum(char chs); +uint8_t rhex(void); void cmd_dump(void); void showmac(int partnum); void hexdump(int partnum); @@ -217,6 +218,26 @@ uint8_t hextonum(char chs) { uint8_t val8, ch; + ch = (uint8_t) chs; + + if ((ch >= '0') && ch <= '9') { + val8 = ch - '0'; + } else if ((ch >= 'A') && (ch <= 'F')) { + val8 = ch - 'A' + 10; + } else if ((ch >= 'a') && (ch <= 'f')) { + val8 = ch - 'a' + 10; + } else if (ch == '?') { + val8 = rhex(); + } else { + return 16; + } + + return val8; +} + +uint8_t +rhex(void) +{ static int rfd = -1; static uint8_t *rbuf = NULL; static size_t rindex = BUFSIZ; @@ -237,21 +258,7 @@ hextonum(char chs) } } - ch = (uint8_t) chs; - - if ((ch >= '0') && ch <= '9') { - val8 = ch - '0'; - } else if ((ch >= 'A') && (ch <= 'F')) { - val8 = ch - 'A' + 10; - } else if ((ch >= 'a') && (ch <= 'f')) { - val8 = ch - 'a' + 10; - } else if (ch == '?') { - val8 = rbuf[rindex++] & 0xf; - } else { - return 16; - } - - return val8; + return rbuf[rindex++] & 0xf; } void -- cgit v1.2.1