diff options
author | Monty <xiphmont@xiph.org> | 2009-05-19 23:27:38 +0000 |
---|---|---|
committer | Monty <xiphmont@xiph.org> | 2009-05-19 23:27:38 +0000 |
commit | 2847fef44c50bd277aed2200b8e06f7aabb34822 (patch) | |
tree | fb4a077fac9b41ecd266b9d2aac9be87401d23eb | |
parent | 19dbb37556979fa67a5830af3db9efb9503e6eb2 (diff) | |
download | ogg-2847fef44c50bd277aed2200b8e06f7aabb34822.tar.gz |
Update configure script to use new 'legal' names for cache vals (I hate you so much, autofoo)
Update ogg to check the return of all allocs for those on embedded
platforms with guarantees non-ovecommit. Be aware that these checks
are useless on any modern desktop OS, but that embedded folks with no
MMU and a hard heap boundary will benefit.
Add one new call to bitpacker that will query a write-mode
oggpack_buffer to see if it ran out of memory at some point; there was
no other way to add the error reporting in an ABI-compatible manner
(and it also allows the writepacker to avoid extra checks; it will
just keep going after running out of memory without faulting and state
can be queried later).
git-svn-id: http://svn.xiph.org/trunk/ogg@16016 0101bb08-14d6-0310-b084-bc0e0c8e3800
-rw-r--r-- | configure.in | 84 | ||||
-rw-r--r-- | include/ogg/ogg.h | 2 | ||||
-rw-r--r-- | src/bitwise.c | 63 | ||||
-rw-r--r-- | src/framing.c | 47 |
4 files changed, 127 insertions, 69 deletions
diff --git a/configure.in b/configure.in index 2957d76..d1185b6 100644 --- a/configure.in +++ b/configure.in @@ -83,7 +83,7 @@ AC_C_CONST dnl Check for types AC_MSG_CHECKING(for int16_t) -AC_CACHE_VAL(has_int16_t, +AC_CACHE_VAL(has_cv_int16_t, [AC_TRY_RUN([ #if defined __BEOS__ && !defined __HAIKU__ #include <inttypes.h> @@ -92,14 +92,14 @@ AC_CACHE_VAL(has_int16_t, int16_t foo; int main() {return 0;} ], -has_int16_t=yes, -has_int16_t=no, -has_int16_t=no +has_cv_int16_t=yes, +has_cv_int16_t=no, +has_cv_int16_t=no )]) -AC_MSG_RESULT($has_int16_t) +AC_MSG_RESULT($has_cv_int16_t) AC_MSG_CHECKING(for int32_t) -AC_CACHE_VAL(has_int32_t, +AC_CACHE_VAL(has_cv_int32_t, [AC_TRY_RUN([ #if defined __BEOS__ && !defined __HAIKU__ #include <inttypes.h> @@ -108,14 +108,14 @@ AC_CACHE_VAL(has_int32_t, int32_t foo; int main() {return 0;} ], -has_int32_t=yes, -has_int32_t=no, -has_int32_t=no +has_cv_int32_t=yes, +has_cv_int32_t=no, +has_cv_int32_t=no )]) -AC_MSG_RESULT($has_int32_t) +AC_MSG_RESULT($has_cv_int32_t) AC_MSG_CHECKING(for uint32_t) -AC_CACHE_VAL(has_uint32_t, +AC_CACHE_VAL(has_cv_uint32_t, [AC_TRY_RUN([ #if defined __BEOS__ && !defined __HAIKU__ #include <inttypes.h> @@ -124,14 +124,14 @@ AC_CACHE_VAL(has_uint32_t, uint32_t foo; int main() {return 0;} ], -has_uint32_t=yes, -has_uint32_t=no, -has_uint32_t=no +has_cv_uint32_t=yes, +has_cv_uint32_t=no, +has_cv_uint32_t=no )]) -AC_MSG_RESULT($has_uint32_t) +AC_MSG_RESULT($has_cv_uint32_t) AC_MSG_CHECKING(for uint16_t) -AC_CACHE_VAL(has_uint16_t, +AC_CACHE_VAL(has_cv_uint16_t, [AC_TRY_RUN([ #if defined __BEOS__ && !defined __HAIKU__ #include <inttypes.h> @@ -140,14 +140,14 @@ AC_CACHE_VAL(has_uint16_t, uint16_t foo; int main() {return 0;} ], -has_uint16_t=yes, -has_uint16_t=no, -has_uint16_t=no +has_cv_uint16_t=yes, +has_cv_uint16_t=no, +has_cv_uint16_t=no )]) -AC_MSG_RESULT($has_uint16_t) +AC_MSG_RESULT($has_cv_uint16_t) AC_MSG_CHECKING(for u_int32_t) -AC_CACHE_VAL(has_u_int32_t, +AC_CACHE_VAL(has_cv_u_int32_t, [AC_TRY_RUN([ #if defined __BEOS__ && !defined __HAIKU__ #include <inttypes.h> @@ -156,14 +156,14 @@ AC_CACHE_VAL(has_u_int32_t, u_int32_t foo; int main() {return 0;} ], -has_u_int32_t=yes, -has_u_int32_t=no, -has_u_int32_t=no +has_cv_u_int32_t=yes, +has_cv_u_int32_t=no, +has_cv_u_int32_t=no )]) -AC_MSG_RESULT($has_u_int32_t) +AC_MSG_RESULT($has_cv_u_int32_t) AC_MSG_CHECKING(for u_int16_t) -AC_CACHE_VAL(has_u_int16_t, +AC_CACHE_VAL(has_cv_u_int16_t, [AC_TRY_RUN([ #if defined __BEOS__ && !defined __HAIKU__ #include <inttypes.h> @@ -172,14 +172,14 @@ AC_CACHE_VAL(has_u_int16_t, u_int16_t foo; int main() {return 0;} ], -has_u_int16_t=yes, -has_u_int16_t=no, -has_u_int16_t=no +has_cv_u_int16_t=yes, +has_cv_u_int16_t=no, +has_cv_u_int16_t=no )]) -AC_MSG_RESULT($has_u_int16_t) +AC_MSG_RESULT($has_cv_u_int16_t) AC_MSG_CHECKING(for int64_t) -AC_CACHE_VAL(has_int64_t, +AC_CACHE_VAL(has_cv_int64_t, [AC_TRY_RUN([ #if defined __BEOS__ && !defined __HAIKU__ #include <inttypes.h> @@ -188,11 +188,11 @@ AC_CACHE_VAL(has_int64_t, int64_t foo; int main() {return 0;} ], -has_int64_t=yes, -has_int64_t=no, -has_int64_t=no +has_cv_int64_t=yes, +has_cv_int64_t=no, +has_cv_int64_t=no )]) -AC_MSG_RESULT($has_int64_t) +AC_MSG_RESULT($has_cv_int64_t) AC_CHECK_SIZEOF(short,2) AC_CHECK_SIZEOF(int,4) @@ -200,7 +200,7 @@ AC_CHECK_SIZEOF(long,4) AC_CHECK_SIZEOF(long long,8) -if test x$has_int16_t = "xyes" ; then +if test x$has_cv_int16_t = "xyes" ; then SIZE16="int16_t" else case 2 in @@ -209,7 +209,7 @@ else esac fi -if test x$has_int32_t = "xyes" ; then +if test x$has_cv_int32_t = "xyes" ; then SIZE32="int32_t" else case 4 in @@ -219,10 +219,10 @@ else esac fi -if test x$has_uint32_t = "xyes" ; then +if test x$has_cv_uint32_t = "xyes" ; then USIZE32="uint32_t" else - if test x$has_u_int32_t = "xyes" ; then + if test x$has_cv_u_int32_t = "xyes" ; then USIZE32="u_int32_t" else case 4 in @@ -233,10 +233,10 @@ else fi fi -if test x$has_uint16_t = "xyes" ; then +if test x$has_cv_uint16_t = "xyes" ; then USIZE16="uint16_t" else - if test x$has_u_int16_t = "xyes" ; then + if test x$has_cv_u_int16_t = "xyes" ; then USIZE16="u_int16_t" else case 2 in @@ -247,7 +247,7 @@ else fi fi -if test x$has_int64_t = "xyes" ; then +if test x$has_cv_int64_t = "xyes" ; then SIZE64="int64_t" else case 8 in diff --git a/include/ogg/ogg.h b/include/ogg/ogg.h index 33ae3da..9ff2c95 100644 --- a/include/ogg/ogg.h +++ b/include/ogg/ogg.h @@ -116,6 +116,7 @@ typedef struct { /* Ogg BITSTREAM PRIMITIVES: bitstream ************************/ extern void oggpack_writeinit(oggpack_buffer *b); +extern int oggpack_writecheck(oggpack_buffer *b); extern void oggpack_writetrunc(oggpack_buffer *b,long bits); extern void oggpack_writealign(oggpack_buffer *b); extern void oggpack_writecopy(oggpack_buffer *b,void *source,long bits); @@ -134,6 +135,7 @@ extern long oggpack_bits(oggpack_buffer *b); extern unsigned char *oggpack_get_buffer(oggpack_buffer *b); extern void oggpackB_writeinit(oggpack_buffer *b); +extern int oggpackB_writecheck(oggpack_buffer *b); extern void oggpackB_writetrunc(oggpack_buffer *b,long bits); extern void oggpackB_writealign(oggpack_buffer *b); extern void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits); diff --git a/src/bitwise.c b/src/bitwise.c index 380119e..25cf862 100644 --- a/src/bitwise.c +++ b/src/bitwise.c @@ -47,28 +47,48 @@ void oggpackB_writeinit(oggpack_buffer *b){ oggpack_writeinit(b); } +int oggpack_writecheck(oggpack_buffer *b){ + if(!b->ptr || !b->storage)return -1; + return 0; +} + +int oggpackB_writecheck(oggpack_buffer *b){ + return oggpackB_writecheck(b); +} + void oggpack_writetrunc(oggpack_buffer *b,long bits){ long bytes=bits>>3; - bits-=bytes*8; - b->ptr=b->buffer+bytes; - b->endbit=bits; - b->endbyte=bytes; - *b->ptr&=mask[bits]; + if(b->ptr){ + bits-=bytes*8; + b->ptr=b->buffer+bytes; + b->endbit=bits; + b->endbyte=bytes; + *b->ptr&=mask[bits]; + } } void oggpackB_writetrunc(oggpack_buffer *b,long bits){ long bytes=bits>>3; - bits-=bytes*8; - b->ptr=b->buffer+bytes; - b->endbit=bits; - b->endbyte=bytes; - *b->ptr&=mask8B[bits]; + if(b->ptr){ + bits-=bytes*8; + b->ptr=b->buffer+bytes; + b->endbit=bits; + b->endbyte=bytes; + *b->ptr&=mask8B[bits]; + } } /* Takes only up to 32 bits. */ void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){ if(b->endbyte+4>=b->storage){ - b->buffer=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT); + void *ret; + if(!b->ptr)return; + ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT); + if(!ret){ + oggpack_writeclear(b); + return; + } + b->buffer=ret; b->storage+=BUFFER_INCREMENT; b->ptr=b->buffer+b->endbyte; } @@ -102,7 +122,14 @@ void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){ /* Takes only up to 32 bits. */ void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){ if(b->endbyte+4>=b->storage){ - b->buffer=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT); + void *ret; + if(!b->ptr)return; + ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT); + if(!ret){ + oggpack_writeclear(b); + return; + } + b->buffer=ret; b->storage+=BUFFER_INCREMENT; b->ptr=b->buffer+b->endbyte; } @@ -165,8 +192,15 @@ static void oggpack_writecopy_helper(oggpack_buffer *b, }else{ /* aligned block copy */ if(b->endbyte+bytes+1>=b->storage){ + void *ret; + if(!b->ptr)return; b->storage=b->endbyte+bytes+BUFFER_INCREMENT; - b->buffer=_ogg_realloc(b->buffer,b->storage); + ret=_ogg_realloc(b->buffer,b->storage); + if(!ret){ + oggpack_writeclear(b); + return; + } + b->buffer=ret; b->ptr=b->buffer+b->endbyte; } @@ -193,6 +227,7 @@ void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){ } void oggpack_reset(oggpack_buffer *b){ + if(!b->ptr)return; b->ptr=b->buffer; b->buffer[0]=0; b->endbit=b->endbyte=0; @@ -203,7 +238,7 @@ void oggpackB_reset(oggpack_buffer *b){ } void oggpack_writeclear(oggpack_buffer *b){ - _ogg_free(b->buffer); + if(b->buffer)_ogg_free(b->buffer); memset(b,0,sizeof(*b)); } diff --git a/src/framing.c b/src/framing.c index 59049f0..d202d62 100644 --- a/src/framing.c +++ b/src/framing.c @@ -189,12 +189,17 @@ int ogg_stream_init(ogg_stream_state *os,int serialno){ if(os){ memset(os,0,sizeof(*os)); os->body_storage=16*1024; - os->body_data=_ogg_malloc(os->body_storage*sizeof(*os->body_data)); - os->lacing_storage=1024; + + os->body_data=_ogg_malloc(os->body_storage*sizeof(*os->body_data)); os->lacing_vals=_ogg_malloc(os->lacing_storage*sizeof(*os->lacing_vals)); os->granule_vals=_ogg_malloc(os->lacing_storage*sizeof(*os->granule_vals)); + if(!os->body_data || !os->lacing_vals || !os->granule_vals){ + ogg_stream_clear(os); + return -1; + } + os->serialno=serialno; return(0); @@ -225,19 +230,32 @@ int ogg_stream_destroy(ogg_stream_state *os){ /* Helpers for ogg_stream_encode; this keeps the structure and what's happening fairly clear */ -static void _os_body_expand(ogg_stream_state *os,int needed){ +static int _os_body_expand(ogg_stream_state *os,int needed){ if(os->body_storage<=os->body_fill+needed){ + void *ret; + ret=_ogg_realloc(os->body_data,(os->body_storage+needed+1024)* + sizeof(*os->body_data)); + if(!ret) return -1; os->body_storage+=(needed+1024); - os->body_data=_ogg_realloc(os->body_data,os->body_storage*sizeof(*os->body_data)); + os->body_data=ret; } + return 0; } -static void _os_lacing_expand(ogg_stream_state *os,int needed){ +static int _os_lacing_expand(ogg_stream_state *os,int needed){ if(os->lacing_storage<=os->lacing_fill+needed){ + void *ret; + ret=_ogg_realloc(os->lacing_vals,(os->lacing_storage+needed+32)* + sizeof(*os->lacing_vals)); + if(!ret)return -1; + os->lacing_vals=ret; + ret=_ogg_realloc(os->granule_vals,(os->lacing_storage+needed+32)* + sizeof(*os->granule_vals)); + if(!ret)return -1; + os->granule_vals=ret; os->lacing_storage+=(needed+32); - os->lacing_vals=_ogg_realloc(os->lacing_vals,os->lacing_storage*sizeof(*os->lacing_vals)); - os->granule_vals=_ogg_realloc(os->granule_vals,os->lacing_storage*sizeof(*os->granule_vals)); } + return 0; } /* checksum the page */ @@ -288,8 +306,8 @@ int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, int count, } /* make sure we have the buffer storage */ - _os_body_expand(os,bytes); - _os_lacing_expand(os,lacing_vals); + if(_os_body_expand(os,bytes))return -1; + if(_os_lacing_expand(os,lacing_vals))return -1; /* Copy in the submitted packet. Yes, the copy is a waste; this is the liability of overly clean abstraction for the time being. It @@ -528,11 +546,14 @@ char *ogg_sync_buffer(ogg_sync_state *oy, long size){ if(size>oy->storage-oy->fill){ /* We need to extend the internal buffer */ long newsize=size+oy->fill+4096; /* an extra page to be nice */ + void *ret; if(oy->data) - oy->data=_ogg_realloc(oy->data,newsize); + ret=_ogg_realloc(oy->data,newsize); else - oy->data=_ogg_malloc(newsize); + ret=_ogg_malloc(newsize); + if(!ret)return NULL; + oy->data=ret; oy->storage=newsize; } @@ -729,7 +750,7 @@ int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){ if(serialno!=os->serialno)return(-1); if(version>0)return(-1); - _os_lacing_expand(os,segments+1); + if(_os_lacing_expand(os,segments+1))return -1; /* are we in sequence? */ if(pageno!=os->pageno){ @@ -766,7 +787,7 @@ int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){ } if(bodysize){ - _os_body_expand(os,bodysize); + if(_os_body_expand(os,bodysize))return -1; memcpy(os->body_data+os->body_fill,body,bodysize); os->body_fill+=bodysize; } |