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
This commit is contained in:
Vadim Yanitskiy 2020-02-19 04:20:08 +07:00
parent 6e270e2530
commit 832d8b8633
2 changed files with 12 additions and 15 deletions

View File

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

View File

@ -119,19 +119,19 @@ 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
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...