Change in libosmocore[master]: tlv: Introduce enum with error codes for TLV parser functions

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Fri Dec 4 13:10:10 UTC 2020


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/21535 )


Change subject: tlv: Introduce enum with error codes for TLV parser functions
......................................................................

tlv: Introduce enum with error codes for TLV parser functions

Change-Id: I0b352792089c5c0c714712d2ea237beb92e1d73f
---
M include/osmocom/gsm/tlv.h
M src/gsm/tlv_parser.c
2 files changed, 31 insertions(+), 17 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/35/21535/1

diff --git a/include/osmocom/gsm/tlv.h b/include/osmocom/gsm/tlv.h
index 7fb5052..702a26c 100644
--- a/include/osmocom/gsm/tlv.h
+++ b/include/osmocom/gsm/tlv.h
@@ -40,6 +40,16 @@
 /*! maximum length of TLV of one byte length */
 #define TVLV_MAX_ONEBYTE	0x7f
 
+/*! error return codes of various TLV parser functions */
+enum osmo_tlv_parser_error {
+	OSMO_TLVP_ERR_OFS_BEYOND_BUFFER		= -1,
+	OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER	= -2,
+	OSMO_TLVP_ERR_UNKNOWN_TLV_TYPE		= -3,
+
+	OSMO_TLVP_ERR_MAND_IE_MISSING		= -50,
+	OSMO_TLVP_ERR_IE_TOO_SHORT		= -51,
+};
+
 /*! gross length of a TVLV type field */
 static inline uint16_t TVLV_GROSS_LEN(uint16_t len)
 {
diff --git a/src/gsm/tlv_parser.c b/src/gsm/tlv_parser.c
index ddc48c8..7930d64 100644
--- a/src/gsm/tlv_parser.c
+++ b/src/gsm/tlv_parser.c
@@ -266,24 +266,24 @@
 	case TLV_TYPE_TLV:
 tlv:		/* GSM TS 04.07 11.2.4: Type 4 TLV */
 		if (buf + 1 > buf + buf_len)
-			return -1;
+			return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
 		*o_val = buf+2;
 		*o_len = *(buf+1);
 		len = *o_len + 2;
 		if (len > buf_len)
-			return -2;
+			return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
 		break;
 	case TLV_TYPE_vTvLV_GAN:	/* 44.318 / 11.1.4 */
 		/* FIXME: variable-length TAG! */
 		if (*(buf+1) & 0x80) {
 			/* like TL16Vbut without highest bit of len */
 			if (2 > buf_len)
-				return -1;
+				return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
 			*o_val = buf+3;
 			*o_len = (*(buf+1) & 0x7F) << 8 | *(buf+2);
 			len = *o_len + 3;
 			if (len > buf_len)
-				return -2;
+				return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
 		} else {
 			/* like TLV */
 			goto tlv;
@@ -293,26 +293,26 @@
 		if (*(buf+1) & 0x80) {
 			/* like TLV, but without highest bit of len */
 			if (buf + 1 > buf + buf_len)
-				return -1;
+				return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
 			*o_val = buf+2;
 			*o_len = *(buf+1) & 0x7f;
 			len = *o_len + 2;
 			if (len > buf_len)
-				return -2;
+				return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
 			break;
 		}
 		/* like TL16V, fallthrough */
 	case TLV_TYPE_TL16V:
 		if (2 > buf_len)
-			return -1;
+			return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
 		*o_val = buf+3;
 		*o_len = *(buf+1) << 8 | *(buf+2);
 		len = *o_len + 3;
 		if (len > buf_len)
-			return -2;
+			return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
 		break;
 	default:
-		return -3;
+		return OSMO_TLVP_ERR_UNKNOWN_TLV_TYPE;
 	}
 
 	return len;
@@ -370,12 +370,12 @@
 		const uint8_t *val;
 		uint16_t parsed_len;
 		if (ofs > buf_len)
-			return -1;
+			return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
 		val = &buf[ofs+1];
 		len = buf[ofs];
 		parsed_len = len + 1;
 		if (ofs + parsed_len > buf_len)
-			return -2;
+			return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
 		num_parsed++;
 		ofs += parsed_len;
 		/* store the resulting val and len */
@@ -391,12 +391,12 @@
 		const uint8_t *val;
 		uint16_t parsed_len;
 		if (ofs > buf_len)
-			return -1;
+			return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
 		val = &buf[ofs+1];
 		len = buf[ofs];
 		parsed_len = len + 1;
 		if (ofs + parsed_len > buf_len)
-			return -2;
+			return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
 		num_parsed++;
 		ofs += parsed_len;
 		/* store the resulting val and len */
@@ -661,12 +661,13 @@
  *  \param[in] tp TLV parser result
  *  \param[in] log_subsys logging sub-system for log messages
  *  \param[in] log_pfx prefix for log messages
+ *  \returns 0 in case of success; negative osmo_tlv_parser_error in case of error
  */
 int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type,
 			      const struct tlv_parsed *tp, int log_subsys, const char *log_pfx)
 {
 	const struct osmo_tlv_prot_msg_def *msg_def= &pdef->msg_def[msg_type];
-	unsigned int num_err = 0;
+	unsigned int err = 0;
 	unsigned int i;
 
 	if (msg_def->mand_ies) {
@@ -676,7 +677,8 @@
 				LOGP(log_subsys, LOGL_ERROR, "%s %s %s: Missing Mandatory IE: %s\n",
 				     log_pfx, pdef->name, osmo_tlv_prot_msg_name(pdef, msg_type),
 				     osmo_tlv_prot_ie_name(pdef, iei));
-				num_err++;
+				if (!err)
+					err = OSMO_TLVP_ERR_MAND_IE_MISSING;
 			}
 		}
 	}
@@ -692,11 +694,12 @@
 			LOGP(log_subsys, LOGL_ERROR, "%s %s %s: Short IE %s: %u < %u\n", log_pfx,
 			     pdef->name, osmo_tlv_prot_msg_name(pdef, msg_type),
 			     osmo_tlv_prot_ie_name(pdef, i), TLVP_LEN(tp, i), min_len);
-			num_err++;
+			if (!err)
+				err = OSMO_TLVP_ERR_IE_TOO_SHORT;
 		}
 	}
 
-	return -num_err;
+	return err;
 }
 
 /*! Parse + Validate a TLV-encoded message against the protocol definition.
@@ -710,6 +713,7 @@
  *  \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
  *  \param[in] log_subsys logging sub-system for log messages
  *  \param[in] log_pfx prefix for log messages
+ *  \returns 0 in case of success; negative osmo_tlv_parser_error in case of error
  */
 int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef,
 			struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type,

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/21535
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I0b352792089c5c0c714712d2ea237beb92e1d73f
Gerrit-Change-Number: 21535
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20201204/834f3be4/attachment.htm>


More information about the gerrit-log mailing list