File lldpad-0.9.32-fix-lldptool-segfault of Package lldpad

lldpad: fix segfault handling lldptool arguments

From: Eric Multanen <eric.w.multanen@intel.com>

This patch fixes a segfault which occurred when
incomplete arguments were passed via the lldptool
set commands.  In addition, it tightens up the
argument handling.  Errors are printed in more
cases where incorrect or unnecessary arguments
are supplied.
---

 lldp_mand_cmds.c |   28 ++++++++++++++++++++--------
 lldptool_cmds.c  |   32 +++++++++++++++++++++++---------
 2 files changed, 43 insertions(+), 17 deletions(-)

--
Signed-off-by: Eric Multanen <eric.w.multanen@intel.com>

diff --git a/lldp_mand_cmds.c b/lldp_mand_cmds.c
index 780fe62..5f444f3 100644
--- a/lldp_mand_cmds.c
+++ b/lldp_mand_cmds.c
@@ -65,6 +65,9 @@ int get_arg_adminstatus(struct cmd *cmd, char *arg, char *argvalue, char *obuf)
 	int value;
 	char *s;
 
+	if (cmd->cmd != cmd_get_lldp)
+		return cmd_bad_params;
+
 	if (cmd->tlvid != INVALID_TLVID)
     		return cmd_bad_params;
 
@@ -101,7 +104,7 @@ int get_arg_tlvtxenable(struct cmd *cmd, char *arg, char *argvalue, char *obuf)
 	char arg_path[256];
 
 	if (cmd->cmd != cmd_gettlv)
-		return cmd_invalid;
+		return cmd_bad_params;
 
 	switch (cmd->tlvid) {
 	case CHASSIS_ID_TLV:
@@ -130,7 +133,7 @@ int handle_get_arg(struct cmd *cmd, char *arg, char *argvalue, char *obuf)
 {
 	struct lldp_module *np;
 	struct arg_handlers *ah;
-	int rval = cmd_failed;
+	int rval = cmd_invalid;
 
 	if (!init_cfg())
 		return rval;
@@ -328,7 +331,7 @@ int mand_clif_cmd(void  *data,
 	u8 arglen;
 	u16 argvalue_len;
 	int ioff, roff;
-	int rstatus = cmd_success;
+	int rstatus = cmd_invalid;
 	char *arg = NULL;
 	char *argvalue = NULL;
 
@@ -386,11 +389,15 @@ int mand_clif_cmd(void  *data,
 
 	switch (cmd.cmd) {
 	case cmd_getstats:
+		if (arg || argvalue)
+			break;
 		rstatus = get_port_stats(&cmd, rbuf+roff);
 		break;
 	case cmd_gettlv:
 		sprintf(rbuf+roff, "%08x", cmd.tlvid);
 		roff+=8;
+		if (argvalue)
+			break;
 		if (arg)
 			rstatus = handle_get_arg(&cmd, arg, NULL,
 						 rbuf+strlen(rbuf));
@@ -400,19 +407,24 @@ int mand_clif_cmd(void  *data,
 	case cmd_settlv:
 		sprintf(rbuf+roff, "%08x", cmd.tlvid);
 		roff+=8;
-		rstatus = handle_set_arg(&cmd, arg, argvalue,
+		if (arg && argvalue)
+			rstatus = handle_set_arg(&cmd, arg, argvalue,
 					 rbuf+strlen(rbuf));
 		break;
 	case cmd_get_lldp:
-		rstatus = handle_get_arg(&cmd, arg, NULL,
+		if (argvalue)
+			break;
+		if (arg)
+			rstatus = handle_get_arg(&cmd, arg, NULL,
 					 rbuf+strlen(rbuf));
 		break;
 	case cmd_set_lldp:
-		rstatus = handle_set_arg(&cmd, arg, argvalue,
-			rbuf+strlen(rbuf));
+		if (arg && argvalue)
+			rstatus = handle_set_arg(&cmd, arg, argvalue,
+				rbuf+strlen(rbuf));
 		break;
 	default:
-		return cmd_invalid;
+		break;
 	}
 
 	return rstatus;
diff --git a/lldptool_cmds.c b/lldptool_cmds.c
index 12218cc..08845c1 100644
--- a/lldptool_cmds.c
+++ b/lldptool_cmds.c
@@ -37,6 +37,8 @@
 #include "lldp.h"
 #include "lldp_mod.h"
 
+static char *print_status(cmd_status status);
+
 int render_cmd(struct cmd *cmd, char *arg, char *argvalue)
 {
 	int len;
@@ -112,6 +114,12 @@ int cli_cmd_gettlv(struct clif *clif, int argc, char *argv[],
 		cmd->ops |= op_local;
 	if (arg)
 		cmd->ops |= op_arg;
+
+	if (argvalue) {
+		printf("%s\n", print_status(cmd_invalid));
+		return cmd_invalid;
+	}
+
 	render_cmd(cmd, arg, argvalue);
 	return clif_command(clif, cmd->obuf, raw);
 }
@@ -125,8 +133,10 @@ int cli_cmd_settlv(struct clif *clif, int argc, char *argv[],
 	if (argc > 0)
 		get_arg_value(argv[0], &arg, &argvalue);
 
-	if (!argvalue)
+	if (!argvalue) {
+		printf("%s\n", print_status(cmd_invalid));
 		return cmd_invalid;
+	}
 
 	render_cmd(cmd, arg, argvalue);
 	return clif_command(clif, cmd->obuf, raw);
@@ -138,10 +148,14 @@ int cli_cmd_getlldp(struct clif *clif, int argc, char *argv[],
 	char *arg = NULL;
 	char *argvalue = NULL;
 
-	if (argc < 1)
-		return 1;
+	if (argc > 0)
+		get_arg_value(argv[0], &arg, &argvalue);
+
+	if (!arg || argvalue) {
+		printf("%s\n", print_status(cmd_invalid));
+		return cmd_invalid;
+	}
 
-	get_arg_value(argv[0], &arg, &argvalue);
 
 	render_cmd(cmd, arg, argvalue);
 	return clif_command(clif, cmd->obuf, raw);
@@ -153,13 +167,13 @@ int cli_cmd_setlldp(struct clif *clif, int argc, char *argv[],
 	char *arg = NULL;
 	char *argvalue = NULL;
 
-	if (argc < 1)
-		return 1;
-
-	get_arg_value(argv[0], &arg, &argvalue);
+	if (argc > 0)
+		get_arg_value(argv[0], &arg, &argvalue);
 
-	if (!argvalue)
+	if (!argvalue) {
+		printf("%s\n", print_status(cmd_invalid));
 		return cmd_invalid;
+	}
 
 	render_cmd(cmd, arg, argvalue);
 	return clif_command(clif, cmd->obuf, raw);