From fbaecde6bb325cdfaaa2fed99fe177f26eb7d505 Mon Sep 17 00:00:00 2001 From: Yang Liang Date: Tue, 11 Dec 2018 17:16:58 +0800 Subject: [PATCH] DM USB: xHCI: Fix banned API issue. In USB mediator, sscanf, strtok and atoi API is banned, so replace them with permitted API function. Tracked-On: #1254 Signed-off-by: Yang Liang Reviewed-by: Xiaoguang Wu Reviewed-by: Shuo Liu Reviewed-by: Yonghua Huang Acked-by: Yu Wang --- devicemodel/hw/pci/xhci.c | 21 ++++++++++++++------- devicemodel/hw/usb_core.c | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/devicemodel/hw/pci/xhci.c b/devicemodel/hw/pci/xhci.c index 1e82bb7f7..ddba94f48 100644 --- a/devicemodel/hw/pci/xhci.c +++ b/devicemodel/hw/pci/xhci.c @@ -92,6 +92,7 @@ #include "xhci.h" #include "usb_pmapper.h" #include "vmmapi.h" +#include "dm_string.h" #undef LOG_TAG #define LOG_TAG "xHCI: " @@ -3704,18 +3705,24 @@ errout: static int pci_xhci_parse_bus_port(struct pci_xhci_vdev *xdev, char *opts) { - int rc = 0, cnt; - uint32_t port, bus, index; + int rc = 0; + char *tstr; + int port, bus, index; struct usb_devpath path; struct usb_native_devinfo di; assert(xdev); assert(opts); + tstr = opts; /* 'bus-port' format */ - cnt = sscanf(opts, "%u-%u", &bus, &port); - if (cnt == EOF || cnt < 2 || bus >= USB_NATIVE_NUM_BUS || - port >= USB_NATIVE_NUM_PORT) { + if (!tstr || dm_strtoi(tstr, &tstr, 10, &bus) || *tstr != '-' || + dm_strtoi(tstr + 1, &tstr, 10, &port)) { + rc = -1; + goto errout; + } + + if (bus >= USB_NATIVE_NUM_BUS || port >= USB_NATIVE_NUM_PORT) { rc = -1; goto errout; } @@ -3873,7 +3880,7 @@ errout: static int pci_xhci_parse_opts(struct pci_xhci_vdev *xdev, char *opts) { - char *s, *t, *n; + char *s, *t, *n, *tptr; int i, rc = 0; struct pci_xhci_option_elem *elem; int (*f)(struct pci_xhci_vdev *, char *); @@ -3900,7 +3907,7 @@ pci_xhci_parse_opts(struct pci_xhci_vdev *xdev, char *opts) elem = xhci_option_table; elem_cnt = sizeof(xhci_option_table) / sizeof(*elem); - for (t = strtok(s, ",:"); t; t = strtok(NULL, ",:")) { + for (t = strtok_r(s, ",:", &tptr); t; t = strtok_r(NULL, ",:", &tptr)) { if (isdigit(t[0])) { /* bus-port */ if (pci_xhci_parse_bus_port(xdev, t)) { rc = -3; diff --git a/devicemodel/hw/usb_core.c b/devicemodel/hw/usb_core.c index c70d46d8e..75aa2c58e 100644 --- a/devicemodel/hw/usb_core.c +++ b/devicemodel/hw/usb_core.c @@ -82,6 +82,7 @@ #include #include #include "usb_core.h" +#include "dm_string.h" SET_DECLARE(usb_emu_set, struct usb_devemu); int usb_log_level; @@ -205,7 +206,13 @@ usb_native_is_port_existed(uint8_t bus_num, uint8_t port_num) return 0; } - native_port_cnt = atoi(cnt); + rc = dm_strtoi(cnt, (char **)&cnt, 10, &native_port_cnt); + if (rc) { + UPRINTF(LWRN, "fail to get maxchild number\r\n"); + close(fd); + return 0; + } + if (port_num > native_port_cnt || port_num < 0) { UPRINTF(LWRN, "invalid port_num %d, max port count %d\r\n", port_num, native_port_cnt); @@ -269,6 +276,7 @@ int usb_get_hub_port_num(struct usb_devpath *path) { int rc, fd; + int icnt; char buf[128]; char cnt[8]; @@ -296,5 +304,12 @@ usb_get_hub_port_num(struct usb_devpath *path) } close(fd); - return atoi(cnt); + + rc = dm_strtoi(cnt, (char **)&cnt, 10, &icnt); + if (rc) { + UPRINTF(LWRN, "fail to get maxchild\r\n"); + return -1; + } + + return icnt; }