Change in libosmocore[master]: bitvec: fix bitvec_unhex(): do not return 1 on success

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

fixeria gerrit-no-reply at lists.osmocom.org
Wed Feb 19 11:43:19 UTC 2020


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

Change subject: bitvec: fix bitvec_unhex(): do not return 1 on success
......................................................................

bitvec: fix bitvec_unhex(): do not return 1 on success

This function is supposed to return 0 on success or 1 in case of
error. However, it used to return 1 even in case of success. The
reason is that length of the input string was not taken into
account and sscanf() was failing on '\0'.

Let's use osmo_hexparse() and rely on its return value.

P.S. Funny that the unit test expectations were wrong too.

Change-Id: I441a22c7964bb31688071d8bcf6a282d8c0187ff
---
M src/bitvec.c
M tests/bitvec/bitvec_test.ok
2 files changed, 12 insertions(+), 15 deletions(-)

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



diff --git a/src/bitvec.c b/src/bitvec.c
index 5130705..10aa776 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -45,6 +45,7 @@
 #include <osmocom/core/bits.h>
 #include <osmocom/core/bitvec.h>
 #include <osmocom/core/panic.h>
+#include <osmocom/core/utils.h>
 
 #define BITNUM_FROM_COMP(byte, bit)	((byte*8)+bit)
 
@@ -457,17 +458,13 @@
  */
 int bitvec_unhex(struct bitvec *bv, const char *src)
 {
-	unsigned i;
-	unsigned val;
-	unsigned write_index = 0;
-	unsigned digits = bv->data_len * 2;
+	int rc;
 
-	for (i = 0; i < digits; i++) {
-		if (sscanf(src + i, "%1x", &val) < 1) {
-			return 1;
-		}
-		bitvec_write_field(bv, &write_index, val, 4);
-	}
+	rc = osmo_hexparse(src, bv->data, bv->data_len);
+	if (rc < 0) /* turn -1 into 1 in case of error */
+		return 1;
+
+	bv->cur_bit = rc * 8;
 	return 0;
 }
 
diff --git a/tests/bitvec/bitvec_test.ok b/tests/bitvec/bitvec_test.ok
index 2974554..b118502 100644
--- a/tests/bitvec/bitvec_test.ok
+++ b/tests/bitvec/bitvec_test.ok
@@ -119,19 +119,19 @@
 bitvec: 00 00 00 00 fc 82 84 86 88 8a 8c 8e 90 92 94 96 98 9a 9c 9e a0 a2 a4 a6 a8 aa ac ae b0 b2 b4 fc 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
 out: ff 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a ff 
 === end test_byte_ops ===
-1 -=> cur_bit=184
+0 -=> cur_bit=184
 48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
 48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b
-1 -=> cur_bit=184
+0 -=> cur_bit=184
 47240c00400000000000000079eb2ac9402b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
 47240c00400000000000000079eb2ac9402b2b2b2b2b2b
-1 -=> cur_bit=184
+0 -=> cur_bit=184
 47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
 47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b
-1 -=> cur_bit=184
+0 -=> cur_bit=184
 deadface000000000000000000000000000000beeffeed0000000000000000000000000000000000000000000000000000000000000000000000000000000000
 DEADFACE000000000000000000000000000000BEEFFEED
-0 -=> cur_bit=512
+1 -=> cur_bit=0
 fffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 FFFFFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
 arrr...

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I441a22c7964bb31688071d8bcf6a282d8c0187ff
Gerrit-Change-Number: 17214
Gerrit-PatchSet: 3
Gerrit-Owner: fixeria <axilirator at gmail.com>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <axilirator at gmail.com>
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/20200219/107ad1a7/attachment.htm>


More information about the gerrit-log mailing list