diff --git a/src/bin/jpip/opj_dec_server.c b/src/bin/jpip/opj_dec_server.c index f0b7fc13..da9d7e64 100644 --- a/src/bin/jpip/opj_dec_server.c +++ b/src/bin/jpip/opj_dec_server.c @@ -59,7 +59,7 @@ int main(int argc, char *argv[]){ dec_server_record_t *server_record; client_t client; int port = 50000; - int erreur; + int erreur = 0; (void)erreur; if( argc > 1) diff --git a/src/bin/jpip/opj_jpip_addxml.c b/src/bin/jpip/opj_jpip_addxml.c index 626fc5d1..52dfac2c 100644 --- a/src/bin/jpip/opj_jpip_addxml.c +++ b/src/bin/jpip/opj_jpip_addxml.c @@ -121,6 +121,11 @@ FILE * open_jp2file( const char filename[]) } data = (char *)malloc( 12); /* size of header */ + if(data == NULL){ + fprintf(stderr,"open_jp2file: memory out\n"); + fclose(fp); + return NULL; + } if( fread( data, 12, 1, fp) != 1){ free( data); fclose(fp); @@ -170,7 +175,11 @@ char * read_xmlfile( const char filename[], long *fsize) } data = (char *)malloc( (size_t)*fsize); - + if(data == NULL){ + fprintf(stderr, "read_xmlfile: memory out\n"); + fclose(fp); + return NULL; + } if( fread( data, (size_t)*fsize, 1, fp) != 1){ fprintf( stderr, "XML file %s broken (read error)\n", filename); free( data); diff --git a/src/bin/jpip/opj_jpip_transcode.c b/src/bin/jpip/opj_jpip_transcode.c index 19570af5..89a41b0f 100644 --- a/src/bin/jpip/opj_jpip_transcode.c +++ b/src/bin/jpip/opj_jpip_transcode.c @@ -29,6 +29,7 @@ */ #include +#include #include "openjpip.h" /*! \file diff --git a/src/lib/openjpip/CMakeLists.txt b/src/lib/openjpip/CMakeLists.txt index 858a1095..0583e6dd 100644 --- a/src/lib/openjpip/CMakeLists.txt +++ b/src/lib/openjpip/CMakeLists.txt @@ -80,7 +80,7 @@ install(TARGETS openjpip if(BUILD_JPIP_SERVER) add_library(openjpip_server STATIC ${OPENJPIP_SRCS} ${SERVER_SRCS}) - target_link_libraries(openjpip_server ${FCGI_LIBRARIES} ${CURL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries(openjpip_server ${FCGI_LIBRARIES} ${CURL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${OPENJPEG_LIBRARY_NAME}) set_target_properties(openjpip_server PROPERTIES COMPILE_FLAGS "-DSERVER") install(TARGETS openjpip_server diff --git a/src/lib/openjpip/box_manager.c b/src/lib/openjpip/box_manager.c index 85b3a948..436d2b0d 100644 --- a/src/lib/openjpip/box_manager.c +++ b/src/lib/openjpip/box_manager.c @@ -49,8 +49,9 @@ boxlist_param_t * gene_boxlist(void) { boxlist_param_t *boxlist; - boxlist = (boxlist_param_t *)malloc( sizeof(boxlist_param_t)); - + boxlist = (boxlist_param_t *)opj_malloc( sizeof(boxlist_param_t)); + if(boxlist == NULL) return NULL; + boxlist->first = NULL; boxlist->last = NULL; @@ -73,8 +74,10 @@ boxlist_param_t * get_boxstructure( int fd, OPJ_OFF_T offset, OPJ_SIZE_T length) assert( (OPJ_OFF_T)box->length >= 0); pos += (OPJ_OFF_T)box->length; - if( !boxlist) + if( !boxlist){ boxlist = gene_boxlist(); + if(boxlist == NULL) break; + } insert_box_into_list( box, boxlist); }while( pos < offset+(OPJ_OFF_T)length); @@ -103,7 +106,7 @@ box_param_t * gene_boxbyOffset( int fd, OPJ_OFF_T offset) if( !isalpha(boxtype[0]) || !isalpha(boxtype[1]) || (!isalnum(boxtype[2])&&!isspace(boxtype[2])) || (!isalpha(boxtype[3])&&!isspace(boxtype[3]))){ - free( data); + opj_free( data); return NULL; } @@ -113,22 +116,26 @@ box_param_t * gene_boxbyOffset( int fd, OPJ_OFF_T offset) /* read XLBox*/ if((data2 = fetch_bytes( fd, offset+8, 8))){ boxlen = big8(data2); - free(data2); + opj_free(data2); } else{ fprintf( FCGI_stderr, "Error: error in gene_boxbyOffset( %d, %" PRId64 ")\n", fd, offset); - free( data); + opj_free( data); return NULL; } } - box = (box_param_t *)malloc( sizeof( box_param_t)); + box = (box_param_t *)opj_malloc( sizeof( box_param_t)); + if(box == NULL){ + opj_free(data); + return NULL; + } box->fd = fd; box->offset = offset; box->headlen = headlen; box->length = boxlen; strncpy( box->type, boxtype, 4); box->next = NULL; - free( data); + opj_free( data); return box; } @@ -155,7 +162,9 @@ box_param_t * gene_boxbyOffinStream( Byte_t *stream, OPJ_OFF_T offset) headlen = 16; boxlen = big8( stream+8); /* read XLBox*/ } - box = (box_param_t *)malloc( sizeof( box_param_t)); + box = (box_param_t *)opj_malloc( sizeof( box_param_t)); + if(box == NULL) return NULL; + box->fd = -1; box->offset = offset; box->headlen = headlen; @@ -202,7 +211,7 @@ box_param_t * gene_boxbyType( int fd, OPJ_OFF_T offset, OPJ_SIZE_T length, const /* read XLBox*/ if((data2 = fetch_bytes( fd, pos+8, 8))){ boxlen = big8(data2); - free(data2); + opj_free(data2); } else{ fprintf( FCGI_stderr, "Error: error in gene_boxbyType( %d, %" PRId64 ", %" PRId64 ", %s)\n", fd, offset, length, TBox); @@ -210,17 +219,21 @@ box_param_t * gene_boxbyType( int fd, OPJ_OFF_T offset, OPJ_SIZE_T length, const } } if( strncmp ( boxtype, TBox, 4) == 0){ - foundbox = (box_param_t *)malloc( sizeof( box_param_t)); + foundbox = (box_param_t *)opj_malloc( sizeof( box_param_t)); + if(foundbox == NULL){ + opj_free(data); + return NULL; + } foundbox->fd = fd; foundbox->offset = pos; foundbox->headlen = headlen; foundbox->length = boxlen; strncpy( foundbox->type, TBox, 4); foundbox->next = NULL; - free( data); + opj_free( data); return foundbox; } - free( data); + opj_free( data); } else{ fprintf( FCGI_stderr, "Error: error in gene_boxbyType( %d, %" PRId64 ", %" PRId64 ", %s)\n", fd, offset, length, TBox); @@ -261,7 +274,9 @@ box_param_t * gene_boxbyTypeinStream( Byte_t *stream, OPJ_OFF_T offset, OPJ_SIZE } if( strncmp ( boxtype, TBox, 4) == 0){ - foundbox = (box_param_t *)malloc( sizeof( box_param_t)); + foundbox = (box_param_t *)opj_malloc( sizeof( box_param_t)); + if(foundbox == NULL) return NULL; + foundbox->fd = -1; foundbox->offset = pos; foundbox->headlen = headlen; @@ -397,7 +412,7 @@ void delete_box_in_list( box_param_t **box, boxlist_param_t *boxlist) if( *box == boxlist->last) boxlist->last = ptr; } - free( *box); + opj_free( *box); } void delete_box_in_list_by_type( const char type[], boxlist_param_t *boxlist) @@ -418,10 +433,10 @@ void delete_boxlist( boxlist_param_t **boxlist) boxPtr = (*boxlist)->first; while( boxPtr != NULL){ boxNext=boxPtr->next; - free( boxPtr); + opj_free( boxPtr); boxPtr=boxNext; } - free( *boxlist); + opj_free( *boxlist); } void insert_box_into_list( box_param_t *box, boxlist_param_t *boxlist) diff --git a/src/lib/openjpip/box_manager.h b/src/lib/openjpip/box_manager.h index d45d84e6..c32289b5 100644 --- a/src/lib/openjpip/box_manager.h +++ b/src/lib/openjpip/box_manager.h @@ -31,6 +31,7 @@ #ifndef BOX_MANAGER_H_ # define BOX_MANAGER_H_ +#include "opj_includes.h" #include "byte_manager.h" /** box parameters*/ diff --git a/src/lib/openjpip/boxheader_manager.c b/src/lib/openjpip/boxheader_manager.c index 3b97c6df..fcf217b9 100644 --- a/src/lib/openjpip/boxheader_manager.c +++ b/src/lib/openjpip/boxheader_manager.c @@ -61,13 +61,15 @@ boxheader_param_t * gene_boxheader( int fd, OPJ_OFF_T offset) headlen = 16; } - boxheader = (boxheader_param_t *)malloc( sizeof( boxheader_param_t)); + boxheader = (boxheader_param_t *)opj_malloc( sizeof( boxheader_param_t)); + if(boxheader == NULL) return NULL; + boxheader->headlen = headlen; boxheader->length = boxlen; strncpy( boxheader->type, boxtype, 4); boxheader->next = NULL; - free( boxtype); + opj_free( boxtype); return boxheader; } diff --git a/src/lib/openjpip/boxheader_manager.h b/src/lib/openjpip/boxheader_manager.h index 81e8288b..14328993 100644 --- a/src/lib/openjpip/boxheader_manager.h +++ b/src/lib/openjpip/boxheader_manager.h @@ -31,6 +31,7 @@ #ifndef BOXHEADER_MANAGER_H_ # define BOXHEADER_MANAGER_H_ +#include "opj_includes.h" #include "openjpeg.h" #include "byte_manager.h" #include "box_manager.h" diff --git a/src/lib/openjpip/byte_manager.c b/src/lib/openjpip/byte_manager.c index 75537424..f53d4ef4 100644 --- a/src/lib/openjpip/byte_manager.c +++ b/src/lib/openjpip/byte_manager.c @@ -59,10 +59,11 @@ Byte_t * fetch_bytes( int fd, OPJ_OFF_T offset, OPJ_SIZE_T size) return NULL; } - data = (Byte_t *)malloc( size); + data = (Byte_t *)opj_malloc( size); + if(data == NULL) return NULL; if( (OPJ_SIZE_T)read( fd, data, size) != size){ - free( data); + opj_free( data); fprintf( FCGI_stdout, "Reason: Target broken (read error)\r\n"); fprintf( FCGI_stderr, "Error: error in fetch_bytes( %d, %ld, %lu)\n", fd, offset, size); return NULL; @@ -98,7 +99,7 @@ Byte2_t fetch_2bytebigendian( int fd, OPJ_OFF_T offset) return 0; } code = big2(data); - free( data); + opj_free( data); return code; } @@ -113,7 +114,7 @@ Byte4_t fetch_4bytebigendian( int fd, OPJ_OFF_T offset) return 0; } code = big4(data); - free( data); + opj_free( data); return code; } @@ -128,7 +129,7 @@ Byte8_t fetch_8bytebigendian( int fd, OPJ_OFF_T offset) return 0; } code = big8(data); - free( data); + opj_free( data); return code; } diff --git a/src/lib/openjpip/byte_manager.h b/src/lib/openjpip/byte_manager.h index 16091ece..d6f03709 100644 --- a/src/lib/openjpip/byte_manager.h +++ b/src/lib/openjpip/byte_manager.h @@ -32,6 +32,7 @@ #define BYTE_MANAGER_H_ #include +#include "opj_includes.h" #include "openjpeg.h" #include "opj_stdint.h" typedef uint8_t Byte_t; diff --git a/src/lib/openjpip/cache_manager.c b/src/lib/openjpip/cache_manager.c index 56b10465..3bdbf1cb 100644 --- a/src/lib/openjpip/cache_manager.c +++ b/src/lib/openjpip/cache_manager.c @@ -37,8 +37,9 @@ cachelist_param_t * gene_cachelist(void) { cachelist_param_t *cachelist; - cachelist = (cachelist_param_t *)malloc( sizeof(cachelist_param_t)); - + cachelist = (cachelist_param_t *)opj_malloc( sizeof(cachelist_param_t)); + if(cachelist == NULL) return NULL; + cachelist->first = NULL; cachelist->last = NULL; @@ -55,19 +56,29 @@ void delete_cachelist(cachelist_param_t **cachelist) delete_cache( &cachePtr); cachePtr=cacheNext; } - free( *cachelist); + opj_free( *cachelist); } cache_param_t * gene_cache( const char *targetname, int csn, char *tid, char *cid) { cache_param_t *cache; - cache = (cache_param_t *)malloc( sizeof(cache_param_t)); + cache = (cache_param_t *)opj_malloc( sizeof(cache_param_t)); + if(cache == NULL) return NULL; + cache->filename = strdup( targetname); + if(cache->filename == NULL) goto fails; + cache->tid = strdup( tid); + if(cache->tid == NULL) goto fails; + cache->csn = csn; - cache->cid = (char **)malloc( sizeof(char *)); + cache->cid = (char **)opj_malloc( sizeof(char *)); + if(cache->cid == NULL) goto fails; + *cache->cid = strdup( cid); + if(*cache->cid == NULL) goto fails; + cache->numOfcid = 1; #if 1 cache->metadatalist = NULL; @@ -78,23 +89,29 @@ cache_param_t * gene_cache( const char *targetname, int csn, char *tid, char *ci cache->next = NULL; return cache; + +fails: + delete_cache(&cache); + return NULL; } void delete_cache( cache_param_t **cache) { int i; - free( (*cache)->filename); - free( (*cache)->tid); + if((*cache)->filename) opj_free( (*cache)->filename); + if((*cache)->tid) opj_free( (*cache)->tid); delete_metadatalist( &(*cache)->metadatalist); if((*cache)->ihdrbox) - free((*cache)->ihdrbox); - for( i=0; i<(*cache)->numOfcid; i++) - free( (*cache)->cid[i]); - free( (*cache)->cid); - free( *cache); + opj_free((*cache)->ihdrbox); + if((*cache)->cid){ + for( i=0; i<(*cache)->numOfcid; i++) + opj_free( (*cache)->cid[i]); + opj_free( (*cache)->cid); + } + opj_free( *cache); } void insert_cache_into_list( cache_param_t *cache, cachelist_param_t *cachelist) @@ -180,12 +197,14 @@ void add_cachecid( const char *cid, cache_param_t *cache) { if( !cid) return; - - if( (cache->cid = realloc( cache->cid, (OPJ_SIZE_T)(cache->numOfcid+1)*sizeof(char *))) == NULL){ + { + char **cid = cache->cid; + + if( (cache->cid = opj_realloc( cid, (OPJ_SIZE_T)(cache->numOfcid+1)*sizeof(char *))) == NULL){ fprintf( stderr, "failed to add new cid to cache table in add_cachecid()\n"); return; } - + } cache->cid[ cache->numOfcid] = strdup( cid); cache->numOfcid ++; @@ -198,7 +217,7 @@ void update_cachetid( const char *tid, cache_param_t *cache) if( tid[0] != '0' && strcmp( tid, cache->tid) !=0){ fprintf( stderr, "tid is updated to %s for %s\n", tid, cache->filename); - free( cache->tid); + opj_free( cache->tid); cache->tid = strdup( tid); } } @@ -232,16 +251,19 @@ void remove_cidInCache( const char *cid, cache_param_t *cache) tmp = cache->cid; - cache->cid = (char **)malloc( (OPJ_SIZE_T)(cache->numOfcid-1)*sizeof(char *)); - + cache->cid = (char **)opj_malloc( (OPJ_SIZE_T)(cache->numOfcid-1)*sizeof(char *)); + if(cache->cid == NULL){ + fprintf( stderr, "cid: memory out\n"); + return; + } for( i=0, j=0; inumOfcid; i++){ if( i != idx){ cache->cid[j] = strdup( tmp[i]); j++; } - free( tmp[i]); + opj_free( tmp[i]); } - free( tmp); + opj_free( tmp); cache->numOfcid --; } diff --git a/src/lib/openjpip/cachemodel_manager.c b/src/lib/openjpip/cachemodel_manager.c index da30d940..b4821e3b 100644 --- a/src/lib/openjpip/cachemodel_manager.c +++ b/src/lib/openjpip/cachemodel_manager.c @@ -49,7 +49,8 @@ cachemodellist_param_t * gene_cachemodellist(void) cachemodellist_param_t *cachemodellist; cachemodellist = (cachemodellist_param_t *)opj_malloc( sizeof(cachemodellist_param_t)); - + if(cachemodellist == NULL) return NULL; + cachemodellist->first = NULL; cachemodellist->last = NULL; @@ -66,6 +67,7 @@ cachemodel_param_t * gene_cachemodel( cachemodellist_param_t *cachemodellist, ta int i; cachemodel = (cachemodel_param_t *)opj_malloc( sizeof(cachemodel_param_t)); + if(cachemodel == NULL) return NULL; refer_target( target, &cachemodel->target); @@ -87,8 +89,20 @@ cachemodel_param_t * gene_cachemodel( cachemodellist_param_t *cachemodellist, ta numOftiles = get_m( tilepart); numOfelem = get_nmax( tilepart)*numOftiles; cachemodel->tp_model = (OPJ_BOOL *)opj_calloc( 1, numOfelem*sizeof(OPJ_BOOL)); + if(cachemodel->tp_model == NULL){ + delete_cachemodel(&cachemodel); + return NULL; + } cachemodel->th_model = (OPJ_BOOL *)opj_calloc( 1, numOftiles*sizeof(OPJ_BOOL)); + if(cachemodel->th_model == NULL){ + delete_cachemodel(&cachemodel); + return NULL; + } cachemodel->pp_model = (OPJ_BOOL **)opj_malloc( target->codeidx->SIZ.Csiz*sizeof(OPJ_BOOL *)); + if(cachemodel->pp_model == NULL){ + delete_cachemodel(&cachemodel); + return NULL; + } for( i=0; icodeidx->SIZ.Csiz; i++){ precpacket = target->codeidx->precpacket[i]; cachemodel->pp_model[i] = (OPJ_BOOL *)opj_calloc( 1, get_nmax(precpacket)*get_m(precpacket)*sizeof(OPJ_BOOL)); diff --git a/src/lib/openjpip/channel_manager.c b/src/lib/openjpip/channel_manager.c index b4344b92..bb846ddc 100644 --- a/src/lib/openjpip/channel_manager.c +++ b/src/lib/openjpip/channel_manager.c @@ -50,7 +50,8 @@ channellist_param_t * gene_channellist(void) channellist_param_t *channellist; channellist = (channellist_param_t *)opj_malloc( sizeof(channellist_param_t)); - + if(channellist == NULL) return NULL; + channellist->first = NULL; channellist->last = NULL; @@ -69,6 +70,8 @@ channel_param_t * gene_channel( query_param_t query_param, auxtrans_param_t auxt } channel = (channel_param_t *)opj_malloc( sizeof(channel_param_t)); + if(channel == NULL) return NULL; + channel->cachemodel = cachemodel; /* set channel ID and get present time */ diff --git a/src/lib/openjpip/dec_clientmsg_handler.c b/src/lib/openjpip/dec_clientmsg_handler.c index 693651e3..0d21f459 100644 --- a/src/lib/openjpip/dec_clientmsg_handler.c +++ b/src/lib/openjpip/dec_clientmsg_handler.c @@ -58,6 +58,12 @@ void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist opj_free( newjpipstream); metadatalist = gene_metadatalist(); + if(metadatalist == NULL){ + if( target) opj_free( target); + if( tid) opj_free( tid); + if( cid) opj_free( cid); + return; + } parse_metamsg( msgqueue, *jpipstream, *streamlen, metadatalist); assert( msgqueue->last ); @@ -143,6 +149,8 @@ void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_pa boxcontents = cache->metadatalist->last->boxcontents; xmlstream = (Byte_t *)opj_malloc( boxcontents->length); + if(xmlstream == NULL) return; + memcpy( xmlstream, jpipstream+boxcontents->offset, boxcontents->length); send_XMLstream( connected_socket, xmlstream, boxcontents->length); opj_free( xmlstream); @@ -220,8 +228,11 @@ void handle_SIZreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_par width = height = 0; if( cache){ assert( cache->csn >= 0); - if( !cache->ihdrbox) + if( !cache->ihdrbox){ cache->ihdrbox = get_SIZ_from_jpipstream( jpipstream, msgqueue, (Byte8_t)cache->csn); + + if(cache->ihdrbox == NULL) return; + } width = cache->ihdrbox->width; height = cache->ihdrbox->height; } diff --git a/src/lib/openjpip/faixbox_manager.c b/src/lib/openjpip/faixbox_manager.c index 714773c2..435c5c98 100644 --- a/src/lib/openjpip/faixbox_manager.c +++ b/src/lib/openjpip/faixbox_manager.c @@ -48,21 +48,24 @@ faixbox_param_t * gene_faixbox( box_param_t *box) size_t numOfelem; long pos = 0; - faix = ( faixbox_param_t *)malloc( sizeof(faixbox_param_t)); - + faix = ( faixbox_param_t *)opj_malloc( sizeof(faixbox_param_t)); + if(faix == NULL) return NULL; + faix->version = fetch_DBox1byte( box, (pos+=1)-1); if( 3< faix->version){ fprintf( FCGI_stderr, "Error: version %d in faix box is reserved for ISO use.\n", faix->version); - free(faix); - return NULL; + goto fails; } if( faix->version%2){ subfaixbox8_param_t *subfaixbox; size_t i; - faix->subfaixbox.byte8_params = (subfaixbox8_param_t *)malloc( sizeof(subfaixbox8_param_t)); + faix->subfaixbox.byte8_params = (subfaixbox8_param_t *)opj_malloc( sizeof(subfaixbox8_param_t)); + if(faix->subfaixbox.byte8_params == NULL){ + goto fails; + } subfaixbox = faix->subfaixbox.byte8_params; subfaixbox->nmax = fetch_DBox8bytebigendian( box, (pos+=8)-8); @@ -70,11 +73,17 @@ faixbox_param_t * gene_faixbox( box_param_t *box) numOfelem = subfaixbox->nmax*subfaixbox->m; - subfaixbox->elem = ( faixelem8_param_t *)malloc( numOfelem*sizeof(faixelem8_param_t)); - - if( faix->version == 3) - subfaixbox->aux = ( Byte4_t *)malloc( numOfelem*sizeof(Byte4_t)); + subfaixbox->elem = ( faixelem8_param_t *)opj_malloc( numOfelem*sizeof(faixelem8_param_t)); + if(subfaixbox->elem == NULL){ + goto fails; + } + if( faix->version == 3){ + subfaixbox->aux = ( Byte4_t *)opj_malloc( numOfelem*sizeof(Byte4_t)); + if(subfaixbox->aux == NULL){ + goto fails; + } + } for( i=0; ielem[i].off = fetch_DBox8bytebigendian( box, (pos+=8)-8); subfaixbox->elem[i].len = fetch_DBox8bytebigendian( box, (pos+=8)-8); @@ -86,19 +95,26 @@ faixbox_param_t * gene_faixbox( box_param_t *box) subfaixbox4_param_t *subfaixbox; size_t i; - faix->subfaixbox.byte4_params = (subfaixbox4_param_t *)malloc( sizeof(subfaixbox4_param_t)); - + faix->subfaixbox.byte4_params = (subfaixbox4_param_t *)opj_malloc( sizeof(subfaixbox4_param_t)); + if(faix->subfaixbox.byte4_params == NULL){ + goto fails; + } subfaixbox = faix->subfaixbox.byte4_params; subfaixbox->nmax = fetch_DBox4bytebigendian( box, (pos+=4)-4); subfaixbox->m = fetch_DBox4bytebigendian( box, (pos+=4)-4); numOfelem = subfaixbox->nmax*subfaixbox->m; - subfaixbox->elem = ( faixelem4_param_t *)malloc( numOfelem*sizeof(faixelem4_param_t)); - - if( faix->version == 2) - subfaixbox->aux = ( Byte4_t *)malloc( numOfelem*sizeof(Byte4_t)); - + subfaixbox->elem = ( faixelem4_param_t *)opj_malloc( numOfelem*sizeof(faixelem4_param_t)); + if(subfaixbox->elem == NULL){ + goto fails; + } + if( faix->version == 2){ + subfaixbox->aux = ( Byte4_t *)opj_malloc( numOfelem*sizeof(Byte4_t)); + if(subfaixbox->aux == NULL){ + goto fails; + } + } for( i=0; ielem[i].off = fetch_DBox4bytebigendian( box, (pos+=4)-4); subfaixbox->elem[i].len = fetch_DBox4bytebigendian( box, (pos+=4)-4); @@ -107,6 +123,10 @@ faixbox_param_t * gene_faixbox( box_param_t *box) } } return faix; + +fails: + delete_faixbox(&faix); + return NULL; } void print_faixbox( faixbox_param_t *faix) @@ -133,18 +153,18 @@ void print_faixbox( faixbox_param_t *faix) void delete_faixbox( faixbox_param_t **faix) { if((*faix)->version%2){ - free((*faix)->subfaixbox.byte8_params->elem); + opj_free((*faix)->subfaixbox.byte8_params->elem); if( (*faix)->version == 3) - free((*faix)->subfaixbox.byte8_params->aux); - free((*faix)->subfaixbox.byte8_params); + opj_free((*faix)->subfaixbox.byte8_params->aux); + opj_free((*faix)->subfaixbox.byte8_params); } else{ - free((*faix)->subfaixbox.byte4_params->elem); + opj_free((*faix)->subfaixbox.byte4_params->elem); if( (*faix)->version == 2) - free((*faix)->subfaixbox.byte4_params->aux); - free((*faix)->subfaixbox.byte4_params); + opj_free((*faix)->subfaixbox.byte4_params->aux); + opj_free((*faix)->subfaixbox.byte4_params); } - free( *faix); + opj_free( *faix); } Byte8_t get_nmax( faixbox_param_t *faix) diff --git a/src/lib/openjpip/ihdrbox_manager.c b/src/lib/openjpip/ihdrbox_manager.c index 686e0ece..052e6590 100644 --- a/src/lib/openjpip/ihdrbox_manager.c +++ b/src/lib/openjpip/ihdrbox_manager.c @@ -62,8 +62,9 @@ ihdrbox_param_t * gene_ihdrbox( metadatalist_param_t *metadatalist, Byte_t *jpip return NULL; } - ihdrbox = (ihdrbox_param_t *)malloc( sizeof(ihdrbox_param_t)); - + ihdrbox = (ihdrbox_param_t *)opj_malloc( sizeof(ihdrbox_param_t)); + if(ihdrbox == NULL) return NULL; + ihdrbox->height = big4( jpipstream+get_DBoxoff(ihdr)); ihdrbox->width = big4( jpipstream+get_DBoxoff(ihdr)+4); ihdrbox->nc = big2( jpipstream+get_DBoxoff(ihdr)+8); @@ -71,7 +72,7 @@ ihdrbox_param_t * gene_ihdrbox( metadatalist_param_t *metadatalist, Byte_t *jpip assert( bpc_val >= 0 && bpc_val <= 255 ); ihdrbox->bpc = (Byte_t)bpc_val; - free( ihdr); + opj_free( ihdr); return ihdrbox; } diff --git a/src/lib/openjpip/index_manager.c b/src/lib/openjpip/index_manager.c index b26d6d2a..8905905f 100644 --- a/src/lib/openjpip/index_manager.c +++ b/src/lib/openjpip/index_manager.c @@ -99,7 +99,8 @@ index_param_t * parse_jp2file( int fd) } jp2idx = (index_param_t *)opj_malloc( sizeof(index_param_t)); - + if(jp2idx == NULL) return NULL; + if( !set_cidxdata( cidx, jp2idx)){ fprintf( FCGI_stderr, "Error: Not correctl format in cidx box\n"); opj_free(jp2idx); @@ -493,12 +494,22 @@ OPJ_BOOL set_thixdata( box_param_t *cidx_box, index_param_t *jp2idx) } manf = gene_manfbox( manf_box); + if(manf == NULL){ + opj_free(manf_box); + opj_free( thix_box); + return OPJ_FALSE; + } ptr = manf->first; mhixseqoff = manf_box->offset+(OPJ_OFF_T)manf_box->length; pos = 0; tile_no = 0; jp2idx->tileheader = (mhixbox_param_t **)opj_malloc( jp2idx->SIZ.XTnum*jp2idx->SIZ.YTnum*sizeof(mhixbox_param_t *)); - + if(jp2idx->tileheader == NULL){ + delete_manfbox(&manf); + opj_free(manf); + opj_free(thix_box); + return OPJ_FALSE; + } while( ptr){ if( !(mhix_box = gene_boxbyType( thix_box->fd, mhixseqoff+(OPJ_OFF_T)pos, get_DBoxlen( thix_box)-manf_box->length-pos, "mhix"))){ fprintf( FCGI_stderr, "Error: mhix box not present in thix box\n"); @@ -547,25 +558,38 @@ OPJ_BOOL set_ppixdata( box_param_t *cidx_box, index_param_t *jp2idx) opj_free( ppix_box); manf = gene_manfbox( manf_box); + if(manf == NULL){ + return OPJ_FALSE; + } bh = search_boxheader( "faix", manf); inbox_offset = manf_box->offset + (OPJ_OFF_T)manf_box->length; opj_free( manf_box); jp2idx->precpacket = (faixbox_param_t **)opj_malloc( jp2idx->SIZ.Csiz*sizeof(faixbox_param_t *)); - + if(jp2idx->precpacket == NULL){ + delete_manfbox( &manf); + return OPJ_FALSE; + } for( comp_idx=0; bh!=NULL; bh=bh->next, comp_idx++){ if( jp2idx->SIZ.Csiz <= comp_idx ){ fprintf( FCGI_stderr, "Error: num of faix boxes is not identical to num of components in ppix box\n"); + delete_manfbox( &manf); return OPJ_FALSE; } if( !(faix_box = gene_boxbyOffset( cidx_box->fd, inbox_offset))){ fprintf( FCGI_stderr, "Error: faix box not present in ppix box\n"); + delete_manfbox( &manf); return OPJ_FALSE; } faix = gene_faixbox( faix_box); + if(faix == NULL){ + delete_manfbox( &manf); + opj_free( faix_box); + return OPJ_FALSE; + } jp2idx->precpacket[comp_idx] = faix; inbox_offset = faix_box->offset + (OPJ_OFF_T)faix_box->length; @@ -634,8 +658,13 @@ OPJ_BOOL set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestr if(COD->Scod & 0x01){ COD->XPsiz = (Byte4_t *)opj_malloc( (OPJ_SIZE_T)(COD->numOfdecomp+1)*sizeof(Byte4_t)); - COD->YPsiz = (Byte4_t *)opj_malloc( (OPJ_SIZE_T)(COD->numOfdecomp+1)*sizeof(Byte4_t)); + if(COD->XPsiz == NULL) return OPJ_FALSE; + COD->YPsiz = (Byte4_t *)opj_malloc( (OPJ_SIZE_T)(COD->numOfdecomp+1)*sizeof(Byte4_t)); + if(COD->YPsiz == NULL){ + opj_free(COD->XPsiz); COD->XPsiz = NULL; + return OPJ_FALSE; + } for( i=0; i<=COD->numOfdecomp; i++){ /*precinct size*/ COD->XPsiz[i] = (Byte2_t)pow( 2, fetch_marker1byte( codmkr, 12+i) & 0x0F); @@ -644,8 +673,13 @@ OPJ_BOOL set_CODmkrdata( markeridx_param_t *codmkidx, codestream_param_t codestr } else{ COD->XPsiz = (Byte4_t *)opj_malloc( sizeof(Byte4_t)); - COD->YPsiz = (Byte4_t *)opj_malloc( sizeof(Byte4_t)); + if(COD->XPsiz == NULL) return OPJ_FALSE; + COD->YPsiz = (Byte4_t *)opj_malloc( sizeof(Byte4_t)); + if(COD->YPsiz == NULL){ + opj_free(COD->XPsiz); COD->XPsiz = NULL; + return OPJ_FALSE; + } COD->XPsiz[0] = COD->YPsiz[0] = 1 << 15; /* pow(2,15); */ } return OPJ_TRUE; diff --git a/src/lib/openjpip/j2kheader_manager.c b/src/lib/openjpip/j2kheader_manager.c index 1e964516..88a0c41a 100644 --- a/src/lib/openjpip/j2kheader_manager.c +++ b/src/lib/openjpip/j2kheader_manager.c @@ -117,6 +117,7 @@ CODmarker_param_t get_CODmkrdata_from_j2kstream( Byte_t *CODstream) if( *CODstream++ != 0xff || *CODstream++ != 0x52){ fprintf( FCGI_stderr, "Error, COD marker not found in the reconstructed j2kstream\n"); + memset(&COD, 0, sizeof(CODmarker_param_t)); return COD; } @@ -128,8 +129,13 @@ CODmarker_param_t get_CODmkrdata_from_j2kstream( Byte_t *CODstream) if(COD.Scod & 0x01){ COD.XPsiz = (Byte4_t *)opj_malloc( (OPJ_SIZE_T)(COD.numOfdecomp+1)*sizeof(Byte4_t)); + if(COD.XPsiz == NULL) return COD;/* FIXME szukw000 */ + COD.YPsiz = (Byte4_t *)opj_malloc( (OPJ_SIZE_T)(COD.numOfdecomp+1)*sizeof(Byte4_t)); - + if(COD.YPsiz == NULL){ + opj_free(COD.XPsiz); COD.XPsiz = NULL; + return COD;/* FIXME szukw000 */ + } for( i=0; i<=COD.numOfdecomp; i++){ /*precinct size */ COD.XPsiz[i] = (Byte4_t)pow( 2, *( CODstream+12+i) & 0x0F); @@ -138,7 +144,13 @@ CODmarker_param_t get_CODmkrdata_from_j2kstream( Byte_t *CODstream) } else{ COD.XPsiz = (Byte4_t *)opj_malloc( sizeof(Byte4_t)); + if(COD.XPsiz == NULL) return COD; /* FIXME szukw000 */ + COD.YPsiz = (Byte4_t *)opj_malloc( sizeof(Byte4_t)); + if(COD.YPsiz == NULL){ + opj_free(COD.XPsiz); COD.XPsiz = NULL; + return COD; + } COD.XPsiz[0] = COD.YPsiz[0] = 1<<15; /*pow(2,15)*/ } return COD; @@ -208,7 +220,7 @@ Byte2_t modify_CODmkrstream( CODmarker_param_t COD, int numOfdecomp, Byte_t *COD { Byte2_t newLcod; - assert( numOfdecomp >= 0 || numOfdecomp <= 255 ); + assert( numOfdecomp >= 0 && numOfdecomp <= 255 ); if( *CODstream++ != 0xff || *CODstream++ != 0x52){ fprintf( FCGI_stderr, "Error, COD marker not found in the reconstructed j2kstream\n"); return 0; diff --git a/src/lib/openjpip/jp2k_decoder.c b/src/lib/openjpip/jp2k_decoder.c index 14e5970d..1b555339 100644 --- a/src/lib/openjpip/jp2k_decoder.c +++ b/src/lib/openjpip/jp2k_decoder.c @@ -174,7 +174,9 @@ static Byte_t * imagetopnm(opj_image_t *image, ihdrbox_param_t **ihdrbox) fprintf( stderr, "Exception: bits per component not identical, codestream: %d, ihdrbox: %d\n", image->comps[0].prec, (*ihdrbox)->bpc); } else{ - *ihdrbox = (ihdrbox_param_t *)malloc( sizeof(ihdrbox_param_t)); + *ihdrbox = (ihdrbox_param_t *)opj_malloc( sizeof(ihdrbox_param_t)); + if(*ihdrbox == NULL) return NULL; + (*ihdrbox)->width = image->comps[0].w; (*ihdrbox)->height = image->comps[0].h; assert( image->comps[0].prec < 256 ); @@ -208,7 +210,12 @@ static Byte_t * imagetopnm(opj_image_t *image, ihdrbox_param_t **ihdrbox) adjustB = 0; } - pix = (Byte_t *)malloc( datasize); + pix = (Byte_t *)opj_malloc( datasize); + if(pix == NULL){ + opj_free(*ihdrbox); + *ihdrbox = NULL; + return NULL; + } ptr = pix; for( i = 0; i < image->comps[0].w * image->comps[0].h; i++){ diff --git a/src/lib/openjpip/jp2k_encoder.c b/src/lib/openjpip/jp2k_encoder.c index 60eb1d16..46a2787e 100644 --- a/src/lib/openjpip/jp2k_encoder.c +++ b/src/lib/openjpip/jp2k_encoder.c @@ -106,14 +106,23 @@ Byte_t * recons_jp2( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte8_t csn while(( ptr = search_message( METADATA_MSG, (Byte8_t)-1, csn, ptr))!=NULL){ if( ptr->phld){ if( strncmp( (char *)ptr->phld->OrigBH+4, "jp2c", 4) == 0){ - jp2cDBoxOffset = *jp2len + ptr->phld->OrigBHlen; - jp2stream = add_emptyboxstream( ptr->phld, jp2stream, jp2len); /* header only */ - jp2cDBoxlen = *jp2len - jp2cDBoxOffset; + jp2cDBoxOffset = *jp2len + ptr->phld->OrigBHlen; + jp2stream = add_emptyboxstream( ptr->phld, jp2stream, jp2len); /* header only */ + if(jp2stream == NULL){ + return NULL; + } + jp2cDBoxlen = *jp2len - jp2cDBoxOffset; } else - jp2stream = add_emptyboxstream( ptr->phld, jp2stream, jp2len); /* header only */ + jp2stream = add_emptyboxstream( ptr->phld, jp2stream, jp2len); /* header only */ + if(jp2stream == NULL){ + return NULL; + } } jp2stream = add_msgstream( ptr, jpipstream, jp2stream, jp2len); + if(jp2stream == NULL){ + return NULL; + } ptr = ptr->next; } @@ -179,7 +188,9 @@ Byte_t * recons_codestream_from_JPTstream( msgqueue_param_t *msgqueue, Byte_t *j *j2klen = 0; j2kstream = add_mainhead_msgstream( msgqueue, jpipstream, j2kstream, csn, j2klen); - + if(j2kstream == NULL){ + return NULL; + } if( !get_mainheader_from_j2kstream( j2kstream, &SIZ, NULL)) return j2kstream; @@ -199,6 +210,9 @@ Byte_t * recons_codestream_from_JPTstream( msgqueue_param_t *msgqueue, Byte_t *j if( ptr->bin_offset == binOffset){ found = OPJ_TRUE; j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); + if(j2kstream == NULL){ + return NULL; + } binOffset += ptr->length; } ptr = ptr->next; @@ -209,13 +223,20 @@ Byte_t * recons_codestream_from_JPTstream( msgqueue_param_t *msgqueue, Byte_t *j if( ptr->bin_offset == binOffset){ found = OPJ_TRUE; j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); + if(j2kstream == NULL){ + return NULL; + } binOffset += ptr->length; } } ptr = ptr->next; } - if(!found) + if(!found){ j2kstream = add_emptytilestream( tileID, j2kstream, j2klen); + if(j2kstream == NULL){ + return NULL; + } + } } j2kstream = add_EOC( j2kstream, j2klen); @@ -243,7 +264,9 @@ Byte_t * recons_codestream_from_JPPstream( msgqueue_param_t *msgqueue, Byte_t *j *j2klen = 0; j2kstream = add_mainhead_msgstream( msgqueue, jpipstream, j2kstream, csn, j2klen); - + if(j2kstream == NULL){ + return NULL; + } if( !get_mainheader_from_j2kstream( j2kstream, &SIZ, &COD)) return j2kstream; @@ -263,8 +286,14 @@ Byte_t * recons_codestream_from_JPPstream( msgqueue_param_t *msgqueue, Byte_t *j SOToffset = *j2klen; while(( ptr = search_message( TILE_HEADER_MSG, tileID, csn, ptr))!=NULL){ if( ptr->bin_offset == binOffset){ - j2kstream = add_SOTmkr( j2kstream, j2klen); + j2kstream = add_SOTmkr( j2kstream, j2klen); + if(j2kstream == NULL){ + return NULL; + } j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); + if(j2kstream == NULL){ + return NULL; + } foundTH = OPJ_TRUE; binOffset += ptr->length; } @@ -275,8 +304,12 @@ Byte_t * recons_codestream_from_JPPstream( msgqueue_param_t *msgqueue, Byte_t *j j2kstream = recons_bitstream( msgqueue, jpipstream, j2kstream, csn, tileID, SIZ, COD, mindeclev, &max_reslev, j2klen); modify_tileheader( j2kstream, SOToffset, (max_reslevbin_offset == binOffset){ j2kstream = add_msgstream( ptr, origstream, j2kstream, j2klen); + if(j2kstream == NULL){ + return NULL; + } binOffset += ptr->length; } ptr = ptr->next; @@ -316,12 +352,13 @@ Byte_t * add_SOTmkr( Byte_t *j2kstream, Byte8_t *j2klen) buf = (Byte_t *)opj_malloc(( *j2klen)+2); + if(buf){ memcpy( buf, j2kstream, *j2klen); memcpy( buf+(*j2klen), &SOT, 2); *j2klen += 2; - - if(j2kstream) opj_free(j2kstream); + } + opj_free(j2kstream); return buf; } @@ -383,13 +420,18 @@ Byte_t * recons_LRCPbitstream( msgqueue_param_t *msgqueue, Byte_t *jpipstream, B for( l=0; lbin_offset == binOffset){ if( lay_idx == l){ j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); + if(j2kstream == NULL){ + return NULL; + } foundPrec = OPJ_TRUE; if( *max_reslev < res_idx) *max_reslev = res_idx; @@ -576,7 +627,9 @@ Byte_t * recons_precinct( msgqueue_param_t *msgqueue, Byte_t *jpipstream, Byte_t while(( ptr = search_message( PRECINCT_MSG, precID, csn, ptr))!=NULL){ if( ptr->bin_offset == binOffset){ j2kstream = add_msgstream( ptr, jpipstream, j2kstream, j2klen); - + if(j2kstream == NULL){ + return NULL; + } foundPrec = OPJ_TRUE; binOffset += ptr->length; if( *max_reslev < res_idx) @@ -659,16 +712,19 @@ Byte_t * add_msgstream( message_param_t *message, Byte_t *origstream, Byte_t *j2 return NULL; newstream = gene_msgstream( message, origstream, &newlen); - + if(newstream == NULL){ + return NULL; + } buf = (Byte_t *)opj_malloc(( *j2klen)+newlen); + if(buf){ memcpy( buf, j2kstream, *j2klen); memcpy( buf+(*j2klen), newstream, newlen); *j2klen += newlen; - + } opj_free( newstream); - if(j2kstream) opj_free(j2kstream); + opj_free(j2kstream); return buf; } @@ -678,7 +734,7 @@ Byte_t * add_emptyboxstream( placeholder_param_t *phld, Byte_t *jp2stream, Byte8 { Byte_t *newstream; Byte8_t newlen; - Byte_t *buf; + Byte_t *buf = NULL; if( phld->OrigBHlen == 8) newlen = big4(phld->OrigBH); @@ -686,18 +742,22 @@ Byte_t * add_emptyboxstream( placeholder_param_t *phld, Byte_t *jp2stream, Byte8 newlen = big8(phld->OrigBH+8); newstream = (Byte_t *)opj_malloc( newlen); - memset( newstream, 0, newlen); - memcpy( newstream, phld->OrigBH, phld->OrigBHlen); - buf = (Byte_t *)opj_malloc(( *jp2len)+newlen); + if(newstream){ + memset( newstream, 0, newlen); + memcpy( newstream, phld->OrigBH, phld->OrigBHlen); - memcpy( buf, jp2stream, *jp2len); - memcpy( buf+(*jp2len), newstream, newlen); + buf = (Byte_t *)opj_malloc(( *jp2len)+newlen); + + if(buf){ + memcpy( buf, jp2stream, *jp2len); + memcpy( buf+(*jp2len), newstream, newlen); - *jp2len += newlen; - - opj_free( newstream); - if(jp2stream) opj_free(jp2stream); + *jp2len += newlen; + } + opj_free( newstream); + } + opj_free(jp2stream); return buf; } @@ -706,19 +766,22 @@ Byte_t * add_emptytilestream( const Byte8_t tileID, Byte_t *j2kstream, Byte8_t * { Byte_t *newstream; Byte8_t newlen; - Byte_t *buf; + Byte_t *buf = NULL; newstream = gene_emptytilestream( tileID, &newlen); - buf = (Byte_t *)opj_malloc(( *j2klen)+newlen); + if(newstream){ + buf = (Byte_t *)opj_malloc(( *j2klen)+newlen); - memcpy( buf, j2kstream, *j2klen); - memcpy( buf+(*j2klen), newstream, newlen); - - *j2klen += newlen; - - opj_free( newstream); - if(j2kstream) opj_free(j2kstream); + if(buf){ + memcpy( buf, j2kstream, *j2klen); + memcpy( buf+(*j2klen), newstream, newlen); + + *j2klen += newlen; + } + opj_free( newstream); + } + opj_free(j2kstream); return buf; } @@ -729,12 +792,13 @@ Byte_t * add_padding( Byte8_t padding, Byte_t *j2kstream, Byte8_t *j2klen) buf = (Byte_t *)opj_malloc(( *j2klen)+padding); - memcpy( buf, j2kstream, *j2klen); - memset( buf+(*j2klen), 0, padding); + if(buf){ + memcpy( buf, j2kstream, *j2klen); + memset( buf+(*j2klen), 0, padding); - *j2klen += padding; - - if(j2kstream) opj_free(j2kstream); + *j2klen += padding; + } + opj_free(j2kstream); return buf; } @@ -747,12 +811,13 @@ Byte_t * add_EOC( Byte_t *j2kstream, Byte8_t *j2klen) buf = (Byte_t *)opj_malloc(( *j2klen)+2); - memcpy( buf, j2kstream, *j2klen); - memcpy( buf+(*j2klen), &EOC, 2); + if(buf){ + memcpy( buf, j2kstream, *j2klen); + memcpy( buf+(*j2klen), &EOC, 2); - *j2klen += 2; - - if(j2kstream) opj_free(j2kstream); + *j2klen += 2; + } + opj_free(j2kstream); return buf; } @@ -766,8 +831,10 @@ Byte_t * gene_msgstream( message_param_t *message, Byte_t *stream, Byte8_t *leng *length = message->length; buf = (Byte_t *)opj_malloc( *length); - memcpy( buf, stream+message->res_offset, *length); + if(buf){ + memcpy( buf, stream+message->res_offset, *length); + } return buf; } @@ -784,16 +851,17 @@ Byte_t * gene_emptytilestream( const Byte8_t tileID, Byte8_t *length) *length = 14; buf = (Byte_t *)opj_malloc(*length); - Isot = (Byte2_t)((((Byte2_t)tileID) << 8) | ((((Byte2_t)tileID) & 0xf0) >> 8)); + if(buf){ + Isot = (Byte2_t)((((Byte2_t)tileID) << 8) | ((((Byte2_t)tileID) & 0xf0) >> 8)); - memcpy( buf, &SOT, 2); - memcpy( buf+2, &Lsot, 2); - memcpy( buf+4, &Isot, 2); - memcpy( buf+6, &Psot, 4); - memcpy( buf+10, &TPsot, 1); - memcpy( buf+11, &TNsot, 1); - memcpy( buf+12, &SOD, 2); - + memcpy( buf, &SOT, 2); + memcpy( buf+2, &Lsot, 2); + memcpy( buf+4, &Isot, 2); + memcpy( buf+6, &Psot, 4); + memcpy( buf+10, &TPsot, 1); + memcpy( buf+11, &TNsot, 1); + memcpy( buf+12, &SOD, 2); + } return buf; } diff --git a/src/lib/openjpip/jpip_parser.c b/src/lib/openjpip/jpip_parser.c index d44c84c8..8a7dabe6 100644 --- a/src/lib/openjpip/jpip_parser.c +++ b/src/lib/openjpip/jpip_parser.c @@ -103,8 +103,10 @@ OPJ_BOOL open_channel( query_param_t query_param, cachemodel_param_t *cachemodel = NULL; if( target){ - if( !(*cursession)) + if( !(*cursession)){ *cursession = gene_session( sessionlist); + if(*cursession == NULL) return OPJ_FALSE; + } if( !( cachemodel = search_cachemodel( target, (*cursession)->cachemodellist))) if( !(cachemodel = gene_cachemodel( (*cursession)->cachemodellist, target, query_param.return_type==JPPstream))) return OPJ_FALSE; @@ -438,7 +440,7 @@ void enqueue_allprecincts( int tile_id, int level, int lastcomp, OPJ_BOOL *comps OPJ_BOOL enqueue_metabins( query_param_t query_param, metadatalist_param_t *metadatalist, msgqueue_param_t *msgqueue) { int i; - for( i=0; query_param.box_type[i][0]!=0 && i 0) memcpy( stream, cache_stream, *streamlen); memcpy( stream+(*streamlen), newstream, newstreamlen); @@ -123,7 +125,10 @@ ihdrbox_param_t * get_SIZ_from_jpipstream( Byte_t *jpipstream, msgqueue_param_t } ihdrbox = (ihdrbox_param_t *)opj_malloc( sizeof(ihdrbox_param_t)); - + if(ihdrbox == NULL){ + opj_free( j2kstream); + return NULL; + } ihdrbox->width = SIZ.Xsiz; ihdrbox->height = SIZ.Ysiz; ihdrbox->nc = SIZ.Csiz; diff --git a/src/lib/openjpip/manfbox_manager.c b/src/lib/openjpip/manfbox_manager.c index 66269577..64a3ce9c 100644 --- a/src/lib/openjpip/manfbox_manager.c +++ b/src/lib/openjpip/manfbox_manager.c @@ -49,7 +49,8 @@ manfbox_param_t * gene_manfbox( box_param_t *box) boxheader_param_t *last; /* last boxheader pointer of the list */ OPJ_OFF_T pos; /* current position in manf_box contents; */ - manf = ( manfbox_param_t *)malloc( sizeof( manfbox_param_t)); + manf = ( manfbox_param_t *)opj_malloc( sizeof( manfbox_param_t)); + if(manf == NULL) return NULL; pos = 0; manf->first = last = NULL; @@ -79,10 +80,10 @@ void delete_manfbox( manfbox_param_t **manf) #ifndef SERVER /* fprintf( logstream, "local log: boxheader %.4s deleted!\n", bhPtr->type); */ #endif - free(bhPtr); + opj_free(bhPtr); bhPtr = bhNext; } - free( *manf); + opj_free( *manf); } void print_manfbox( manfbox_param_t *manf) diff --git a/src/lib/openjpip/metadata_manager.c b/src/lib/openjpip/metadata_manager.c index e4666622..3ddbf6ba 100644 --- a/src/lib/openjpip/metadata_manager.c +++ b/src/lib/openjpip/metadata_manager.c @@ -50,8 +50,9 @@ metadatalist_param_t * gene_metadatalist(void) { metadatalist_param_t *list; - list = (metadatalist_param_t *)malloc( sizeof(metadatalist_param_t)); - + list = (metadatalist_param_t *)opj_malloc( sizeof(metadatalist_param_t)); + if(list == NULL) return NULL; + list->first = NULL; list->last = NULL; @@ -78,8 +79,13 @@ metadatalist_param_t * const_metadatalist( int fd) } phldlist = gene_placeholderlist(); - metadatalist = gene_metadatalist(); + if(phldlist == NULL) return NULL; + metadatalist = gene_metadatalist(); + if(metadatalist == NULL){ + opj_free(phldlist); + return NULL; + } box = toplev_boxlist->first; idx = 0; while( box){ @@ -89,20 +95,35 @@ metadatalist_param_t * const_metadatalist( int fd) boxcontents_param_t *boxcontents = NULL; phld = gene_placeholder( box, ++idx); + if(phld == NULL){ + delete_placeholderlist(&phldlist); + return NULL; + } insert_placeholder_into_list( phld, phldlist); boxlist = get_boxstructure( box->fd, get_DBoxoff( box), get_DBoxlen(box)); - if( !boxlist) - boxcontents = gene_boxcontents( get_DBoxoff( box), get_DBoxlen(box)); - + if( !boxlist){ + boxcontents = gene_boxcontents( get_DBoxoff( box), get_DBoxlen(box)); + if(boxcontents == NULL){ + delete_placeholderlist(&phldlist); + return NULL; + } + } delete_box_in_list( &box, toplev_boxlist); metabin = gene_metadata( idx, boxlist, NULL, boxcontents); + if(metabin == NULL){ + delete_placeholderlist(&phldlist); + return NULL; + } insert_metadata_into_list( metabin, metadatalist); } box = next; } metabin = gene_metadata( 0, toplev_boxlist, phldlist, NULL); + if(metabin == NULL){ + return NULL; + } insert_metadata_into_list( metabin, metadatalist); return metadatalist; @@ -119,14 +140,16 @@ void delete_metadatalist( metadatalist_param_t **list) delete_metadata( &ptr); ptr=next; } - free( *list); + opj_free( *list); } metadata_param_t * gene_metadata( Byte8_t idx, boxlist_param_t *boxlist, placeholderlist_param_t *phldlist, boxcontents_param_t *boxcontents) { metadata_param_t *bin; - bin = (metadata_param_t *)malloc( sizeof(metadata_param_t)); + bin = (metadata_param_t *)opj_malloc( sizeof(metadata_param_t)); + if(bin == NULL) return NULL; + bin->idx = idx; bin->boxlist = boxlist; bin->placeholderlist = phldlist; @@ -141,11 +164,11 @@ void delete_metadata( metadata_param_t **metadata) delete_boxlist( &((*metadata)->boxlist)); delete_placeholderlist( &((*metadata)->placeholderlist)); if((*metadata)->boxcontents) - free((*metadata)->boxcontents); + opj_free((*metadata)->boxcontents); #ifndef SERVER /* fprintf( logstream, "local log: Metadata-bin: %d deleted\n", (*metadata)->idx);*/ #endif - free( *metadata); + opj_free( *metadata); } void insert_metadata_into_list( metadata_param_t *metabin, metadatalist_param_t *metadatalist) @@ -188,7 +211,8 @@ boxcontents_param_t * gene_boxcontents( OPJ_OFF_T offset, OPJ_SIZE_T length) { boxcontents_param_t *contents; - contents = (boxcontents_param_t *)malloc( sizeof(boxcontents_param_t)); + contents = (boxcontents_param_t *)opj_malloc( sizeof(boxcontents_param_t)); + if(contents == NULL) return NULL; contents->offset = offset; contents->length = length; diff --git a/src/lib/openjpip/mhixbox_manager.c b/src/lib/openjpip/mhixbox_manager.c index dcfcfac4..291e2830 100644 --- a/src/lib/openjpip/mhixbox_manager.c +++ b/src/lib/openjpip/mhixbox_manager.c @@ -50,14 +50,18 @@ mhixbox_param_t * gene_mhixbox( box_param_t *box) markeridx_param_t *mkridx, *lastmkidx; OPJ_OFF_T pos = 0; - mhix = ( mhixbox_param_t *)malloc( sizeof( mhixbox_param_t)); - + mhix = ( mhixbox_param_t *)opj_malloc( sizeof( mhixbox_param_t)); + if(mhix == NULL) return NULL; + mhix->tlen = fetch_DBox8bytebigendian( box, (pos+=8)-8); mhix->first = lastmkidx = NULL; while( (OPJ_SIZE_T)pos < get_DBoxlen( box)){ - mkridx = ( markeridx_param_t *)malloc( sizeof( markeridx_param_t)); + mkridx = ( markeridx_param_t *)opj_malloc( sizeof( markeridx_param_t)); + if(mkridx == NULL){ + goto fails; + } mkridx->code = fetch_DBox2bytebigendian( box, (pos+=2)-2); mkridx->num_remain = fetch_DBox2bytebigendian( box, (pos+=2)-2); mkridx->offset = (OPJ_OFF_T)fetch_DBox8bytebigendian( box, (pos+=8)-8); @@ -71,6 +75,10 @@ mhixbox_param_t * gene_mhixbox( box_param_t *box) lastmkidx = mkridx; } return mhix; + +fails: + delete_mhixbox(&mhix); + return NULL; } @@ -132,10 +140,10 @@ void delete_mhixbox( mhixbox_param_t **mhix) #ifndef SERVER /* fprintf( logstream, "local log: marker index %#x deleted!\n", mkPtr->code); */ #endif - free(mkPtr); + opj_free(mkPtr); mkPtr=mkNext; } - free(*mhix); + opj_free(*mhix); } diff --git a/src/lib/openjpip/msgqueue_manager.c b/src/lib/openjpip/msgqueue_manager.c index 072a970b..252bfaa6 100644 --- a/src/lib/openjpip/msgqueue_manager.c +++ b/src/lib/openjpip/msgqueue_manager.c @@ -61,6 +61,7 @@ msgqueue_param_t * gene_msgqueue( OPJ_BOOL stateless, cachemodel_param_t *cachem msgqueue_param_t *msgqueue; msgqueue = (msgqueue_param_t *)opj_malloc( sizeof(msgqueue_param_t)); + if(msgqueue == NULL) return NULL; msgqueue->first = NULL; msgqueue->last = NULL; @@ -136,7 +137,11 @@ void enqueue_mainheader( msgqueue_param_t *msgqueue) codeidx = target->codeidx; msg = (message_param_t *)opj_malloc( sizeof(message_param_t)); - + if(msg == NULL) + { + cachemodel->mhead_model = OPJ_FALSE; + return; + } msg->last_byte = OPJ_TRUE; msg->in_class_id = 0; msg->class_id = MAINHEADER_MSG; @@ -167,6 +172,10 @@ void enqueue_tileheader( int tile_id, msgqueue_param_t *msgqueue) if( !cachemodel->th_model[ tile_id]){ msg = (message_param_t *)opj_malloc( sizeof(message_param_t)); + if(msg == NULL){ + cachemodel->th_model[ tile_id] = OPJ_FALSE; + return; + } msg->last_byte = OPJ_TRUE; assert( tile_id >= 0 ); msg->in_class_id = (Byte8_t)tile_id; @@ -221,7 +230,10 @@ void enqueue_tile( Byte4_t tile_id, int level, msgqueue_param_t *msgqueue) if( !tp_model[i]){ msg = (message_param_t *)opj_malloc( sizeof(message_param_t)); - + if(msg == NULL){ + tp_model[i] = OPJ_FALSE; + return; + } msg->last_byte = (i==numOftparts-1); msg->in_class_id = tile_id; msg->class_id = class_id; @@ -270,6 +282,10 @@ void enqueue_precinct( int seq_id, int tile_id, int comp_id, int layers, msgqueu if( !cachemodel->pp_model[comp_id][tile_id*(int)nmax+seq_id*numOflayers+layer_id]){ msg = (message_param_t *)opj_malloc( sizeof(message_param_t)); + if(msg == NULL){ + cachemodel->pp_model[comp_id][tile_id*(int)nmax+seq_id*numOflayers+layer_id] = OPJ_FALSE; + return; + } msg->last_byte = (layer_id == (numOflayers-1)); msg->in_class_id = comp_precinct_id( tile_id, comp_id, seq_id, codeidx->SIZ.Csiz, (int)codeidx->SIZ.XTnum * (int) codeidx->SIZ.YTnum); msg->class_id = PRECINCT_MSG; @@ -337,6 +353,8 @@ void enqueue_box( Byte8_t meta_id, boxlist_param_t *boxlist, msgqueue_param_t *m assert( msgqueue->cachemodel->target->csn >= 0); while( box){ msg = gene_metamsg( meta_id, *binOffset, box->length, box->offset, NULL, (Byte8_t)msgqueue->cachemodel->target->csn); + if(msg == NULL) return; /* FIXME szukw000 */ + enqueue_message( msg, msgqueue); *binOffset += box->length; @@ -353,6 +371,8 @@ void enqueue_phld( Byte8_t meta_id, placeholderlist_param_t *phldlist, msgqueue_ assert( msgqueue->cachemodel->target->csn >= 0); while( phld){ msg = gene_metamsg( meta_id, *binOffset, phld->LBox, 0, phld, (Byte8_t)msgqueue->cachemodel->target->csn); + if(msg == NULL) return; /* FIXME szukw000 */ + enqueue_message( msg, msgqueue); *binOffset += phld->LBox; @@ -367,6 +387,8 @@ void enqueue_boxcontents( Byte8_t meta_id, boxcontents_param_t *boxcontents, msg assert(msgqueue->cachemodel->target->csn >= 0); msg = gene_metamsg( meta_id, *binOffset, boxcontents->length, boxcontents->offset, NULL, (Byte8_t)msgqueue->cachemodel->target->csn); + if(msg == NULL) return; + enqueue_message( msg, msgqueue); *binOffset += boxcontents->length; @@ -377,7 +399,8 @@ message_param_t * gene_metamsg( Byte8_t meta_id, Byte8_t binOffset, Byte8_t leng message_param_t *msg; msg = (message_param_t *)opj_malloc( sizeof(message_param_t)); - + if(msg == NULL) return NULL; + msg->last_byte = OPJ_FALSE; msg->in_class_id = meta_id; msg->class_id = METADATA_MSG; @@ -597,7 +620,8 @@ void parse_JPIPstream( Byte_t *JPIPstream, Byte8_t streamlen, OPJ_OFF_T offset, ptr = JPIPstream; while( (Byte8_t)(ptr-JPIPstream) < streamlen){ msg = (message_param_t *)opj_malloc( sizeof(message_param_t)); - + if(msg == NULL) return; /* FIXME szukw000 */ + ptr = parse_bin_id_vbas( ptr, &bb, &c, &msg->in_class_id); msg->last_byte = c == 1 ? OPJ_TRUE : OPJ_FALSE; @@ -665,20 +689,26 @@ void parse_metadata( metadata_param_t *metadata, message_param_t *msg, Byte_t *d msg->phld = NULL; if( strncmp( boxtype, "phld", 4) == 0){ - if( !metadata->placeholderlist) - metadata->placeholderlist = gene_placeholderlist(); - + if( !metadata->placeholderlist){ + metadata->placeholderlist = gene_placeholderlist(); + if(metadata->placeholderlist == NULL) return; + } phld = parse_phld( datastream, msg->length); + if(phld == NULL) return; + msg->phld = phld; insert_placeholder_into_list( phld, metadata->placeholderlist); } else if( isalpha(boxtype[0]) && isalpha(boxtype[1]) && (isalnum(boxtype[2])||isspace(boxtype[2])) && (isalpha(boxtype[3])||isspace(boxtype[3]))){ - if( !metadata->boxlist) + if( !metadata->boxlist){ metadata->boxlist = gene_boxlist(); - + if(metadata->boxlist == NULL) return; + } box = gene_boxbyOffinStream( datastream, msg->res_offset); + if(box == NULL) return; + insert_box_into_list( box, metadata->boxlist); } else @@ -690,13 +720,18 @@ placeholder_param_t * parse_phld( Byte_t *datastream, Byte8_t metalength) placeholder_param_t *phld; phld = (placeholder_param_t *)opj_malloc( sizeof(placeholder_param_t)); - + if(phld == NULL) return NULL; + phld->LBox = big4( datastream); strncpy( phld->TBox, "phld", 4); phld->Flags = big4( datastream+8); phld->OrigID = big8( datastream+12); phld->OrigBHlen = (Byte_t)(metalength - 20); phld->OrigBH = (Byte_t *)opj_malloc(phld->OrigBHlen); + if(phld->OrigBH == NULL){ + opj_free(phld); + return NULL; + } memcpy( phld->OrigBH, datastream+20, phld->OrigBHlen); phld->next = NULL; diff --git a/src/lib/openjpip/openjpip.c b/src/lib/openjpip/openjpip.c index 6932dfa7..e8537795 100644 --- a/src/lib/openjpip/openjpip.c +++ b/src/lib/openjpip/openjpip.c @@ -58,7 +58,8 @@ server_record_t * init_JPIPserver( int tcp_auxport, int udp_auxport) { server_record_t *record = (server_record_t *)opj_malloc( sizeof(server_record_t)); - + if(record == NULL) return NULL; + record->sessionlist = gene_sessionlist(); record->targetlist = gene_targetlist(); record->auxtrans = init_aux_transport( tcp_auxport, udp_auxport); @@ -80,7 +81,8 @@ QR_t * parse_querystring( const char *query_string) QR_t *qr; qr = (QR_t *)opj_malloc( sizeof(QR_t)); - + if(qr == NULL) return NULL; + qr->query = parse_query( query_string); qr->msgqueue = NULL; qr->channel = NULL; @@ -212,6 +214,7 @@ void local_log( OPJ_BOOL query, OPJ_BOOL messages, OPJ_BOOL sessions, OPJ_BOOL t dec_server_record_t * OPJ_CALLCONV init_dec_server( int port) { dec_server_record_t *record = (dec_server_record_t *)opj_malloc( sizeof(dec_server_record_t)); + if(record == NULL) return NULL; record->cachelist = gene_cachelist(); record->jpipstream = NULL; @@ -311,6 +314,7 @@ jpip_dec_param_t * OPJ_CALLCONV init_jpipdecoder( OPJ_BOOL jp2) jpip_dec_param_t *dec; dec = (jpip_dec_param_t *)opj_calloc( 1, sizeof(jpip_dec_param_t)); + if(dec == NULL) return NULL; dec->msgqueue = gene_msgqueue( OPJ_TRUE, NULL); @@ -334,7 +338,10 @@ OPJ_BOOL OPJ_CALLCONV fread_jpip( const char fname[], jpip_dec_param_t *dec) return OPJ_FALSE; dec->jpipstream = (Byte_t *)opj_malloc( dec->jpiplen); - + if(dec->jpipstream == NULL){ + close(infd); + return OPJ_FALSE; + } if( read( infd, dec->jpipstream, dec->jpiplen) != (int)dec->jpiplen){ fprintf( stderr, "file reading error\n"); opj_free( dec->jpipstream); @@ -420,6 +427,8 @@ index_t * OPJ_CALLCONV get_index_from_JP2file( int fd) } data = (char *)opj_malloc( 12); /* size of header*/ + if(data == NULL) return NULL; + if( read( fd, data, 12) != 12){ opj_free( data); fprintf( stderr, "Error: File broken (read error)\n"); diff --git a/src/lib/openjpip/placeholder_manager.c b/src/lib/openjpip/placeholder_manager.c index 4acfeb87..0f313a8e 100644 --- a/src/lib/openjpip/placeholder_manager.c +++ b/src/lib/openjpip/placeholder_manager.c @@ -49,8 +49,9 @@ placeholderlist_param_t * gene_placeholderlist(void) { placeholderlist_param_t *list; - list = (placeholderlist_param_t *)malloc( sizeof(placeholderlist_param_t)); - + list = (placeholderlist_param_t *)opj_malloc( sizeof(placeholderlist_param_t)); + if(list == NULL) return NULL; + list->first = NULL; list->last = NULL; @@ -71,15 +72,16 @@ void delete_placeholderlist( placeholderlist_param_t **list) delete_placeholder( &ptr); ptr=next; } - free( *list); + opj_free( *list); } placeholder_param_t * gene_placeholder( box_param_t *box, Byte8_t origID) { placeholder_param_t *placeholder; - placeholder = (placeholder_param_t *)malloc( sizeof(placeholder_param_t)); - + placeholder = (placeholder_param_t *)opj_malloc( sizeof(placeholder_param_t)); + if(placeholder == NULL) return NULL; + strncpy( placeholder->TBox, "phld", 4); placeholder->Flags = 1; /* only the access to the original contents of this box, for now */ placeholder->OrigID = origID; @@ -94,8 +96,8 @@ placeholder_param_t * gene_placeholder( box_param_t *box, Byte8_t origID) void delete_placeholder( placeholder_param_t **placeholder) { if( (*placeholder)->OrigBH) - free((*placeholder)->OrigBH); - free(*placeholder); + opj_free((*placeholder)->OrigBH); + opj_free(*placeholder); } void insert_placeholder_into_list( placeholder_param_t *phld, placeholderlist_param_t *phldlist) diff --git a/src/lib/openjpip/query_parser.c b/src/lib/openjpip/query_parser.c index 9e69ebd4..e755d0db 100644 --- a/src/lib/openjpip/query_parser.c +++ b/src/lib/openjpip/query_parser.c @@ -160,6 +160,7 @@ query_param_t * get_initquery(void) int i; query = (query_param_t *)opj_malloc( sizeof(query_param_t)); + if(query == NULL) return NULL; query->target = NULL; query->tid = NULL; @@ -264,7 +265,7 @@ void print_queryparam( query_param_t query_param) } fprintf( logstream, "\t req-box-prop\n"); - for( i=0; query_param.box_type[i][0]!=0 && ipriority[idx] = OPJ_TRUE; - idx++; +/* idx++; */ } void parse_comps( char *field, query_param_t *query_param) @@ -400,6 +401,7 @@ void parse_comps( char *field, query_param_t *query_param) query_param->lastcomp = stop > aux ? stop : aux; query_param->comps = (OPJ_BOOL *)opj_calloc( 1, (OPJ_SIZE_T)(query_param->lastcomp+1)*sizeof(OPJ_BOOL)); + if(query_param->comps == NULL) return; /* FIXME szukw000 */ for( i=start; i<=stop; i++) query_param->comps[i]=OPJ_TRUE; diff --git a/src/lib/openjpip/query_parser.h b/src/lib/openjpip/query_parser.h index 5d6ce443..6cb37fe0 100644 --- a/src/lib/openjpip/query_parser.h +++ b/src/lib/openjpip/query_parser.h @@ -34,6 +34,15 @@ #include "opj_includes.h" +#ifndef OPJ_BOOL +typedef int OPJ_BOOL; +#define OPJ_TRUE 1 +#define OPJ_FALSE 0 +#endif +#ifndef OPJ_SIZE_T +typedef size_t OPJ_SIZE_T; +#endif + /** maximum number of meta request box */ #define MAX_NUMOFBOX 10 diff --git a/src/lib/openjpip/session_manager.c b/src/lib/openjpip/session_manager.c index e48873e8..9b686133 100644 --- a/src/lib/openjpip/session_manager.c +++ b/src/lib/openjpip/session_manager.c @@ -49,7 +49,8 @@ sessionlist_param_t * gene_sessionlist(void) sessionlist_param_t *sessionlist; sessionlist = (sessionlist_param_t *)opj_malloc( sizeof(sessionlist_param_t)); - + if(sessionlist == NULL) return NULL; + sessionlist->first = NULL; sessionlist->last = NULL; @@ -61,6 +62,7 @@ session_param_t * gene_session( sessionlist_param_t *sessionlist) session_param_t *session; session = (session_param_t *)opj_malloc( sizeof(session_param_t)); + if(session == NULL) return NULL; session->channellist = gene_channellist(); session->cachemodellist = gene_cachemodellist(); diff --git a/src/lib/openjpip/sock_manager.c b/src/lib/openjpip/sock_manager.c index 5634556d..fef53d83 100644 --- a/src/lib/openjpip/sock_manager.c +++ b/src/lib/openjpip/sock_manager.c @@ -119,14 +119,16 @@ void * receive_stream( SOCKET connected_socket, OPJ_SIZE_T length) char *stream, *ptr; OPJ_SIZE_T remlen; - ptr = stream = malloc( length); + ptr = stream = opj_malloc( length); + if(ptr == NULL) return NULL; + remlen = length; while( remlen > 0){ ssize_t redlen = recv( connected_socket, ptr, remlen, 0); if( redlen == -1){ fprintf( FCGI_stderr, "receive stream error\n"); - free( stream); + opj_free( stream); stream = NULL; break; } diff --git a/src/lib/openjpip/target_manager.c b/src/lib/openjpip/target_manager.c index fe9a6235..77af53d9 100644 --- a/src/lib/openjpip/target_manager.c +++ b/src/lib/openjpip/target_manager.c @@ -59,7 +59,8 @@ targetlist_param_t * gene_targetlist(void) targetlist_param_t *targetlist; targetlist = (targetlist_param_t *)opj_malloc( sizeof(targetlist_param_t)); - + if(targetlist == NULL) return NULL; + targetlist->first = NULL; targetlist->last = NULL; @@ -100,6 +101,8 @@ target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath) } target = (target_param_t *)opj_malloc( sizeof(target_param_t)); + if(target == NULL) return NULL; + snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand()); target->targetname = strdup( targetpath); target->fd = fd; @@ -275,7 +278,10 @@ int open_jp2file( const char filepath[], char tmpfname[]) } data = (char *)opj_malloc( 12); /* size of header*/ - + if(data == NULL){ + close(fd); + return -1; + } if( read( fd, data, 12) != 12){ opj_free( data); close(fd);