Out by one?

Greetings,

could someone please check this out by one bug?


SDL_RWops *SDL_RWFromMem(void *mem, int size)
{
	SDL_RWops *rwops;

	rwops = SDL_AllocRW();
	if ( rwops != NULL ) {
		rwops->seek = mem_seek;
		rwops->read = mem_read;
		rwops->write = mem_write;
		rwops->close = mem_close;
		rwops->hidden.mem.base = (Uint8 *)mem;
		rwops->hidden.mem.here = rwops->hidden.mem.base;
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
		                                               ^
						 shouldn't this say +size-1  ?
	}
	return(rwops);
}-- 

-dv

Greetings,

sorry the last posts formatting was a little screwed up. here it 

is again…

could someone please check this out by one bug?


SDL_RWops *SDL_RWFromMem(void *mem, int size)
{
	SDL_RWops *rwops;

	rwops = SDL_AllocRW();
	if ( rwops != NULL ) {
		rwops->seek = mem_seek;
		rwops->read = mem_read;
		rwops->write = mem_write;
		rwops->close = mem_close;
		rwops->hidden.mem.base = (Uint8 *)mem;
		rwops->hidden.mem.here = rwops->hidden.mem.base;
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
                                                               ^
                                       shouldn't this say +size-1  ?
	}
	return(rwops);
}


thanks.-- 

-dv

could someone please check this out by one bug?

SDL_RWops *SDL_RWFromMem(void *mem, int size)
{
SDL_RWops *rwops;

  rwops = SDL_AllocRW();
  if ( rwops != NULL ) {
  	rwops->seek = mem_seek;
  	rwops->read = mem_read;
  	rwops->write = mem_write;
  	rwops->close = mem_close;
  	rwops->hidden.mem.base = (Uint8 *)mem;
  	rwops->hidden.mem.here = rwops->hidden.mem.base;
  	rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
                                                               ^
                                       shouldn't this say +size-1  ?

Hmm, no, it should be base+size, but the checking against stop is incorrect.

e.g. size = 1, base = 0, stop = 1, here = 0
trying to read 8 bytes:
if ( here+8 > stop ) {
len = here+8 - stop
}
len = 1, as it should be, but the check should be (here+8 >= stop), not >

stop is the first invalid byte, and should never actually be read.

Thanks!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software