SDL 2.0.10 SDL_RWread null term missing?

The change in 2.0.10 for the SDL_RW macros to functions appears to no longer automatically apply a null terminator at the end of the void ptr* that is passed in after the data is read.

This code would read exactly 7 characters in 1 byte sizes and apply \0 at the 7th place.
char data[7];
SDL_RWread( rwops, &data, 7, 1 );

Printf output with 2.0.8

Printf output with 2.0.10
12345670] ÿΦ]D

I had to add data[7]=’\0’; after the read call.

The code is wrong, and that it worked with SDL_RW from 2.0.8 seems to be luck?
it fails for me with SDL2-2.0.8 for obvious reasons: With a char[7] array, you can’t
expect to add a zero at 7th place and printf() to print 7 characters. The following
works, though, with both 2.0.8 and 2.0.10:

$ cat 0

$ cat 0.c
#include "SDL.h"
int main (void) {
    SDL_RWops *rwops;
    char data[8];

    rwops = SDL_RWFromFile("0", "r");
    SDL_RWread(rwops, data, 7, 1); /*  data,  not  &data  */
    data[7] = 0;
    printf("%s\n", data);
    return 0;

$ gcc -O2 -Wall -W `sdl2-config --cflags` -c 0.c -o 0.o
$ gcc `sdl2-config --libs` -o a.out 0.o
$ ./a.out 

The 7 was a typo should’ve been 6. I tried your code WITHOUT the data[7]=0; line and received this output:
1234567r0-r ÿΦ rD

That’s most expected. data[] is on the stack and contains random garbage.
You do need to manually nul terminate if you want a C string there.

1 Like

Yea that was my point - in 2.0.8 it automatically added a null terminator

If you tell it to read 7 bytes and write them to the buffer, and it writes \0 as an 8th byte to the buffer, that is be a buffer overflow.
If that was indeed the behavior in 2.0.8, it was a bug.

If it instead reads 6 bytes and writes \0 as 7th when you tell it to read 7 bytes, I’d consider that a data corruption bug.