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 4283

Summary: SDL's version of memset is different from libc's
Product: SDL Reporter: janisozaur <janisozaur+libsdl>
Component: *don't know*Assignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: janisozaur+libsdl, sezeroz
Version: 2.0.9   
Hardware: x86_64   
OS: Linux   
Attachments: reproducer.c
0001-SDL_memset-correct-the-passed-argument.patch

Description janisozaur 2018-09-28 20:59:19 UTC
Created attachment 3328 [details]
reproducer.c

memset's documentation reads:

* The memset() function shall copy c (converted to an unsigned char) into each of the first n bytes of the object pointed to by s. (http://pubs.opengroup.org/onlinepubs/9699919799/functions/memset.html)
* Sets the first count characters of dest to the character c. (https://msdn.microsoft.com/en-us/library/1fdeehz6.aspx)
* write a byte to a byte string (https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/memset.3.html)

The highlight here is they all mean a single _byte_, even though memset receives a parameter of type int, which can hold more data than a single byte. SDL's implementation of memset, however, does not clear any of the higher bits, causing an erroneous behaviour when passed an argument bigger than 0xff.

Please find attached reproducer and proposed patch.
Comment 1 janisozaur 2018-09-28 21:01:34 UTC
Created attachment 3329 [details]
0001-SDL_memset-correct-the-passed-argument.patch
Comment 2 Sam Lantinga 2018-09-29 03:48:37 UTC
Fixed, thanks!
https://hg.libsdl.org/SDL/rev/9f3949b8ec40
Comment 3 Ozkan Sezer 2018-09-30 18:39:21 UTC
As far as I can see, video/SDL_fillrect.c::SDL_FillRect1SSE() for e.g.
will probably have different output after this change, might (or might
not) need adjusting.
Comment 4 Sam Lantinga 2018-10-01 16:26:49 UTC
Nope, that already takes an 8 bit color value so it should be good.

Thanks for checking!
Comment 5 Ozkan Sezer 2018-10-01 17:11:52 UTC
My mistake.  Thanks for clarifying.