Valgrind memcheck is not happy with SDL_RWread (?)

Hi all,

I’m not sure if this is a problem on my end or with SDL - if it’s my fault,
which it probably is, it means that I’m not using SDL correctly. If the
problem lies with SDL, well, I figure you guys probably want to hear about
it. Here’s my code, which works fine:

// Open the file

SDL_RWops* file = SDL_RWFromFile(file_path, “r”);
ASSERT(file, “Opening text file using SDL_RWops”);
// Find the length of the file
int file_length = SDL_RWseek(file, 0, SEEK_END);
(*destination) = new char[file_length];
// Reset seek to beginning of file and read text
SDL_RWseek(file, 0, SEEK_SET);
int n_blocks = SDL_RWread(file, (*destination), 8, file_length); // <—
line 63 of file.cpp

Whoever valgrind seems to be unhappy with it:

==19342== Syscall param read(buf) points to unaddressable byte(s)

==19342== at 0x5FDEA50: __read_nocancel (syscall-template.S:82)
==19342== by 0x5F78E8E: _IO_file_xsgetn (fileops.c:1465)
==19342== by 0x5F6ECB1: fread (iofread.c:44)
==19342== by 0x54D7F92: stdio_read (stdio2.h:287)
==19342== by 0x419676: io::read_text(char const*, char**) (file.cpp:63)

Does anyone know what the problem is here? What can I do to make the
mem-checker happy?

Cheers,

William

(*destination) = new char[file_length];
int n_blocks = SDL_RWread(file, (*destination), 8, file_length); //

Does anyone know what the problem is here? What can I do to make the
mem-checker happy?

You allocated a buffer of file_length bytes, and then read file_length *
8 bytes into it.

–ryan.