Fixed crash in bug 3367 - RGBA_FROM_PIXEL macro can't handle SDL_PIXELFORMAT_ARGB2101010
authorSam Lantinga <slouken@libsdl.org>
Fri, 11 Aug 2017 18:56:41 -0700
changeset 112267f429e943ed3
parent 11225 10e7bb76a62a
child 11227 10b07421903d
Fixed crash in bug 3367 - RGBA_FROM_PIXEL macro can't handle SDL_PIXELFORMAT_ARGB2101010

Simon Hug

The RGBA_FROM_PIXEL macro in src/video/blit.h [1] is not designed to work with more than 8 bits per channel and the ARGB2101010 format makes it read outside of the array bounds causing access violations. This can happen during blitting with the BlitNtoNPixelAlpha and SDL_Blit_Slow functions.

When SDL_InitFormat tries to calculate the loss of the channels [2], the Uint8 will wrap around and it will end up at 254 for the 10-bit channels. Clearly way over the 9 entries of the SDL_expand_byte array. (Not that a signed integer would help.) Then the macro tries to access the lookup table with the channel value which could be up to 1023. If the previous indirection didn't cause an access violation this one will.

I guess it's not worth modifying this macro for a format that only a few will use. It will only make the other blitters slower. I don't have good ideas to solve this issue.

Attached is a test case that does three blits. A copy one that work and the two that use the functions mentioned above.

[1] https://hg.libsdl.org/SDL/file/b82c0f22d22a/src/video/SDL_blit.h#l303
[2] https://hg.libsdl.org/SDL/file/b82c0f22d22a/src/video/SDL_pixels.c#l540
src/video/SDL_blit.c
     1.1 --- a/src/video/SDL_blit.c	Fri Aug 11 13:37:40 2017 -0700
     1.2 +++ b/src/video/SDL_blit.c	Fri Aug 11 18:56:41 2017 -0700
     1.3 @@ -245,6 +245,10 @@
     1.4      /* Choose a standard blit function */
     1.5      if (map->identity && !(map->info.flags & ~SDL_COPY_RLE_DESIRED)) {
     1.6          blit = SDL_BlitCopy;
     1.7 +    } else if (surface->format->Rloss > 8 || dst->format->Rloss > 8) {
     1.8 +        /* Greater than 8 bits per channel not supported yet */
     1.9 +        SDL_InvalidateMap(map);
    1.10 +        return SDL_SetError("Blit combination not supported");
    1.11      } else if (surface->format->BitsPerPixel < 8 &&
    1.12                 SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
    1.13          blit = SDL_CalculateBlit0(surface);