Fix some corner cases in ringbuffer and make unitary test have no leak

This commit is contained in:
Hardening 2014-05-21 18:32:59 +02:00
parent 5234e05843
commit a04843bc9e
2 changed files with 6 additions and 0 deletions

View File

@ -69,6 +69,7 @@ static BOOL ringbuffer_realloc(RingBuffer *rb, size_t targetSize)
if (!newData) if (!newData)
return FALSE; return FALSE;
rb->readPtr = rb->writePtr = 0; rb->readPtr = rb->writePtr = 0;
rb->buffer = newData;
} }
else if ((rb->writePtr >= rb->readPtr) && (rb->writePtr < targetSize)) else if ((rb->writePtr >= rb->readPtr) && (rb->writePtr < targetSize))
{ {
@ -118,6 +119,7 @@ static BOOL ringbuffer_realloc(RingBuffer *rb, size_t targetSize)
} }
rb->writePtr = rb->size - rb->freeSize; rb->writePtr = rb->size - rb->freeSize;
rb->readPtr = 0; rb->readPtr = 0;
free(rb->buffer);
rb->buffer = newData; rb->buffer = newData;
} }

View File

@ -75,6 +75,7 @@ BOOL test_overlaps(void)
if (ringbuffer_capacity(&rb) != 5) if (ringbuffer_capacity(&rb) != 5)
goto error; goto error;
ringbuffer_destroy(&rb);
return TRUE; return TRUE;
error: error:
ringbuffer_destroy(&rb); ringbuffer_destroy(&rb);
@ -220,6 +221,9 @@ int TestRingBuffer(int argc, char* argv[])
return -1; return -1;
} }
fprintf(stderr, "ok\n"); fprintf(stderr, "ok\n");
ringbuffer_destroy(&ringBuffer);
free(tmpBuf);
return 0; return 0;
} }