• Sam Lantinga's avatar
    Fixed bug 1091 - Hardcoded size in SDL_audiocvt.c may lead to heap/stack corruption · 7e2e5d34
    Sam Lantinga authored
    Markovtsev Vadim 2011-01-18 22:00:16 PST
    
    SDL_audiocvt.c:
    
    static void SDLCALL
    SDL_ConvertStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format):
    
    #define dup_chans_1_to_2(type) \
        { \
            const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
            type *dst = (type *) (cvt->buf + cvt->len_cvt * 2); \
            for (i = cvt->len_cvt / 2; i; --i, --src) { \
                const type val = *src; \
                dst -= 2; \
                dst[0] = dst[1] = val; \
            } \
        }
    
    Pay attention to cvt->len_cvt / 2. 2 is the sizeof(Uint16), hovewer, below we
    see that the conversion function supports Uint8 and Uint32:
    
    switch (SDL_AUDIO_BITSIZE(format)) {
        case 8:
            dup_chans_1_to_2(Uint8);
            break;
        case 16:
            dup_chans_1_to_2(Uint16);
            break;
        case 32:
            dup_chans_1_to_2(Uint32);
            break;
        }
    
    If type is Uint32, src will be decreased twice as it should be, memory being
    written before the cvt->buf. If type is Uint8, the conversion will not be
    complete. I suggest to change that define to
    
    #define dup_chans_1_to_2(type) \
        { \
            const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
            type *dst = (type *) (cvt->buf + cvt->len_cvt * 2); \
            for (i = cvt->len_cvt / sizeof(type); i; --i, --src) { \
                const type val = *src; \
                dst -= 2; \
                dst[0] = dst[1] = val; \
            } \
        }
    
    I tested that and now it's working fine. I did not consider the similar defines
    in functions nearby.
    7e2e5d34
Name
Last commit
Last update
VisualC Loading commit data...
Xcode Loading commit data...
Xcode-iOS Loading commit data...
acinclude Loading commit data...
android-project Loading commit data...
build-scripts Loading commit data...
include Loading commit data...
src Loading commit data...
test Loading commit data...
.DISABLED-hgeol Loading commit data...
.hgignore Loading commit data...
.hgtags Loading commit data...
.indent.pro Loading commit data...
Android.mk Loading commit data...
BUGS Loading commit data...
Borland.html Loading commit data...
Borland.zip Loading commit data...
COPYING Loading commit data...
CREDITS Loading commit data...
INSTALL Loading commit data...
Makefile.ds Loading commit data...
Makefile.in Loading commit data...
Makefile.minimal Loading commit data...
Makefile.pandora Loading commit data...
Makefile.wiz Loading commit data...
NOTES Loading commit data...
README Loading commit data...
README-SDL.txt Loading commit data...
README.BeOS Loading commit data...
README.DirectFB Loading commit data...
README.HG Loading commit data...
README.MacOSX Loading commit data...
README.Platforms Loading commit data...
README.Porting Loading commit data...
README.Watcom Loading commit data...
README.WinCE Loading commit data...
README.android Loading commit data...
README.ds Loading commit data...
README.gesture Loading commit data...
README.iOS Loading commit data...
README.pandora Loading commit data...
README.touch Loading commit data...
SDL.spec.in Loading commit data...
TODO Loading commit data...
UNDER_CONSTRUCTION.txt Loading commit data...
VisualC.html Loading commit data...
Watcom-Win32.zip Loading commit data...
WhatsNew Loading commit data...
aclocal.m4 Loading commit data...
autogen.sh Loading commit data...
configure Loading commit data...
configure.in Loading commit data...
sdl-config.in Loading commit data...
sdl.m4 Loading commit data...
sdl.pc.in Loading commit data...