[lldp-devel] [PATCH 7/8] prevent crash on invalid response code

Jens Osterkamp jens at linux.vnet.ibm.com
Fri Aug 5 15:35:43 UTC 2011


If an unknown response code was received for a profile, the lookup of the
error string could cause a crash.

Signed-off-by: Jens Osterkamp <jens at linux.vnet.ibm.com>
---
 include/lldp_vdp.h |    2 ++
 lldp_vdp.c         |   24 +++++++++++++++++++++++-
 lldp_vdp_cmds.c    |    2 +-
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/include/lldp_vdp.h b/include/lldp_vdp.h
index f9cdee2..1192364 100644
--- a/include/lldp_vdp.h
+++ b/include/lldp_vdp.h
@@ -43,6 +43,7 @@
 #define VDP_RESPONSE_VTID_VIOLATION	0x4
 #define VDP_RESPONSE_VTID_VER_VIOLATION	0x5
 #define VDP_RESPONSE_OUT_OF_SYNC	0x6
+#define VDP_RESPONSE_UNKNOWN		0xfe
 #define VDP_RESPONSE_NO_RESPONSE	0xff
 
 extern const char * const vsi_responses[];
@@ -138,6 +139,7 @@ void vdp_ack_profiles(struct vdp_data *vd, int seqnr);
 int vdp_indicate(struct vdp_data *vd, struct unpacked_tlv *tlv, int ecp_mode);
 int vdp_vsis_pending(struct vdp_data *vd);
 int vdp_vsis(char *ifname);
+const char *vdp_response2str(int);
 void vdp_print_profile(struct vsi_profile *);
 void ecp_somethingChangedLocal(struct vdp_data *vd, bool flag);
 void ecp_rx_send_ack_frame(struct vdp_data *vd);
diff --git a/lldp_vdp.c b/lldp_vdp.c
index 1699981..e317e6f 100644
--- a/lldp_vdp.c
+++ b/lldp_vdp.c
@@ -53,6 +53,8 @@ const char * const vsi_responses[] = {
 	[VDP_RESPONSE_VTID_VIOLATION] = "VTID violation",
 	[VDP_RESPONSE_VTID_VER_VIOLATION] = "VTID version violation",
 	[VDP_RESPONSE_OUT_OF_SYNC] = "out of sync",
+	[VDP_RESPONSE_UNKNOWN] = "unknown response",
+	[VDP_RESPONSE_NO_RESPONSE] = "no response",
 };
 
 const char * const vsi_states[] = {
@@ -126,6 +128,26 @@ static void vdp_free_data(struct vdp_user_data *ud)
 	}
 }
 
+/* vdp_response2str - map response to string
+ * @response: response received
+ *
+ * no return value
+ *
+ * maps VDP response received for a profile to human readable string for
+ * printing.
+ */
+const char *vdp_response2str(int response)
+{
+	if ((response >= VDP_RESPONSE_SUCCESS) &&
+	    (response <= VDP_RESPONSE_OUT_OF_SYNC))
+		return vsi_responses[response];
+
+	if (response == VDP_RESPONSE_NO_RESPONSE)
+		return vsi_responses[VDP_RESPONSE_NO_RESPONSE];
+
+	return vsi_responses[VDP_RESPONSE_UNKNOWN];
+}
+
 /* vdp_print_profile - print a vsi profile
  * @profile: profile to print
  *
@@ -992,7 +1014,7 @@ int vdp_indicate(struct vdp_data *vd, struct unpacked_tlv *tlv, int ecp_mode)
 				LLDPAD_DBG("%s(%i): profile response: %s (%i) "
 					   "for profile 0x%02x at state %s.\n",
 					   __func__, __LINE__,
-					   vsi_responses[p->response],
+					   vdp_response2str(p->response),
 					   p->response, p->instance[15],
 					   vsi_states[p->state]);
 				free(profile);
diff --git a/lldp_vdp_cmds.c b/lldp_vdp_cmds.c
index 754d3e2..5549981 100644
--- a/lldp_vdp_cmds.c
+++ b/lldp_vdp_cmds.c
@@ -97,7 +97,7 @@ static char * print_profile(char *s, size_t length, struct vsi_profile *p)
 		return r;
 
 	c = snprintf(s, length, "response: %i (%s)\n", p->response,
-		vsi_responses[p->response]);
+		vdp_response2str(p->response));
 	s = check_and_update(&total, &length, s, c);
 	if (!s)
 		return r;
-- 
1.7.4.4




More information about the lldp-devel mailing list