From 3e0b06cfd624f6747154fcb4ab3b53799f2c4d70 Mon Sep 17 00:00:00 2001 From: Shuo A Liu Date: Thu, 20 Dec 2018 11:00:31 +0800 Subject: [PATCH] dm: Fix some issues from string operations The patch fix some string operations issues and also improve readability of several snippet. Tracked-On: #2133 Signed-off-by: Shuo A Liu Reviewed-by: Yonghua Huang --- devicemodel/core/main.c | 10 +-- devicemodel/hw/pci/core.c | 89 ++++++++++-------------- devicemodel/hw/pci/gvt.c | 7 +- devicemodel/hw/pci/npk.c | 8 +-- devicemodel/hw/pci/virtio/virtio_coreu.c | 2 +- devicemodel/include/dm.h | 1 + devicemodel/lib/dm_string.c | 29 +++----- 7 files changed, 62 insertions(+), 84 deletions(-) diff --git a/devicemodel/core/main.c b/devicemodel/core/main.c index 77ff90cbc..40451be61 100644 --- a/devicemodel/core/main.c +++ b/devicemodel/core/main.c @@ -185,8 +185,10 @@ static int pincpu_parse(const char *opt) { int vcpu, pcpu; + char *cp; - if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) { + if (dm_strtoi(opt, &cp, 10, &vcpu) || *cp != ':' || + dm_strtoi(cp + 1, &cp, 10, &pcpu)) { fprintf(stderr, "invalid format: %s\n", opt); return -1; } @@ -804,7 +806,7 @@ dm_run(int argc, char *argv[]) } break; case 'c': - guest_ncpus = atoi(optarg); + dm_strtoi(optarg, NULL, 0, &guest_ncpus); break; case 'E': if (acrn_parse_elf(optarg) != 0) @@ -1060,10 +1062,10 @@ int main(int argc, char *argv[]) switch (c) { case CMD_OPT_VMCFG: vmcfg = 1; - index = atoi(optarg); + dm_strtoi(optarg, NULL, 0, &index); break; case CMD_OPT_DUMP: - index = atoi(optarg); + dm_strtoi(optarg, NULL, 0, &index); vmcfg_dump(index, long_options, optstr); return 0; default: diff --git a/devicemodel/hw/pci/core.c b/devicemodel/hw/pci/core.c index da470c24a..e5fbaa25a 100644 --- a/devicemodel/hw/pci/core.c +++ b/devicemodel/hw/pci/core.c @@ -35,6 +35,7 @@ #include #include +#include "dm.h" #include "vmmapi.h" #include "acpi.h" #include "inout.h" @@ -155,41 +156,34 @@ pci_parse_slot_usage(char *aopt) int parse_bdf(char *s, int *bus, int *dev, int *func, int base) { - int i; - int nums[3] = {-1, -1, -1}; - char *str; + char *s_bus, *s_dev, *s_func; + char *str, *cp; + int ret = 0; - if (bus) *bus = 0; - if (dev) *dev = 0; - if (func) *func = 0; - str = s; - for (i = 0, errno = 0; i < 3; i++) { - nums[i] = (int)strtol(str, &str, base); - if (errno == ERANGE || *str == '\0' || s == str) - break; - str++; + str = cp = strdup(s); + bus ? *bus = 0 : 0; + dev ? *dev = 0 : 0; + func ? *func = 0 : 0; + s_bus = s_dev = s_func = NULL; + s_dev = strsep(&cp, ":/."); + if (cp) { + s_func = strsep(&cp, ":/."); + if (cp) { + s_bus = s_dev; + s_dev = s_func; + s_func = strsep(&cp, ":/."); + } } - if (s == str || errno == ERANGE) - { - printf("%s: parse_bdf error!\n", __func__); - return -1; - } - switch (i) { - case 0: - if (dev) *dev = nums[0]; - break; - case 1: - if (dev) *dev = nums[0]; - if (func) *func = nums[1]; - break; - case 2: - if (bus) *bus = nums[0]; - if (dev) *dev = nums[1]; - if (func) *func = nums[2]; - break; - } - return 0; + if (s_dev && dev) + ret |= dm_strtoi(s_dev, &s_dev, base, dev); + if (s_func && func) + ret |= dm_strtoi(s_func, &s_func, base, func); + if (s_bus && bus) + ret |= dm_strtoi(s_bus, &s_bus, base, bus); + free(str); + + return ret; } int @@ -208,35 +202,22 @@ pci_parse_slot(char *opt) } emul = config = NULL; - cp = strchr(str, ','); - if (cp != NULL) { - *cp = '\0'; - emul = cp + 1; - cp = strchr(emul, ','); - if (cp != NULL) { - *cp = '\0'; - config = cp + 1; - if (*config == 'b') { - b = config; - cp = config + 1; - if (*cp == ',') { - *cp = '\0'; - config = cp + 1; - } else { - b = NULL; - } - } - } + cp = str; + str = strsep(&cp, ","); + if (cp) { + emul = strsep(&cp, ","); + /* for boot device */ + if (cp && *cp == 'b' && *(cp+1) == ',') + b = strsep(&cp, ","); + config = cp; } else { pci_parse_slot_usage(opt); goto done; } /* :: */ - if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0) { - fprintf(stderr, "pci bdf parse fail\n"); + if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0) snum = -1; - } if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS || fnum < 0 || fnum >= MAXFUNCS) { diff --git a/devicemodel/hw/pci/gvt.c b/devicemodel/hw/pci/gvt.c index 036cb5c87..f0d8eaffe 100644 --- a/devicemodel/hw/pci/gvt.c +++ b/devicemodel/hw/pci/gvt.c @@ -52,10 +52,11 @@ int guest_domid = 1; int acrn_parse_gvtargs(char *arg) { - if (parse_bdf(arg, &gvt_low_gm_sz, &gvt_high_gm_sz, - &gvt_fence_sz, 10) != 0) { + if (dm_strtoi(arg, &arg, 10, &gvt_low_gm_sz) != 0 || + dm_strtoi(arg, &arg, 10, &gvt_high_gm_sz) != 0 || + dm_strtoi(arg, &arg, 10, &gvt_fence_sz) != 0) return -1; - } + printf("passed gvt-g optargs low_gm %d, high_gm %d, fence %d\n", gvt_low_gm_sz, gvt_high_gm_sz, gvt_fence_sz); diff --git a/devicemodel/hw/pci/npk.c b/devicemodel/hw/pci/npk.c index 6a7ab6c0c..7dce94630 100644 --- a/devicemodel/hw/pci/npk.c +++ b/devicemodel/hw/pci/npk.c @@ -186,6 +186,7 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) uint8_t h_cfg[PCI_REGMAX + 1]; uint32_t m_off, m_num; struct npk_reg_default_val *d; + char *cp; if (npk_in_use) { WPRINTF(("NPK is already in use\n")); @@ -212,8 +213,8 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) */ /* get the master offset and the number for this guest */ - if ((opts == NULL) || (sscanf(opts, "%u/%u", &m_off, &m_num) != 2) - || !valid_param(m_off, m_num)) { + if ((opts == NULL) || dm_strtoui(opts, &cp, 10, &m_off) || *cp != '/' || + dm_strtoui(cp + 1, &cp, 10, &m_num) || !valid_param(m_off, m_num)) { m_off = 256; m_num = 256; } @@ -228,8 +229,7 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) /* traverse the driver folder, and try to find the NPK BDF# */ while ((dent = readdir(dir)) != NULL) { if (strncmp(dent->d_name, "0000:", 5) != 0 || - parse_bdf((dent->d_name + 5), - &b, &s, &f, 10) != 0) + parse_bdf((dent->d_name + 5), &b, &s, &f, 16) != 0) continue; else break; diff --git a/devicemodel/hw/pci/virtio/virtio_coreu.c b/devicemodel/hw/pci/virtio/virtio_coreu.c index 05ebf1c57..1c2353fca 100644 --- a/devicemodel/hw/pci/virtio/virtio_coreu.c +++ b/devicemodel/hw/pci/virtio/virtio_coreu.c @@ -208,7 +208,7 @@ connect_coreu_daemon() memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, COREU_SERVICE_NAME, sizeof(&COREU_SERVICE_NAME)); + strncpy(addr.sun_path, COREU_SERVICE_NAME, sizeof(addr.sun_path)); ret = connect(fd, &addr, sizeof(struct sockaddr_un)); if (ret < 0) { diff --git a/devicemodel/include/dm.h b/devicemodel/include/dm.h index 0a949b737..c2c08c538 100644 --- a/devicemodel/include/dm.h +++ b/devicemodel/include/dm.h @@ -32,6 +32,7 @@ #include #include "types.h" #include "vmm.h" +#include "dm_string.h" struct vmctx; extern int guest_ncpus; diff --git a/devicemodel/lib/dm_string.c b/devicemodel/lib/dm_string.c index 4402de5ef..8a682374d 100644 --- a/devicemodel/lib/dm_string.c +++ b/devicemodel/lib/dm_string.c @@ -8,23 +8,19 @@ #include #include #include +#include #include "dm_string.h" int dm_strtol(const char *s, char **end, unsigned int base, long *val) { if (!s) - goto err; + return -1; *val = strtol(s, end, base); - if (*end == s) { - printf("ERROR! nothing covert for: %s!\n", s); - goto err; - } + if ((end && *end == s) || errno == ERANGE) + return -1; return 0; - -err: - return -1; } int @@ -35,7 +31,8 @@ dm_strtoi(const char *s, char **end, unsigned int base, int *val) l_val = 0; ret = dm_strtol(s, end, base, &l_val); - *val = (int)l_val; + if (ret == 0) + *val = (int)l_val; return ret; } @@ -43,17 +40,12 @@ int dm_strtoul(const char *s, char **end, unsigned int base, unsigned long *val) { if (!s) - goto err; + return -1; *val = strtoul(s, end, base); - if (*end == s) { - printf("ERROR! nothing covert for: %s!\n", s); - goto err; - } + if ((end && *end == s) || errno == ERANGE) + return -1; return 0; - -err: - return -1; } int @@ -64,6 +56,7 @@ dm_strtoui(const char *s, char **end, unsigned int base, unsigned int *val) l_val = 0; ret = dm_strtoul(s, end, base, &l_val); - *val = (unsigned int)l_val; + if (ret == 0) + *val = (unsigned int)l_val; return ret; }