Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UBSAN error in ma_lcg_rand_s32 #853

Closed
kavika13 opened this issue May 27, 2024 · 1 comment
Closed

UBSAN error in ma_lcg_rand_s32 #853

kavika13 opened this issue May 27, 2024 · 1 comment

Comments

@kavika13
Copy link

I am making a zig wrapper, and ported over a random unit test to see if things are working. The unit test is hitting a UBSAN trap.

The test is: https://github.com/mackron/miniaudio/tree/master/tests/test_generation/ma_test_generation_noise.c

The error is:

miniaudio.h:13906:29: runtime error: signed integer overflow: 48271 * 208578991 cannot be represented in type 'int'
    #0 0x1273375 in ma_lcg_rand_s32 miniaudio.h:13906
    #1 0x1273375 in ma_lcg_rand_f64 miniaudio.h:13922
    #2 0x1273375 in ma_noise_f32_white miniaudio.h:66681
    #3 0x1273375 in ma_noise_read_pcm_frames__white miniaudio.h:66742
    #4 0x1273375 in ma_noise_read_pcm_frames miniaudio.h:66974
@mackron
Copy link
Owner

mackron commented May 31, 2024

Thanks. If you change ma_lcg_rand_s32() to cast pLCG->state to ma_uint32, does it work? Like this (untested):

static MA_INLINE ma_int32 ma_lcg_rand_s32(ma_lcg* pLCG)
{
    pLCG->state = (ma_int32)(MA_LCG_A * (ma_uint32)pLCG->state + MA_LCG_C) % MA_LCG_M;
    return pLCG->state;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants