From 3fe720d1e27b16ac19c4fba412dae3acc375d029 Mon Sep 17 00:00:00 2001 From: Bruno Herrera Date: Mon, 25 Sep 2017 07:34:34 -0600 Subject: [PATCH] Fixes for problems found by Coverity in the apps/ repository: nshlib/nsh_parse.c: Avoid truncating the strcmp result into a unsigned char variable. nshlib/nsh_netcmds.c: Check for valid hostip before using it. nshlib/nsh_ddcmd.c: Fix resouce leak when 'if=' or 'of=' params are repeated in the command line. For example: dd if=/dev/null if=/dev/zero of=/dev/null or dd if=/dev/zero of=/dev/zero of=/dev/null --- nshlib/nsh_ddcmd.c | 12 +++++++++++- nshlib/nsh_netcmds.c | 27 +++++++++++++++------------ nshlib/nsh_parse.c | 6 +++--- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/nshlib/nsh_ddcmd.c b/nshlib/nsh_ddcmd.c index 88d934d1a..7f2e82bac 100644 --- a/nshlib/nsh_ddcmd.c +++ b/nshlib/nsh_ddcmd.c @@ -246,10 +246,20 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { if (strncmp(argv[i], "if=", 3) == 0) { + if (infile != NULL) + { + free(infile); + } + infile = nsh_getfullpath(vtbl, &argv[i][3]); } else if (strncmp(argv[i], "of=", 3) == 0) { + if (outfile != NULL) + { + free(outfile); + } + outfile = nsh_getfullpath(vtbl, &argv[i][3]); } else if (strncmp(argv[i], "bs=", 3) == 0) @@ -267,7 +277,7 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) } #ifndef CAN_PIPE_FROM_STD - if (!infile || !outfile) + if (infile == NULL || outfile == NULL) { nsh_output(vtbl, g_fmtargrequired, g_dd); goto errout_with_paths; diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index 3d20116de..d48cc69cf 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -997,21 +997,24 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) else #endif { + if (hostip != NULL) + { #if defined(CONFIG_NSH_DHCPC) - if (!strcmp(hostip, "dhcp")) - { - /* Set DHCP addr */ + if (!strcmp(hostip, "dhcp")) + { + /* Set DHCP addr */ - ninfo("DHCPC Mode\n"); - gip = addr.s_addr = 0; - } - else + ninfo("DHCPC Mode\n"); + gip = addr.s_addr = 0; + } + else #endif - { - /* Set host IP address */ + { + /* Set host IP address */ - ninfo("Host IP: %s\n", hostip); - gip = addr.s_addr = inet_addr(hostip); + ninfo("Host IP: %s\n", hostip); + gip = addr.s_addr = inet_addr(hostip); + } } netlib_set_ipv4addr(ifname, &addr); @@ -1020,7 +1023,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) #ifdef CONFIG_NET_IPv6 #ifdef CONFIG_NET_IPv4 - if (inet6) + if (inet6i != NULL) #endif { #warning Missing Logic diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c index 1dca1825b..5fe259779 100644 --- a/nshlib/nsh_parse.c +++ b/nshlib/nsh_parse.c @@ -1497,10 +1497,10 @@ static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd, { /* Check if the command is preceded by "while" or "until" */ - whilematch = strcmp(cmd, "while"); - untilmatch = strcmp(cmd, "until"); + whilematch = strcmp(cmd, "while") == 0; + untilmatch = strcmp(cmd, "until") == 0; - if (whilematch == 0 || untilmatch == 0) + if (whilematch || untilmatch) { uint8_t state;