Skip to content

UBSAN: undefined behaviour in ivas_qmetadata_entropy_encode_dir() for MASA selftest cases

Running the selftest with USAN compiled binaries reveals undefined behaviour in the metadata encoding:

scripts/ref/logs/stv1MASA1TC48c_MASA_1dir_1TC_at_13_2_kbps_48kHz_in_48kHz_out_BINAURAL_out.enc.txt,lib_enc/ivas_qmetadata_enc.c:2678:35," runtime error:implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'uint16_t' (aka 'unsigned short') changed the value to 65535 (16-bit, unsigned)"
scripts/ref/logs/stv1MASA1TC48c_MASA_1dir_1TC_at_13_2_kbps_48kHz_in_48kHz_out_BINAURAL_out.enc.txt,lib_enc/ivas_qmetadata_enc.c:2681:29," runtime error:implicit conversion from type 'int' of value 65540 (32-bit, signed) to type 'uint16_t' (aka 'unsigned short') changed the value to 4 (16-bit, unsigned)"
scripts/ref/logs/stv1MASA1TC48n_MASA_1dir_1TC_at_13_2_kbps_48kHz_in_48kHz_out_DTX_on_7_1_4_out.enc.txt,lib_enc/ivas_qmetadata_enc.c:2701:39," runtime error:implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'uint16_t' (aka 'unsigned short') changed the value to 65535 (16-bit, unsigned)"
scripts/ref/logs/stv1MASA1TC48n_MASA_1dir_1TC_at_13_2_kbps_48kHz_in_48kHz_out_DTX_on_7_1_4_out.enc.txt,lib_enc/ivas_qmetadata_enc.c:2817:29," runtime error:implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'uint16_t' (aka 'unsigned short') changed the value to 65535 (16-bit, unsigned)"

All of them happen with

make clean
make -j CLANG=3
./IVAS_cod -MASA 1 ./scripts/testv/stv1MASA1TC48c.met -dtx 13200 48 ./scripts/testv/stv1MASA1TC48c.wav bit

I am unsure if the first occurence (line 2678) is intentional or not (could be meant as some overflow/bitflip trick, but is actually undefined):

        if ( sign_th < 0 )
        {
            avg_elevation_index = -avg_elevation_index;
        }

avg_elevation_index is an unsigned integer, so (at least in my case) this overflows from -1 to 65535. If this is the desired outcome, can this be achieved by some bitwise operation instead? If I understand correctly, the other errors somehow result from the first one directly or indirectly.

Edited by Jan Kiene