<div dir="ltr"><div dir="ltr">Hi, while running x264 under Clang's UndefinedBehaviorSanitizer, I'm hitting integer overflow in the following line:<br><div><br></div><div><a href="https://code.videolan.org/videolan/x264/blob/master/encoder/slicetype.c#L408">https://code.videolan.org/videolan/x264/blob/master/encoder/slicetype.c#L408</a><br></div><div><br></div><div>where "ref_mean[plane]" is 0.000003 because the reference frame's "i_pixel_sum[plane]" is 0 ("ref_mean[plane]" is not zero because zero_bias is added). With the following clamping using float I am able to fix the problem locally:</div><div><br></div><div><div>408,409c408,409 encoder/slicetype.c</div><div><    cur_scale = (1 << mindenom) * (fenc_mean[plane] - cur_offset) / ref_mean[plane] + 0.5f;</div><div><    cur_scale = x264_clip3( cur_scale, 0, 127 );</div><div>---</div><div>>    float temp = (1 << mindenom) * (fenc_mean[plane] - cur_offset) / ref_mean[plane] + 0.5f;</div><div>>    cur_scale = temp > 127.0f ? 127 : (temp < 0.0f ? 0 : x264_clip3( temp, 0, 127 ));</div></div><div><br></div><div>If this looks good, can someone help me merge it? Otherwise is there a more appropriate fix?</div></div></div>