diff options
author | Vadim Yanitskiy <axilirator@gmail.com> | 2020-02-19 04:20:08 +0700 |
---|---|---|
committer | Vadim Yanitskiy <axilirator@gmail.com> | 2020-02-19 18:23:40 +0700 |
commit | 832d8b8633915bb8eef66ade43b2780f0ca04842 (patch) | |
tree | 61800270d1486ef4a965c3b24fd434629c037594 /src | |
parent | 6e270e2530fc095c7fe96296aa0d0fd6c0c64796 (diff) |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/bitvec.c | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/src/bitvec.c b/src/bitvec.c index 5130705f..10aa776a 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 @@ unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer) */ 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; } |