Commit Graph

14 Commits

Author SHA1 Message Date
Harald Welte e61d459cef Support building with -Werror=strict-prototypes / -Werror=old-style-definition
Unfortunately "-std=c99" is not sufficient to make gcc ignore code that
uses constructs of earlier C standards, which were abandoned in C99.

See https://lwn.net/ml/fedora-devel/Y1kvF35WozzGBpc8@redhat.com/ for
some related discussion.

Change-Id: I84fd99442d0cc400fa562fa33623c142649230e2
2022-11-03 12:44:28 +01:00
Vadim Yanitskiy 8a55a6c571 bitvec_read_field(): fix incorrect bit-shift issue found by UBSan
While running a sanitized version of the bitvec_test I get:

  bitvec.c:492:24: runtime error: shift exponent 64 is too large
                                  for 64-bit type 'long unsigned int'

This error is triggered by the following line in the bitvec_test:

  _bitvec_read_field(0, 8 * 8 + 1); /* too many bits */

which basically tries to parse more bits (65) than the test vector
actually has (64).  The problem is that we don't check if the
given vector has enough data *before* entering the parsing loop,
so we end up doing weird bit-shifts and getting weird values:

  bitvec_read_field(idx=0, len=65) => bd5b7ddffdd7b5db (error)

Unfortunately, this problem remained unnoticed so far because in
'tests/testsuite.at' we don't check if stderr is empty.  This is
fixed in a follow up change [1].

Rather than checking for errors in every loop iteration, do this
once and return early if the overrun is possible with the given
offset and length arguments.

Change-Id: I4deeabba7ebb720cdbe7c85b37bc011d05bdfa65
Related: [1] Ia82b92eddb18dc596881abcef2f098dc7385538b
2021-11-18 13:11:20 +00:00
Vadim Yanitskiy de3549a234 bitvec_read_field(): indicate errors using errno
This function returns an *unsigned* integer (uint64_t), so returning
a negative value on error is a bad idea.  A negative value turns into
a huge positive value, what was demonstrated in the bitvec_test:

  bitvec_read_field(idx=512, len=16) => ffffffffffffffea
  bitvec_read_field(idx=0, len=65) => ffffffffffffffea
  bitvec_read_field(idx=64, len=16) => ffffffffffffffea

The 0xffffffffffffffea above is basically:

  (uint64_t) -EINVAL, or
  (uint64_t) -22 + 1, or
  0xffffffffffffffff - 0x16 + 1.

Let's make use of the errno in order to indicate an error to the caller.

Change-Id: I2cc734caa3365d03c2ae2b3f2cd9544933c25e9e
Related: OS#4388
2021-11-18 13:11:20 +00:00
Vadim Yanitskiy 832d8b8633 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
2020-02-19 18:23:40 +07:00
Vadim Yanitskiy 6e270e2530 tests/bitvec: add a unit test for bitvec_read_field()
The aim of this unit test is to demonstrate the problem described
in OS#4388: bitvec_read_field() can never return negative value
on error (e.g. out of bounds access).

Change-Id: I340ab5799fa53d5345edb02f3e2a3655527705c0
Related: OS#4388
2020-02-19 18:23:40 +07:00
Harald Welte b0708d3e76 bitvec: Add bitvec_tailroom_bits() function
This is similar to msgb_tailroom(): It returns the amount of space
left at the end of the bit vector (compared to the current cursor).

The function returns the number of bits left in the bitvec.

Change-Id: I8980a6b6d1973b67a2d9ad411c878d956fb428d1
2019-02-05 11:16:44 +01:00
Harald Welte ae7966d145 bitvec: Add bitvec_bytes_used() function
This new bitvec API function returns the number of bytes used in a given
bit-vector.

Change-Id: Id4bd7f7543f5b0f4f6f876e283bd065039c37646
2019-02-05 09:24:17 +00:00
Pravin Kumarvel 848de8f1df Add function to get uninterrupted bit run
Function bitvec_rl_curbit added to get number of  uninterrupted
bits run in vector starting from the current bit till max number
of bits.
Test case is added to check bitvec_rl_curbit.

Change-Id: Iae153d3639ea6b891c1fc10d7801a435c9492e26
2017-01-06 10:37:42 +00:00
Max d4793212b5 Add function to add bits from array to bitvec
Add function which adds specified number of bits from each element of
array to the bit vector prefixing each addition with one and finishing
entire sequence with adding 0. This is very common patter for various
repetitive data structures described with CSN.1 in 3GPP standards.

Corresponding test vectors and doxygen headers are added too.
2016-03-17 14:07:19 +01:00
Max 6a5ef46dd0 Add byte printing macros
It's sometimes handy for debugging to be able to immediately see which
bits are set in a given byte. Generalize macro used for that in bitvec
tests and make it available for the rest of the library.
2016-02-25 22:35:44 +01:00
Max 0a59e9899f Expand bitvec interface
Add bit filling, shifting and other functions necessary for bit
compression implementation. Add corresponding tests.
2016-02-18 19:45:40 +01:00
Holger Hans Peter Freyther 5710804a42 bivec: Fix the output of the testcase
osmo_hexdump_nospc/osmo_hexdump is an old school C routine with a
static internal array. This means that printf will most likely one
of the two strings twice. For Linux/glibc this is the first string
(for whatever reason?) and for FreeBSD it is the last call of the
osmo_hexdump_nospc. We could have noticed by both strings being
of the same length besides the different length input. The second
issue is that we cast a hexstring to uint8_t and dump the string
as hex. So the two strings should not match at all.

Fix it by printing the hex string as plain hex and separating the
two printf calls. Update the test output.
2016-01-30 16:19:33 +01:00
Holger Hans Peter Freyther a9301a1a1f bitvec: Test and fix regression for C++->C conversion
bitvec_read_field/bitvec_write_field in the PCU used a C++ reference
and when porting to C it was decided to pass the parameter by value
and this lost the "back propagation" of the new index. Change the
parameter to be an in/out parameter and this way do not have a silent
semantic break in the osmo-pcu (where we copy the reference in csn.1
by value) and have a true compile failure.

Add Max's simple test for bitvec_unhex function leaving the checking
of bitvec_read_field and the side effect in the datastructure about
the number of bits still open.
2016-01-30 10:54:43 +01:00
Jacob Erlbeck 5f349be820 bitvec: Add get/set byte sequences
The new functions bitvec_get_bytes and bitvec_set_bytes copy
byte sequences from bitvecs to uint8_t arrays and vice versa.
While the bytes in the bitvecs do not need to be aligned, the uint8_t
arrays always are. In case the bytes in the bitvec are aligned, the
implementation uses memcpy.

Note that the implementation like the other existing functions assume
MSB first encoding.

[hfreyther: Squash the comment fix into this commit as well]

Sponsored-by: On-Waves ehf
2016-01-15 14:51:32 +01:00