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 <shuo.a.liu@intel.com>
Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
This commit is contained in:
Shuo A Liu 2018-12-20 11:00:31 +08:00 committed by wenlingz
parent 20d0e666ff
commit 3e0b06cfd6
7 changed files with 62 additions and 84 deletions

View File

@ -185,8 +185,10 @@ static int
pincpu_parse(const char *opt) pincpu_parse(const char *opt)
{ {
int vcpu, pcpu; 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); fprintf(stderr, "invalid format: %s\n", opt);
return -1; return -1;
} }
@ -804,7 +806,7 @@ dm_run(int argc, char *argv[])
} }
break; break;
case 'c': case 'c':
guest_ncpus = atoi(optarg); dm_strtoi(optarg, NULL, 0, &guest_ncpus);
break; break;
case 'E': case 'E':
if (acrn_parse_elf(optarg) != 0) if (acrn_parse_elf(optarg) != 0)
@ -1060,10 +1062,10 @@ int main(int argc, char *argv[])
switch (c) { switch (c) {
case CMD_OPT_VMCFG: case CMD_OPT_VMCFG:
vmcfg = 1; vmcfg = 1;
index = atoi(optarg); dm_strtoi(optarg, NULL, 0, &index);
break; break;
case CMD_OPT_DUMP: case CMD_OPT_DUMP:
index = atoi(optarg); dm_strtoi(optarg, NULL, 0, &index);
vmcfg_dump(index, long_options, optstr); vmcfg_dump(index, long_options, optstr);
return 0; return 0;
default: default:

View File

@ -35,6 +35,7 @@
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include "dm.h"
#include "vmmapi.h" #include "vmmapi.h"
#include "acpi.h" #include "acpi.h"
#include "inout.h" #include "inout.h"
@ -155,41 +156,34 @@ pci_parse_slot_usage(char *aopt)
int int
parse_bdf(char *s, int *bus, int *dev, int *func, int base) parse_bdf(char *s, int *bus, int *dev, int *func, int base)
{ {
int i; char *s_bus, *s_dev, *s_func;
int nums[3] = {-1, -1, -1}; char *str, *cp;
char *str; int ret = 0;
if (bus) *bus = 0; str = cp = strdup(s);
if (dev) *dev = 0; bus ? *bus = 0 : 0;
if (func) *func = 0; dev ? *dev = 0 : 0;
str = s; func ? *func = 0 : 0;
for (i = 0, errno = 0; i < 3; i++) { s_bus = s_dev = s_func = NULL;
nums[i] = (int)strtol(str, &str, base); s_dev = strsep(&cp, ":/.");
if (errno == ERANGE || *str == '\0' || s == str) if (cp) {
break; s_func = strsep(&cp, ":/.");
str++; if (cp) {
s_bus = s_dev;
s_dev = s_func;
s_func = strsep(&cp, ":/.");
}
} }
if (s == str || errno == ERANGE) if (s_dev && dev)
{ ret |= dm_strtoi(s_dev, &s_dev, base, dev);
printf("%s: parse_bdf error!\n", __func__); if (s_func && func)
return -1; ret |= dm_strtoi(s_func, &s_func, base, func);
} if (s_bus && bus)
switch (i) { ret |= dm_strtoi(s_bus, &s_bus, base, bus);
case 0: free(str);
if (dev) *dev = nums[0];
break; return ret;
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;
} }
int int
@ -208,35 +202,22 @@ pci_parse_slot(char *opt)
} }
emul = config = NULL; emul = config = NULL;
cp = strchr(str, ','); cp = str;
if (cp != NULL) { str = strsep(&cp, ",");
*cp = '\0'; if (cp) {
emul = cp + 1; emul = strsep(&cp, ",");
cp = strchr(emul, ','); /* for boot device */
if (cp != NULL) { if (cp && *cp == 'b' && *(cp+1) == ',')
*cp = '\0'; b = strsep(&cp, ",");
config = cp + 1; config = cp;
if (*config == 'b') {
b = config;
cp = config + 1;
if (*cp == ',') {
*cp = '\0';
config = cp + 1;
} else {
b = NULL;
}
}
}
} else { } else {
pci_parse_slot_usage(opt); pci_parse_slot_usage(opt);
goto done; goto done;
} }
/* <bus>:<slot>:<func> */ /* <bus>:<slot>:<func> */
if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0) { if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0)
fprintf(stderr, "pci bdf parse fail\n");
snum = -1; snum = -1;
}
if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS || if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
fnum < 0 || fnum >= MAXFUNCS) { fnum < 0 || fnum >= MAXFUNCS) {

View File

@ -52,10 +52,11 @@ int guest_domid = 1;
int int
acrn_parse_gvtargs(char *arg) acrn_parse_gvtargs(char *arg)
{ {
if (parse_bdf(arg, &gvt_low_gm_sz, &gvt_high_gm_sz, if (dm_strtoi(arg, &arg, 10, &gvt_low_gm_sz) != 0 ||
&gvt_fence_sz, 10) != 0) { dm_strtoi(arg, &arg, 10, &gvt_high_gm_sz) != 0 ||
dm_strtoi(arg, &arg, 10, &gvt_fence_sz) != 0)
return -1; return -1;
}
printf("passed gvt-g optargs low_gm %d, high_gm %d, fence %d\n", 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); gvt_low_gm_sz, gvt_high_gm_sz, gvt_fence_sz);

View File

@ -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]; uint8_t h_cfg[PCI_REGMAX + 1];
uint32_t m_off, m_num; uint32_t m_off, m_num;
struct npk_reg_default_val *d; struct npk_reg_default_val *d;
char *cp;
if (npk_in_use) { if (npk_in_use) {
WPRINTF(("NPK is already in use\n")); 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 */ /* get the master offset and the number for this guest */
if ((opts == NULL) || (sscanf(opts, "%u/%u", &m_off, &m_num) != 2) if ((opts == NULL) || dm_strtoui(opts, &cp, 10, &m_off) || *cp != '/' ||
|| !valid_param(m_off, m_num)) { dm_strtoui(cp + 1, &cp, 10, &m_num) || !valid_param(m_off, m_num)) {
m_off = 256; m_off = 256;
m_num = 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# */ /* traverse the driver folder, and try to find the NPK BDF# */
while ((dent = readdir(dir)) != NULL) { while ((dent = readdir(dir)) != NULL) {
if (strncmp(dent->d_name, "0000:", 5) != 0 || if (strncmp(dent->d_name, "0000:", 5) != 0 ||
parse_bdf((dent->d_name + 5), parse_bdf((dent->d_name + 5), &b, &s, &f, 16) != 0)
&b, &s, &f, 10) != 0)
continue; continue;
else else
break; break;

View File

@ -208,7 +208,7 @@ connect_coreu_daemon()
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX; 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)); ret = connect(fd, &addr, sizeof(struct sockaddr_un));
if (ret < 0) { if (ret < 0) {

View File

@ -32,6 +32,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "types.h" #include "types.h"
#include "vmm.h" #include "vmm.h"
#include "dm_string.h"
struct vmctx; struct vmctx;
extern int guest_ncpus; extern int guest_ncpus;

View File

@ -8,23 +8,19 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include "dm_string.h" #include "dm_string.h"
int int
dm_strtol(const char *s, char **end, unsigned int base, long *val) dm_strtol(const char *s, char **end, unsigned int base, long *val)
{ {
if (!s) if (!s)
goto err; return -1;
*val = strtol(s, end, base); *val = strtol(s, end, base);
if (*end == s) { if ((end && *end == s) || errno == ERANGE)
printf("ERROR! nothing covert for: %s!\n", s); return -1;
goto err;
}
return 0; return 0;
err:
return -1;
} }
int int
@ -35,7 +31,8 @@ dm_strtoi(const char *s, char **end, unsigned int base, int *val)
l_val = 0; l_val = 0;
ret = dm_strtol(s, end, base, &l_val); ret = dm_strtol(s, end, base, &l_val);
*val = (int)l_val; if (ret == 0)
*val = (int)l_val;
return ret; return ret;
} }
@ -43,17 +40,12 @@ int
dm_strtoul(const char *s, char **end, unsigned int base, unsigned long *val) dm_strtoul(const char *s, char **end, unsigned int base, unsigned long *val)
{ {
if (!s) if (!s)
goto err; return -1;
*val = strtoul(s, end, base); *val = strtoul(s, end, base);
if (*end == s) { if ((end && *end == s) || errno == ERANGE)
printf("ERROR! nothing covert for: %s!\n", s); return -1;
goto err;
}
return 0; return 0;
err:
return -1;
} }
int int
@ -64,6 +56,7 @@ dm_strtoui(const char *s, char **end, unsigned int base, unsigned int *val)
l_val = 0; l_val = 0;
ret = dm_strtoul(s, end, base, &l_val); ret = dm_strtoul(s, end, base, &l_val);
*val = (unsigned int)l_val; if (ret == 0)
*val = (unsigned int)l_val;
return ret; return ret;
} }