Change in libosmocore[master]: tlv: Fix length returned by t{l16, 16l}v_put

laforge gerrit-no-reply at lists.osmocom.org
Fri Mar 19 07:16:57 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/23401 )

Change subject: tlv: Fix length returned by t{l16,16l}v_put
......................................................................

tlv: Fix length returned by t{l16,16l}v_put

Every other function returns a pointer to the first byte after the tlv
that was just written.

tl16v seems to be a copy and paste error from tlv16 above and t16lv seems
to count the 16-bit tag twice.

The new tests verify that the return value of *_put(buf, tag, len, val)
points to buf + *_GROSS_LEN(len).

Change-Id: I268a7e11fb5dce67ce1bd7974ab86c4d2bd002f7
---
M include/osmocom/gsm/tlv.h
M tests/tlv/tlv_test.c
2 files changed, 34 insertions(+), 2 deletions(-)

Approvals:
  pespin: Looks good to me, but someone else must approve
  laforge: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/include/osmocom/gsm/tlv.h b/include/osmocom/gsm/tlv.h
index dc61de1..7e6dfb5 100644
--- a/include/osmocom/gsm/tlv.h
+++ b/include/osmocom/gsm/tlv.h
@@ -148,7 +148,7 @@
 	*buf++ = len >> 8;
 	*buf++ = len & 0xff;
 	memcpy(buf, val, len);
-	return buf + len*2;
+	return buf + len;
 }
 
 /*! put (append) a TL16 field. */
@@ -168,7 +168,7 @@
 	*buf++ = tag & 0xff;
 	*buf++ = len;
 	memcpy(buf, val, len);
-	return buf + len + 2;
+	return buf + len;
 }
 
 /*! put (append) a TvLV field */
diff --git a/tests/tlv/tlv_test.c b/tests/tlv/tlv_test.c
index 3b4f441..fdd15ab 100644
--- a/tests/tlv/tlv_test.c
+++ b/tests/tlv/tlv_test.c
@@ -423,6 +423,37 @@
 	OSMO_ASSERT(TLVP_VAL(&dec, 0x23) == NULL);
 }
 
+static void test_tlv_lens()
+{
+	uint16_t buf_len;
+	uint8_t buf[512];
+	uint8_t val[512] = { 0 };
+	uint16_t x;
+
+
+	for (x = 0; x < 16; x++) {
+		buf_len  = lv_put(buf, x, val) - buf;
+		OSMO_ASSERT(buf_len == LV_GROSS_LEN(x));
+		buf_len = tlv_put(buf, 0x23, x, val) - buf;
+		OSMO_ASSERT(buf_len == TLV_GROSS_LEN(x));
+		buf_len = tlv16_put(buf, 0x23, x, (uint16_t *) val) - buf;
+		OSMO_ASSERT(buf_len == TLV16_GROSS_LEN(x));
+		buf_len = tl16v_put(buf, 0x23, x, val) - buf;
+		OSMO_ASSERT(buf_len == TL16V_GROSS_LEN(x));
+		buf_len = t16lv_put(buf, 0x2342, x, val) - buf;
+		OSMO_ASSERT(buf_len == T16LV_GROSS_LEN(x));
+		buf_len = tvlv_put(buf, 0x23, x, val) - buf;
+		OSMO_ASSERT(buf_len == TVLV_GROSS_LEN(x));
+	}
+
+	for (x = 250; x < 300; x++) {
+		buf_len = tl16v_put(buf, 0x23, x, val) - buf;
+		OSMO_ASSERT(buf_len == TL16V_GROSS_LEN(x));
+		buf_len = tvlv_put(buf, 0x23, x, val) - buf;
+		OSMO_ASSERT(buf_len == TVLV_GROSS_LEN(x));
+	}
+}
+
 int main(int argc, char **argv)
 {
 	//osmo_init_logging2(ctx, &info);
@@ -431,6 +462,7 @@
 	test_tlv_repeated_ie();
 	test_tlv_encoder();
 	test_tlv_parser_bounds();
+	test_tlv_lens();
 
 	printf("Done.\n");
 	return EXIT_SUCCESS;

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I268a7e11fb5dce67ce1bd7974ab86c4d2bd002f7
Gerrit-Change-Number: 23401
Gerrit-PatchSet: 1
Gerrit-Owner: daniel <dwillmann at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210319/b0d4a51f/attachment.htm>


More information about the gerrit-log mailing list