[trunk] correct bug when we expand the list of marker in a tile
[openjpeg.git] / libopenjpeg / j2k.c
index 351a58039f805dfbab07cfdb4db56ff10bdae0e0..9bfd4b359ef749793a23d2aec11871518a0f67f5 100644 (file)
@@ -3627,6 +3627,14 @@ opj_bool j2k_read_sot_v2 (
        opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
        p_header_data+=4;
 
+       /* PSot should be equal to zero or >=14 or <= 2^32-1 */
+       if ((l_tot_len !=0 ) && (l_tot_len < 14) )
+       {
+               opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
+               return OPJ_FALSE;
+       }
+
+
 #ifdef USE_JPWL
        if (l_cp->correct) {
 
@@ -5757,10 +5765,9 @@ opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_code
        return OPJ_TRUE;
 }
 
-static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
-
-       if (!cstr_info)
-               return;
+static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
+{
+       assert(cstr_info != 00);
 
        /* expand the list? */
        if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
@@ -5776,10 +5783,9 @@ static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short in
 
 }
 
-static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) {
-
-       if (!cstr_index)
-               return;
+static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
+{
+       assert(cstr_index != 00);
 
        /* expand the list? */
        if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
@@ -5795,13 +5801,11 @@ static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 t
 
 }
 
-static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
-
-
-  opj_marker_info_t *marker;
+static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
+{
+       opj_marker_info_t *marker;
 
-       if (!cstr_info)
-               return;
+       assert(cstr_info != 00);
 
        /* expand the list? */
        if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
@@ -5820,11 +5824,8 @@ static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsi
 
 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
 {
-
-       if (!cstr_index)
-               return;
-
-       if (!cstr_index->tile_index)
+       assert(cstr_index != 00);
+       assert(cstr_index->tile_index != 00);
 
        /* expand the list? */
        if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
@@ -5847,12 +5848,9 @@ static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_
                        cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
 
        }
-
 }
 
 
-
-
 /*
  * -----------------------------------------------------------------------
  * -----------------------------------------------------------------------
@@ -8152,8 +8150,10 @@ opj_bool j2k_decode_tiles (      opj_j2k_v2_t *p_j2k,
        l_max_data_size = 1000;
 
        /*Allocate and initialize some elements of codestrem index*/
-       if (!j2k_allocate_tile_element_cstr_index(p_j2k))
+       if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
+               opj_free(l_current_data);
                return OPJ_FALSE;
+       }
 
        while (OPJ_TRUE) {
                if (! j2k_read_tile_header(     p_j2k,
@@ -8176,6 +8176,7 @@ opj_bool j2k_decode_tiles (       opj_j2k_v2_t *p_j2k,
                if (l_data_size > l_max_data_size) {
                        l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
                        if (! l_current_data) {
+                               opj_free(l_current_data);
                                return OPJ_FALSE;
                        }
 
@@ -8238,8 +8239,10 @@ opj_bool j2k_decode_one_tile (   opj_j2k_v2_t *p_j2k,
        /*Allocate and initialize some elements of codestrem index if not already done*/
        if( !p_j2k->cstr_index->tile_index)
        {
-               if (!j2k_allocate_tile_element_cstr_index(p_j2k))
+               if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
+                       opj_free(l_current_data);
                        return OPJ_FALSE;
+               }
        }
        /* Move into the codestream to the first SOT used to decode the desired tile */
        l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
@@ -8287,6 +8290,7 @@ opj_bool j2k_decode_one_tile (    opj_j2k_v2_t *p_j2k,
                if (l_data_size > l_max_data_size) {
                        l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
                        if (! l_current_data) {
+                               opj_free(l_current_data);
                                return OPJ_FALSE;
                        }