Skip to content
Snippets Groups Projects
Commit 0cb634ec authored by Petter Reinholdtsen's avatar Petter Reinholdtsen
Browse files

Avoided calling __Builting_clz(0) with undefined behaviuor.

Check if argument is null before using this approach, and just return
zero when the value to do bit count on is zero.

Fixes a issue discovered by --enable-gcc-sanitizers in !31:

  ../../lib/encode.c:1384:49: runtime error: passing zero to clz(),
    which is not a valid argument
    #0 0x7f0af5dae97d in th_encode_ctl ../../lib/encode.c:1384
    #1 0x7f0af5e2eb97 in theora_encode_init ../../lib/encapiwrapper.c:72
    #2 0x55fd4beb18b7 in granulepos_test_encode
     ../../tests/granulepos_theora.c:71
    #3 0x55fd4beb126d in main ../../tests/granulepos_theora.c:137
    #4 0x7f0af51d3249 in __libc_start_call_main
     ../sysdeps/nptl/libc_start_call_main.h:58
    #5 0x7f0af51d3304 in __libc_start_main_impl ../csu/libc-start.c:360
    #6 0x55fd4beb1300 in _start
     (/home/user/libtheora/build/tests/.libs/granulepos_theoraenc+0x2300)

Fixes #2323
parent 62b266ae
No related branches found
No related tags found
1 merge request!33Avoided calling __Builting_clz(0) with undefined behaviuor.
Pipeline #6219 passed
......@@ -22,7 +22,7 @@ static const unsigned char OC_DEBRUIJN_IDX32[32]={
int oc_ilog32(ogg_uint32_t _v){
#if defined(OC_CLZ32)
return OC_CLZ32_OFFS-OC_CLZ32(_v)&-!!_v;
return _v ? (OC_CLZ32_OFFS-OC_CLZ32(_v)) : 0;
#else
/*On a Pentium M, this branchless version tested as the fastest version without
multiplications on 1,000,000,000 random 32-bit integers, edging out a
......@@ -62,8 +62,8 @@ int oc_ilog32(ogg_uint32_t _v){
}
int oc_ilog64(ogg_int64_t _v){
#if defined(OC_CLZ64)
return OC_CLZ64_OFFS-OC_CLZ64(_v)&-!!_v;
#if defined(CLZ64)
return _v ? CLZ64_OFFS-CLZ64(_v) : 0;
#else
/*If we don't have a fast 64-bit word implementation, split it into two 32-bit
halves.*/
......
......@@ -67,7 +67,7 @@ int oc_ilog64(ogg_int64_t _v);
* This is the number of bits that would be required to represent _v in two's
* complement notation with all of the leading zeros stripped.
*/
# define OC_ILOG_32(_v) (OC_ILOGNZ_32(_v)&-!!(_v))
# define OC_ILOG_32(_v) ((_v)?OC_ILOGNZ_32(_v):0)
# else
# define OC_ILOGNZ_32(_v) (oc_ilog32(_v))
# define OC_ILOG_32(_v) (oc_ilog32(_v))
......@@ -90,7 +90,7 @@ int oc_ilog64(ogg_int64_t _v);
* This is the number of bits that would be required to represent _v in two's
* complement notation with all of the leading zeros stripped.
*/
# define OC_ILOG_64(_v) (OC_ILOGNZ_64(_v)&-!!(_v))
# define OC_ILOG_64(_v) ((_v)?OC_ILOGNZ_64(_v):0)
# else
# define OC_ILOGNZ_64(_v) (oc_ilog64(_v))
# define OC_ILOG_64(_v) (oc_ilog64(_v))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment