# Bug in SDL_RWops?

This topic is 4910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I have "little" problem with SDL_RWops. If you don't know what's this, I tell you: it's part of SDL responsible for abstracting reading/writing operations on various streams (ie. file, memory). Wiki is here and here. As to my problem - you see, I'm trying to load config file (which is normal text file) line by line (cause it's the way parser is working) from SDL_RWops source. That source is created using this code, borrowed from SDL_Image: SDL_RWops *source = SDL_RWFromFile(filename.c_str(), "r"); Since lines can be of any sizes (could be 10, 1024 or 1, no limits), I need a way to determine where are '\n' located. That way, I can dynamically mallocate enough memory and fill it with all characters up to '\n' - which will be passed to parser, then we deallocate that memory, move to next line and so on. Seeking for char inside SDL_RWops is performed by this "little" function. I know it's far from perfect, but it *should* be working - however, output I get from stderr shows that it isn't, and I suspect nasty bug in SDL_RWread() or SDL_RWtell().

#define CFG_DEBUG(x, y) fprintf(stderr, x, y);

// returns size of line to create, counting from current read-ptr position
//  xxx -char we're looking for
int SDL_RWfind( SDL_RWops * source, char xxx, int * last_line_length)
{
const int buffer_size = 512;

// we're reading up to buffer_size characters to this buffer
char buffer [buffer_size];

// number of bytes that were read up to now
char * buffer_ptr;

do
{

// -------------------------------- start of the most important part !

// get current offset (before reading)
CFG_DEBUG("\nSDL_RWfind: Current offset before RWread: %i.\n", SDL_RWtell(source));

// according to SDL Wiki, SDL_RWread: "It returns the number of memory blocks read"
// and here blocks have size of 1 - so it should return the number of characters read

// get current offset (after reading)
CFG_DEBUG("\nSDL_RWfind: Current offset after RWread: %i.\n", SDL_RWtell(source));

// -------------------------------- end of the most important part,  next lines of this function were added just for completness sake

if (read < 0) { CFG_DEBUG("Couldn't read even one block from stream while seeking: %c.\n", xxx); return -2; }

buffer_ptr = buffer;

while ( (buffer_ptr - buffer) < read )
{
if ( (*buffer_ptr) == xxx) // we've found it
{
return ( buffer_ptr - buffer);
}

++buffer_ptr;
}

} while ( read == buffer_size );

SDL_RWseek(source, -read_all - 1 , SEEK_CUR);
return -1;
}


And now, most important part of this mail: After using that function for this file:
bad_integer = 1234
good_integer = 1234


... I get in stderr this:

1) SDL_RWfind: Current offset before RWread: 0.
3) SDL_RWfind: Current offset after RWread: 45.


Do you see what's going on? We have read 41 characters (line 2) starting from offset 0 (line 1), so we should be at position 41, right? But line 3 says that we're at position 45!!!! And as you can see, there weren't ANY reading / writing / seeking etc. going on! The only logic I could see after this behaviour: whole file has size of 45 bytes. But that shouldn't change anything... If someone could tell me whether this is SDL_RWops bug, it's my code's failure, or has working function for finding char in SDL_RWops stream or sth similiar, I would be really gratefull for sharing it with me ( you know, ++rating etc. ;-) )

##### Share on other sites
Looks like it might be a bug that's been fixed in CVS. Any way you could build the CVS version of SDL and check?

##### Share on other sites
You're right, I remember from SDL mailinglist that there were thread about possible bug in SDL_RWops, that has been fixed in CVS - what a shame I can't google it up now :-/

(after a minute)

Ok, I've found it :-) it's here: http://www.devolution.com/pipermail/sdl/2005-May/068922.html.

and quoting part:

Quote:

First entry in Bugs section looks somewhat similiar (reading from file stream, wrong values) and that's probably what happened to my code.

I think I could build SDL from CVS on my own... but that's the last thing I would like to do, since it would take (too) much time and made some not-trivial changes to my coding enviroment. And knowing that last version of SDL was released more than half a year ago, and it's time for 1.2.9 now...

So, do you see any way to work-around this bug? Yes, I want to hack it, but I don't know exactly what is causing it. Any thoughts? Thx for help :-)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015231
×