Skip to content

Commit

Permalink
DM USB: xHCI: Fix banned API issue.
Browse files Browse the repository at this point in the history
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 <liang3.yang@intel.com>
Reviewed-by: Xiaoguang Wu <xiaoguang.wu@intel.com>
Reviewed-by: Shuo Liu <shuo.a.liu@intel.com>
Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
Acked-by: Yu Wang <yu1.wang@intel.com>
  • Loading branch information
YangLiang3 authored and wenlingz committed Dec 13, 2018
1 parent e835f5f commit fbaecde
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
21 changes: 14 additions & 7 deletions devicemodel/hw/pci/xhci.c
Expand Up @@ -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: "
Expand Down Expand Up @@ -3704,18 +3705,24 @@ pci_xhci_parse_log_level(struct pci_xhci_vdev *xdev, char *opts)
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;
}
Expand Down Expand Up @@ -3873,7 +3880,7 @@ pci_xhci_parse_extcap(struct pci_xhci_vdev *xdev, char *opts)
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 *);
Expand All @@ -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;
Expand Down
19 changes: 17 additions & 2 deletions devicemodel/hw/usb_core.c
Expand Up @@ -82,6 +82,7 @@
#include <fcntl.h>
#include <unistd.h>
#include "usb_core.h"
#include "dm_string.h"

SET_DECLARE(usb_emu_set, struct usb_devemu);
int usb_log_level;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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;
}

0 comments on commit fbaecde

Please sign in to comment.