[][openwrt][common][app][Fix Coverity defects in the switch utility]
[Description]
Fix Coverity defects
[Release-log]
N/A
Change-Id: Ibedd820671e964d5870473f18f4cd235855f7164
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/9446799
diff --git a/feed/app/switch/src/switch_753x.c b/feed/app/switch/src/switch_753x.c
index 6936f6a..9218e57 100644
--- a/feed/app/switch/src/switch_753x.c
+++ b/feed/app/switch/src/switch_753x.c
@@ -8,6 +8,7 @@
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
+#include <errno.h>
#include "switch_netlink.h"
#include "switch_ioctl.h"
@@ -427,27 +428,39 @@
{
unsigned int val;
unsigned int off;
+ char *endptr;
int i, j;
if (!strncmp(argv[len - 3], "reg", 4)) {
if (argv[len - 2][0] == 'r') {
- off = strtoul(argv[len - 1], NULL, 16);
+ errno = 0;
+ off = strtoul(argv[len - 1], &endptr, 16);
+ if (errno != 0 || *endptr != '\0')
+ goto error;
reg_read(off, &val);
printf(" Read reg=%x, value=%x\n", off, val);
} else if (argv[len - 2][0] == 'w') {
- off = strtoul(argv[len - 1], NULL, 16);
+ errno = 0;
+ off = strtoul(argv[len - 1], &endptr, 16);
+ if (errno != 0 || *endptr != '\0')
+ goto error;
if (argc != len + 1)
usage(argv[0]);
- val = strtoul(argv[len], NULL, 16);
+ errno = 0;
+ val = strtoul(argv[len], &endptr, 16);
+ if (errno != 0 || *endptr != '\0')
+ goto error;
reg_write(off, val);
printf(" Write reg=%x, value=%x\n", off, val);
} else if (argv[len - 2][0] == 'd') {
- off = strtoul(argv[len - 1], NULL, 16);
+ errno = 0;
+ off = strtoul(argv[len - 1], &endptr, 16);
+ if (errno != 0 || *endptr != '\0')
+ goto error;
for (i = 0; i < 16; i++) {
printf("0x%08x: ", off + 0x10 * i);
for (j = 0; j < 4; j++) {
- reg_read(off + i * 0x10 + j * 0x4,
- &val);
+ reg_read(off + i * 0x10 + j * 0x4, &val);
printf(" 0x%08x", val);
}
printf("\n");
@@ -456,6 +469,9 @@
usage(argv[0]);
} else
usage(argv[0]);
+ return;
+error:
+ printf("Error: string converting\n");
}
static int get_chip_name()
@@ -622,7 +638,7 @@
int main(int argc, char *argv[])
{
- int err;
+ int err = -EINVAL;
attres = (struct mt753x_attr *)malloc(sizeof(struct mt753x_attr));
if (attres == NULL) {
diff --git a/feed/app/switch/src/switch_netlink.c b/feed/app/switch/src/switch_netlink.c
index 397ebe5..f6f5fb0 100644
--- a/feed/app/switch/src/switch_netlink.c
+++ b/feed/app/switch/src/switch_netlink.c
@@ -44,7 +44,7 @@
nla_get_string(attrs[MT753X_ATTR_TYPE_MESG]);
printf("register switch dev:\n%s", val->dev_info);
} else {
- fprintf(stderr, "ERROR:No switch dev now\n");
+ printf("ERROR:No switch dev now\n");
goto done;
}
} else
@@ -132,7 +132,7 @@
/* Allocate an netllink message buffer */
msg = nlmsg_alloc();
if (!msg) {
- fprintf(stderr, "Failed to allocate netlink message\n");
+ printf("Failed to allocate netlink message\n");
exit(1);
}
if (!construct) {
@@ -148,7 +148,7 @@
if (construct) {
err = construct(msg, arg);
if (err < 0) {
- fprintf(stderr, "attributes error\n");
+ printf("attributes error\n");
goto nal_put_failure;
}
}
@@ -156,14 +156,14 @@
/* Allocate an new callback handler */
callback = nl_cb_alloc(NL_CB_CUSTOM);
if (!callback) {
- fprintf(stderr, "Failed to allocate callback handler\n");
+ printf("Failed to allocate callback handler\n");
exit(1);
}
/* Send netlink message */
err = nl_send_auto_complete(user_sock, msg);
if (err < 0) {
- fprintf(stderr, "nl_send_auto_complete failied:%d\n", err);
+ printf("nl_send_auto_complete failied:%d\n", err);
goto out;
}
finished = 0;
@@ -218,18 +218,18 @@
/* Allocate an new netlink socket */
user_sock = nl_socket_alloc();
if (!user_sock) {
- fprintf(stderr, "Failed to create user socket\n");
+ printf("Failed to create user socket\n");
goto err;
}
/* Connetct the genl controller */
if (genl_connect(user_sock)) {
- fprintf(stderr, "Failed to connetct to generic netlink\n");
+ printf("Failed to connetct to generic netlink\n");
goto err;
}
/* Allocate an new nl_cache */
ret = genl_ctrl_alloc_cache(user_sock, &cache);
if (ret < 0) {
- fprintf(stderr, "Failed to allocate netlink cache\n");
+ printf("Failed to allocate netlink cache\n");
goto err;
}
@@ -254,7 +254,7 @@
err = mt753x_request_callback(cmd, list_swdevs, NULL, arg);
if (err < 0)
- fprintf(stderr, "mt753x list dev error\n");
+ printf("mt753x list dev error\n");
}
static int mt753x_request(struct mt753x_attr *arg, int cmd)
@@ -263,7 +263,7 @@
err = mt753x_request_callback(cmd, spilt_attrs, construct_attrs, arg);
if (err < 0) {
- fprintf(stderr, "mt753x deal request error\n");
+ printf("mt753x deal request error\n");
return err;
}
return 0;
@@ -279,7 +279,7 @@
attr->port_num = port_num;
attr->phy_dev = phy_dev;
attr->reg = offset;
- attr->value = -1;
+ attr->value = 0;
attr->type = MT753X_ATTR_TYPE_REG;
switch (op) {