Change in libosmocore[master]: gsm48_decode_bcd_number2(): fix: return -ENOSPC on truncation

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/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Tue May 28 06:33:15 UTC 2019


Harald Welte has submitted this change and it was merged. ( https://gerrit.osmocom.org/14186 )

Change subject: gsm48_decode_bcd_number2(): fix: return -ENOSPC on truncation
......................................................................

gsm48_decode_bcd_number2(): fix: return -ENOSPC on truncation

The documentation of gsm48_decode_bcd_number2() clearly states that
the output truncation is a erroneous case, so it should actually
return negative in such cases. Let's return -ENOSPC.

Change-Id: I75680f232001ba419a587fed4c24f32c70c3ad2b
---
M src/gsm/gsm48_ie.c
M tests/gsm0408/gsm0408_test.c
M tests/gsm0408/gsm0408_test.ok
3 files changed, 17 insertions(+), 7 deletions(-)

Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved



diff --git a/src/gsm/gsm48_ie.c b/src/gsm/gsm48_ie.c
index 48d0d37..311836d 100644
--- a/src/gsm/gsm48_ie.c
+++ b/src/gsm/gsm48_ie.c
@@ -66,9 +66,15 @@
  * \param[in] bcd_lv  Length-Value part of to-be-decoded IE.
  * \param[in] input_len  Size of the bcd_lv buffer for bounds checking.
  * \param[in] h_len  Length of an optional header between L and V parts.
- * \return 0 in case of success, negative on error. Errors checked: no or too little input data, no or too little
- * output buffer size, IE length exceeds input data size, decoded number exceeds size of the output buffer. The output
- * is guaranteed to be nul terminated iff output_len > 0.
+ * \return 0 in case of success, negative on error.
+ *
+ * Errors checked:
+ *   - no or too little input data (-EIO),
+ *   - IE length exceeds input data size (-EIO),
+ *   - no or too little output buffer size (-ENOSPC),
+ *   - decoded number exceeds size of the output buffer (-ENOSPC).
+ *
+ * The output is guaranteed to be nul terminated iff output_len > 0.
  */
 int gsm48_decode_bcd_number2(char *output, size_t output_len,
 			     const uint8_t *bcd_lv, size_t input_len,
@@ -102,6 +108,10 @@
 	if (output_len >= 1)
 		*output++ = '\0';
 
+	/* Indicate whether the output was truncated */
+	if (i < in_len)
+		return -ENOSPC;
+
 	return 0;
 }
 
diff --git a/tests/gsm0408/gsm0408_test.c b/tests/gsm0408/gsm0408_test.c
index 3ccbf4d..55c9b61 100644
--- a/tests/gsm0408/gsm0408_test.c
+++ b/tests/gsm0408/gsm0408_test.c
@@ -702,7 +702,7 @@
 		/* Decoding test (one 5 digits do not fit) */
 		.dec_hex   = "0a21436587092143658709",
 		.dec_ascii = "123456789012345",
-		.dec_rc    = 0,
+		.dec_rc    = -ENOSPC,
 
 		/* Buffer length limitations */
 		.dec_buf_lim = 15 + 1, /* 5 digits less */
@@ -778,7 +778,7 @@
 			printf("    - Expected: (rc=%d) '%s'\n",
 			       test->dec_rc, test->dec_ascii);
 			printf("    -   Actual: (rc=%d) '%s'\n",
-			       rc, rc == 0 ? buf_dec : "(none)");
+			       rc, (rc == 0 || rc == -ENOSPC) ? buf_dec : "(none)");
 		}
 	}
 
diff --git a/tests/gsm0408/gsm0408_test.ok b/tests/gsm0408/gsm0408_test.ok
index 3938e87..b080411 100644
--- a/tests/gsm0408/gsm0408_test.ok
+++ b/tests/gsm0408/gsm0408_test.ok
@@ -173,8 +173,8 @@
     - Expected: (rc=-5) ''
     -   Actual: (rc=-5) ''
   - Decoding HEX (buffer limit=16) '0a21436587092143658709'...
-    - Expected: (rc=0) '123456789012345'
-    -   Actual: (rc=0) '123456789012345'
+    - Expected: (rc=-28) '123456789012345'
+    -   Actual: (rc=-28) '123456789012345'
 - Running test: LV incorrect length
   - Decoding HEX (buffer limit=0) '05214365'...
     - Expected: (rc=-5) '(none)'

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I75680f232001ba419a587fed4c24f32c70c3ad2b
Gerrit-Change-Number: 14186
Gerrit-PatchSet: 6
Gerrit-Owner: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190528/53b5a1c2/attachment.htm>


More information about the gerrit-log mailing list