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
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;
}