FORUM CLOSED, PLEASE REGISTER AT FORUM.SIO2INTERACTIVE.COM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Bugs in sio2ResourceDel

2 posters

Go down

Bugs in sio2ResourceDel Empty Bugs in sio2ResourceDel

Post  matt Sun Oct 19, 2008 4:15 pm

I just looked at sio2ResourceDel and it appears to me that it's buggy. For all types, it looks like

Code:
unsigned int i = 0;
while( i != _SIO2resource->n_image )
{
   if( _ptr == _SIO2resource->_SIO2image[ i ] )
   {
Next, you're moving the entries of all following resources to the position of the deleted resource.

Code:
      memcpy( &_SIO2resource->_SIO2image[ i ],
            &_SIO2resource->_SIO2image[ i + 1 ],
            (_SIO2resource->n_image - i ) * sizeof( void * ) );
First, you're accessing memory at index i+1 which is unallocated in case of the last element, i.e. i == _SIOresource->n_image-1. Copying should only take place if i < n_image-1.
Second, if i is e.g. 8 and the number of objects is 12, then the expression "_SIO2resource->n_object - i" gives 4, but in fact you only want to copy 3 elements (index 9, 10, 11 (which is the 12th element)).

Bear with me if I'm on the wrong track, it's late here.

Best,
Matt

matt

Posts : 155
Join date : 2008-09-30

http://elfrun.net

Back to top Go down

Bugs in sio2ResourceDel Empty Re: Bugs in sio2ResourceDel

Post  sio2interactive Sun Oct 19, 2008 11:55 pm

Dude you are totally right...

That code should fix the problem:

Code:


         while( i != _SIO2resource->n_image )
         {
            if( _ptr == _SIO2resource->_SIO2image[ i ] )
            {
               --_SIO2resource->n_image;
                           
               if( i < _SIO2resource->n_image )
               {
                  memcpy( &_SIO2resource->_SIO2image[ i ],
                        &_SIO2resource->_SIO2image[ i + 1 ],
                        (_SIO2resource->n_image - i ) * sizeof( void * ) );
               }

               _SIO2resource->_SIO2image = ( void ** ) realloc( _SIO2resource->_SIO2image,
                                           _SIO2resource->n_image * sizeof( void * ) );
               break;
            }

sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 44
Location : Shanghai

http://sio2interactive.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum