We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5165

Summary: Undefined left shift in blit functions
Product: SDL Reporter: James Legg <jlegg>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 2.1   
Hardware: x86_64   
OS: Linux   
Attachments: Patch

Description James Legg 2020-05-29 12:37:14 UTC
Created attachment 4359 [details]
Patch

With the right inputs, undefined behaviour sanitizer reports that some left shifts in some blit functions such as Blit_3or4_to_3or4__inversed_rgb are undefined.

This occurs when an input pixel with an alpha value > 127 is present and dstfmt->Ashift is 24. The Uint8 alpha value is promoted to a (signed) int because of the bit shift, however values of 0x80 or more shifted left 24 places are not representable by an int, assuming a platform with 32 bit ints.

Explicitly casting the Uint8 alpha value to a Uint32 before the shift, as in the attach patch, allows the result type of the shift expression to hold the correct result without undefined behaviour.
Comment 1 Sam Lantinga 2020-05-29 20:29:00 UTC
Patch added, thanks!
https://hg.libsdl.org/SDL/rev/e58323b0cf8e