Fix unsigned int overflow reported by UBSan (#761)
[openjpeg.git] / src / lib / openjp2 / pi.c
index 62eeb39c030e0b82ceab97cc4e063ceede8dcd12..cffad6684f0fc1f7521461c15c5135c19fb57bc6 100644 (file)
@@ -136,10 +136,10 @@ static void opj_pi_update_encode_not_poc (  opj_cp_t *p_cp,
  * @param      p_tx1                   pointer that will hold the X1 parameter for the tile
  * @param      p_ty0                   pointer that will hold the Y0 parameter for the tile
  * @param      p_ty1                   pointer that will hold the Y1 parameter for the tile
- * @param      p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
- * @param      p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
- * @param      p_dx_min                        pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
- * @param      p_dy_min                        pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
+ * @param      p_max_prec              pointer that will hold the maximum precision for all the bands of the tile
+ * @param      p_max_res               pointer that will hold the maximum number of resolutions for all the poc inside the tile.
+ * @param      p_dx_min                        pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
+ * @param      p_dy_min                        pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
  */
 static void opj_get_encoding_parameters(const opj_image_t *p_image,
                                         const opj_cp_t *p_cp,
@@ -167,10 +167,10 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image,
  * @param      p_tx1                   pointer that will hold the X1 parameter for the tile
  * @param      p_ty0                   pointer that will hold the Y0 parameter for the tile
  * @param      p_ty1                   pointer that will hold the Y1 parameter for the tile
- * @param      p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
- * @param      p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
- * @param      p_dx_min                pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
- * @param      p_dy_min                pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
+ * @param      p_max_prec              pointer that will hold the maximum precision for all the bands of the tile
+ * @param      p_max_res               pointer that will hold the maximum number of resolutions for all the poc inside the tile.
+ * @param      p_dx_min                pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
+ * @param      p_dy_min                pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
  * @param      p_resolutions   pointer to an area corresponding to the one described above.
  */
 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
@@ -214,7 +214,7 @@ static void opj_pi_update_decode_poc (  opj_pi_iterator_t * p_pi,
 /**
  * FIXME DOC
  */
-OPJ_BOOL opj_pi_check_next_level(      OPJ_INT32 pos,
+static OPJ_BOOL opj_pi_check_next_level(       OPJ_INT32 pos,
                                                                opj_cp_t *cp,
                                                                OPJ_UINT32 tileno,
                                                                OPJ_UINT32 pino,
@@ -230,7 +230,7 @@ OPJ_BOOL opj_pi_check_next_level(   OPJ_INT32 pos,
 ==========================================================
 */
 
-OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -270,7 +270,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -309,7 +309,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -392,7 +392,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -473,7 +473,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -552,7 +552,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-void opj_get_encoding_parameters(      const opj_image_t *p_image,
+static void opj_get_encoding_parameters(       const opj_image_t *p_image,
                                     const opj_cp_t *p_cp,
                                     OPJ_UINT32 p_tileno,
                                     OPJ_INT32 * p_tx0,
@@ -666,7 +666,7 @@ void opj_get_encoding_parameters(   const opj_image_t *p_image,
 }
 
 
-void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
+static void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
                                         const opj_cp_t *p_cp,
                                         OPJ_UINT32 tileno,
                                         OPJ_INT32 * p_tx0,
@@ -693,6 +693,9 @@ void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
        /* position in x and y of tile*/
        OPJ_UINT32 p, q;
 
+       /* non-corrected (in regard to image offset) tile offset */
+       OPJ_UINT32 l_tx0, l_ty0;
+
        /* preconditions in debug*/
        assert(p_cp != 00);
        assert(p_image != 00);
@@ -708,10 +711,12 @@ void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
        q = tileno / p_cp->tw;
 
        /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
-       *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
-       *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
-       *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
-       *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
+       l_tx0 = p_cp->tx0 + p * p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
+       *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
+       *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
+       l_ty0 = p_cp->ty0 + q * p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
+       *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
+       *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
 
        /* max precision and resolution is 0 (can only grow)*/
        *p_max_prec = 0;
@@ -742,10 +747,12 @@ void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
                }
 
                /* use custom size for precincts*/
-               l_level_no = l_tccp->numresolutions - 1;
+               l_level_no = l_tccp->numresolutions;
                for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
                        OPJ_UINT32 l_dx, l_dy;
 
+                       --l_level_no;
+                       
                        /* precinct width and height*/
                        l_pdx = l_tccp->prcw[resno];
                        l_pdy = l_tccp->prch[resno];
@@ -777,14 +784,13 @@ void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
                                *p_max_prec = l_product;
                        }
 
-                       --l_level_no;
                }
                ++l_tccp;
                ++l_img_comp;
        }
 }
 
-opj_pi_iterator_t * opj_pi_create(     const opj_image_t *image,
+static opj_pi_iterator_t * opj_pi_create(      const opj_image_t *image,
                                     const opj_cp_t *cp,
                                     OPJ_UINT32 tileno )
 {
@@ -845,7 +851,7 @@ opj_pi_iterator_t * opj_pi_create(  const opj_image_t *image,
        return l_pi;
 }
 
-void opj_pi_update_encode_poc_and_final (   opj_cp_t *p_cp,
+static void opj_pi_update_encode_poc_and_final (   opj_cp_t *p_cp,
                                             OPJ_UINT32 p_tileno,
                                             OPJ_INT32 p_tx0,
                                             OPJ_INT32 p_tx1,
@@ -923,7 +929,7 @@ void opj_pi_update_encode_poc_and_final (   opj_cp_t *p_cp,
        }
 }
 
-void opj_pi_update_encode_not_poc (    opj_cp_t *p_cp,
+static void opj_pi_update_encode_not_poc (     opj_cp_t *p_cp,
                                     OPJ_UINT32 p_num_comps,
                                     OPJ_UINT32 p_tileno,
                                     OPJ_INT32 p_tx0,
@@ -978,7 +984,7 @@ void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
        }
 }
 
-void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
+static void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
                                opj_tcp_t * p_tcp,
                                OPJ_UINT32 p_max_precision,
                                OPJ_UINT32 p_max_res)
@@ -1020,7 +1026,7 @@ void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
        }
 }
 
-void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
+static void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
                                    opj_tcp_t * p_tcp,
                                    OPJ_UINT32 p_max_precision,
                                    OPJ_UINT32 p_max_res)
@@ -1057,7 +1063,7 @@ void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
 
 
 
-OPJ_BOOL opj_pi_check_next_level(      OPJ_INT32 pos,
+static OPJ_BOOL opj_pi_check_next_level(       OPJ_INT32 pos,
                                                                opj_cp_t *cp,
                                                                OPJ_UINT32 tileno,
                                                                OPJ_UINT32 pino,
@@ -1105,7 +1111,8 @@ OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
                            break;
                    case 'P':
                            switch(tcp->prg){
-                                   case OPJ_LRCP||OPJ_RLCP:
+                    case OPJ_LRCP: /* fall through */
+                    case OPJ_RLCP:
                                            if(tcp->prc_t == tcp->prcE){
                                                    if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
                                                            return OPJ_TRUE;
@@ -1424,7 +1431,7 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
        l_step_l = l_max_res * l_step_r;
 
        /* set values for first packet iterator*/
-       l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
+       l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
        l_current_pi = l_pi;
 
        /* memory allocation for include*/