Change in libosmocore[master]: gsm48_mi_to_string: use osmo_bcd2str(), fix some corner cases

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
Mon Dec 10 17:06:31 UTC 2018


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

Change subject: gsm48_mi_to_string: use osmo_bcd2str(), fix some corner cases
......................................................................

gsm48_mi_to_string: use osmo_bcd2str(), fix some corner cases

By using osmo_bcd2str(), ensure that the resulting string is always nul
terminated, and always return strlen()+1 whether truncated or not.

Still keep up the previous return value style, even if that isn't consistent at
all.

The difference between IMSI/IMEI and TMSI return values remains and is not part
of this patch.

Change-Id: I1b51b72a721e1cc9d69796b804ebda741ff0f36b
---
M src/gsm/gsm48.c
M tests/gsm0408/gsm0408_test.c
M tests/gsm0408/gsm0408_test.ok
3 files changed, 24 insertions(+), 29 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Pau Espin Pedrol: Looks good to me, but someone else must approve
  Jenkins Builder: Verified



diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c
index e684a3c..4558dfb 100644
--- a/src/gsm/gsm48.c
+++ b/src/gsm/gsm48.c
@@ -638,7 +638,8 @@
 	return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
 }
 
-/*! Convert TS 04.08 Mobile Identity (10.5.1.4) to string
+/*! Convert TS 04.08 Mobile Identity (10.5.1.4) to string.
+ * This function does not validate the Mobile Identity digits, i.e. digits > 9 are returned as 'A'-'F'.
  *  \param[out] string Caller-provided buffer for output
  *  \param[in] str_len Length of \a string in bytes
  *  \param[in] mi Mobile Identity to be stringified
@@ -650,7 +651,7 @@
 int gsm48_mi_to_string(char *string, const int str_len, const uint8_t *mi,
 		       const int mi_len)
 {
-	int i;
+	int rc;
 	uint8_t mi_type;
 	char *str_cur = string;
 	uint32_t tmsi;
@@ -670,17 +671,15 @@
 	case GSM_MI_TYPE_IMSI:
 	case GSM_MI_TYPE_IMEI:
 	case GSM_MI_TYPE_IMEISV:
-		*str_cur++ = osmo_bcd2char(mi[0] >> 4);
-
-                for (i = 1; i < mi_len; i++) {
-			if (str_cur + 2 >= string + str_len)
-				return str_cur - string;
-			*str_cur++ = osmo_bcd2char(mi[i] & 0xf);
-			/* skip last nibble in last input byte when GSM_EVEN */
-			if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
-				*str_cur++ = osmo_bcd2char(mi[i] >> 4);
-		}
-		break;
+		rc = osmo_bcd2str(string, str_len, mi,
+				  1, mi_len * 2 - ((mi[0] & GSM_MI_ODD) ? 0 : 1), true);
+		/* osmo_bcd2str() returns snprintf style strlen(), this returns bytes written. */
+		if (rc < 0)
+			return 0;
+		else if (rc < str_len)
+			return rc + 1;
+		else
+			return strlen(string) + 1;
 	default:
 		break;
 	}
diff --git a/tests/gsm0408/gsm0408_test.c b/tests/gsm0408/gsm0408_test.c
index 24f903a..c786d38 100644
--- a/tests/gsm0408/gsm0408_test.c
+++ b/tests/gsm0408/gsm0408_test.c
@@ -393,7 +393,6 @@
 		.expect_mi_tlv_hex = "170449322453",
 		.str_size = 4,
 		.expect_str = "423",
-		.expect_rc = 3, /* exception: on truncation, gsm48_mi_to_string() returns strlen(), not bytes! */
 	},
 	{
 		.mi_type = GSM_MI_TYPE_IMEI,
@@ -421,7 +420,6 @@
 		.expect_mi_tlv_hex = "170a937856341290785634f2",
 		.str_size = 16,
 		.expect_str = "987654321098765",
-		.expect_rc = 15, /* exception: on truncation, gsm48_mi_to_string() returns strlen(), not bytes! */
 	},
 	{
 		/* gsm48 treats TMSI as decimal string */
diff --git a/tests/gsm0408/gsm0408_test.ok b/tests/gsm0408/gsm0408_test.ok
index 1dc4249..2db58de 100644
--- a/tests/gsm0408/gsm0408_test.ok
+++ b/tests/gsm0408/gsm0408_test.ok
@@ -23,8 +23,7 @@
   -> MI-str="4234235" rc=8
 - IMSI 4234235
   -> MI-TLV-hex='170449322453'
-  -> MI-str="423" rc=3
-     ERROR: resulting string is not explicitly nul terminated
+  -> MI-str="423" rc=4
 - IMEI 123456789012345
   -> MI-TLV-hex='17081a32547698103254'
   -> MI-str="123456789012345" rc=16
@@ -39,8 +38,7 @@
   -> MI-str="987654321098765432" rc=19
 - IMEI-SV 987654321098765432
   -> MI-TLV-hex='170a937856341290785634f2'
-  -> MI-str="987654321098765" rc=15
-     ERROR: resulting string is not explicitly nul terminated
+  -> MI-str="987654321098765" rc=16
 - TMSI 305419896
   -> MI-TLV-hex='1705f412345678'
   -> MI-str="305419896" rc=9
@@ -66,14 +64,14 @@
 Decoding zero length Mobile Identities
 - MI type: IMSI
   - writing to zero-length string:
-    rc=1
-    ERROR: Wrote to invalid memory!
+    rc=0
+    nothing written
   - writing to 1-byte-length string:
     rc=1
-    ERROR: Wrote unexpected string "1!!!!"
+    returned empty string
   - decode zero-length mi:
-    rc=2
-    ERROR: expected empty string, got output string: "1"
+    rc=1
+    returned empty string
 - MI type: TMSI
   - writing to zero-length string:
     rc=1
@@ -96,14 +94,14 @@
     returned empty string
 - MI type: IMSI | GSM_MI_ODD
   - writing to zero-length string:
-    rc=1
-    ERROR: Wrote to invalid memory!
+    rc=0
+    nothing written
   - writing to 1-byte-length string:
     rc=1
-    ERROR: Wrote unexpected string "1!!!!"
+    returned empty string
   - decode zero-length mi:
-    rc=2
-    ERROR: expected empty string, got output string: "1"
+    rc=1
+    returned empty string
 - MI type: TMSI | GSM_MI_ODD
   - writing to zero-length string:
     rc=1

-- 
To view, visit https://gerrit.osmocom.org/12154
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: I1b51b72a721e1cc9d69796b804ebda741ff0f36b
Gerrit-Change-Number: 12154
Gerrit-PatchSet: 7
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Stefan Sperling <stsp at stsp.name>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181210/3a77754a/attachment.htm>


More information about the gerrit-log mailing list