From 9c24aa3036ed82bbc0e9182e1722a5a8dd87a642 Mon Sep 17 00:00:00 2001 From: "Jason M. Bills" Date: Mon, 14 Oct 2019 14:21:57 -0700 Subject: Handle the address and size parameters better Instead of atoi() which has no error handling, switch to strtoul() so failures will return 0 and check the acceptable size and address ranges. Tested: peci_cmds ping -a 0x30 Succeeded peci_cmds ping -a 0x20 ERROR: Invalid address "0x20" peci_cmds RdPCIConfigLocal 0 0 0 -s 1 cc:0x40 0x86 peci_cmds RdPCIConfigLocal 0 0 0 -s 3 ERROR: Invalid size "3" Change-Id: Id42e170844a803122669455db6373201e0624a9c Signed-off-by: Jason M. Bills --- libpeci/peci_cmds.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/libpeci/peci_cmds.c b/libpeci/peci_cmds.c index 08b31cd..dac0fa6 100644 --- a/libpeci/peci_cmds.c +++ b/libpeci/peci_cmds.c @@ -35,9 +35,11 @@ void Usage(char* progname) printf("\t%-6s%s\n", "-v", "Display additional information about the command"); printf("\t%-6s%s\n", "-a", - "Address of the target in decimal. Default is 48"); + "Address of the target. Accepted values are 48-55 (0x30-0x37). " + "Default is 48 (0x30)"); printf("\t%-6s%s\n", "-s", - "Size of data to read or write in bytes. Default is 4"); + "Size of data to read or write in bytes. Accepted values are 1, 2, " + "4, 8, and 16. Default is 4"); printf("Commands:\n"); printf("\t%-28s%s\n", "Ping", "Ping the target"); printf("\t%-28s%s\n", "GetTemp", "Get the temperature"); @@ -114,16 +116,28 @@ int main(int argc, char* argv[]) case 'a': if (optarg != NULL) - address = (unsigned char)atoi(optarg); + address = strtoul(optarg, NULL, 0); + if (address < MIN_CLIENT_ADDR || address > MAX_CLIENT_ADDR) + { + printf("ERROR: Invalid address \"0x%x\"\n", address); + goto ErrorExit; + } + break; case 's': if (optarg != NULL) - u8Size = (unsigned char)atoi(optarg); + u8Size = strtoul(optarg, NULL, 0); + if (u8Size != 1 && u8Size != 2 && u8Size != 4 && u8Size != 8 && + u8Size != 16) + { + printf("ERROR: Invalid size \"%d\"\n", u8Size); + goto ErrorExit; + } break; default: - printf("ERROR: Unrecognized option \"-%c\".\n", optopt); + printf("ERROR: Unrecognized option \"-%c\"\n", optopt); goto ErrorExit; break; } @@ -149,7 +163,7 @@ int main(int argc, char* argv[]) // if (verbose) { - printf("PECI target[%u]: ", address); + printf("PECI target[0x%x]: ", address); } if (strcmp(cmd, "ping") == 0) { -- cgit v1.2.3