From 7c6531cee04e3b3956a2c01eadfd72548bd357e0 Mon Sep 17 00:00:00 2001 From: Monty Date: Thu, 24 Apr 2014 20:59:56 +0000 Subject: Correct oggpack_writecopy bug reported by Ian Nartowicz: Integer overflow checking in oggpack_writecopy_helper got the reallocation size test condition backwards and so would error out when it needed to expand the destination's internal buffer. At the same time, do preexpansion of both aligned and unaligned copies to avoid possible heap thrashing in the unaligned case. Add black and glass box unit tests for oggpack_writecopy and oggpackB_writecopy. git-svn-id: http://svn.xiph.org/trunk/ogg@19119 0101bb08-14d6-0310-b084-bc0e0c8e3800 --- src/bitwise.c | 261 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 246 insertions(+), 15 deletions(-) diff --git a/src/bitwise.c b/src/bitwise.c index eea7858..ab2e58c 100644 --- a/src/bitwise.c +++ b/src/bitwise.c @@ -187,8 +187,22 @@ static void oggpack_writecopy_helper(oggpack_buffer *b, unsigned char *ptr=(unsigned char *)source; long bytes=bits/8; + long pbytes=(b->endbit+bits)/8; bits-=bytes*8; + /* expand storage up-front */ + if(b->endbyte+pbytes>=b->storage){ + void *ret; + if(!b->ptr) goto err; + if(b->storage>b->endbyte+pbytes+BUFFER_INCREMENT) goto err; + b->storage=b->endbyte+pbytes+BUFFER_INCREMENT; + ret=_ogg_realloc(b->buffer,b->storage); + if(!ret) goto err; + b->buffer=ret; + b->ptr=b->buffer+b->endbyte; + } + + /* copy whole octets */ if(b->endbit){ int i; /* unaligned copy. Do it the hard way. */ @@ -196,23 +210,13 @@ static void oggpack_writecopy_helper(oggpack_buffer *b, w(b,(unsigned long)(ptr[i]),8); }else{ /* aligned block copy */ - if(b->endbyte+bytes+1>=b->storage){ - void *ret; - if(!b->ptr) goto err; - if(b->endbyte+bytes+BUFFER_INCREMENT>b->storage) goto err; - b->storage=b->endbyte+bytes+BUFFER_INCREMENT; - ret=_ogg_realloc(b->buffer,b->storage); - if(!ret) goto err; - b->buffer=ret; - b->ptr=b->buffer+b->endbyte; - } - memmove(b->ptr,source,bytes); b->ptr+=bytes; b->endbyte+=bytes; *b->ptr=0; - } + + /* copy trailing bits */ if(bits){ if(msb) w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits); @@ -613,9 +617,190 @@ void cliptestB(unsigned long *b,int vals,int bits,int *comp,int compsize){ if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n"); } +void copytest(int prefill, int copy){ + oggpack_buffer source_write; + oggpack_buffer dest_write; + oggpack_buffer source_read; + oggpack_buffer dest_read; + unsigned char *source; + unsigned char *dest; + long source_bytes,dest_bytes; + int i; + + oggpack_writeinit(&source_write); + oggpack_writeinit(&dest_write); + + for(i=0;i<(prefill+copy+7)/8;i++) + oggpack_write(&source_write,(i^0x5a)&0xff,8); + source=oggpack_get_buffer(&source_write); + source_bytes=oggpack_bytes(&source_write); + + /* prefill */ + oggpack_writecopy(&dest_write,source,prefill); + + /* check buffers; verify end byte masking */ + dest=oggpack_get_buffer(&dest_write); + dest_bytes=oggpack_bytes(&dest_write); + if(dest_bytes!=(prefill+7)/8){ + fprintf(stderr,"wrong number of bytes after prefill! %d!=%d\n",dest_bytes,(prefill+7)/8); + exit(1); + } + oggpack_readinit(&source_read,source,source_bytes); + oggpack_readinit(&dest_read,dest,dest_bytes); + + for(i=0;i