[trunk] added overflow checks (fixes issue 431)
authorAntonin Descampe <antonin@gmail.com>
Mon, 2 Feb 2015 16:11:31 +0000 (16:11 +0000)
committerAntonin Descampe <antonin@gmail.com>
Mon, 2 Feb 2015 16:11:31 +0000 (16:11 +0000)
Thanks mdarbois

src/lib/openjp2/opj_intmath.h
src/lib/openjp2/pi.c
src/lib/openjp2/tcd.c

index 641fcaa22631668e4cfa6c1dca3eaca52bf7dc08..4e299469dee73420b473bc56d5523c62dbe6fbf3 100644 (file)
@@ -81,6 +81,15 @@ static INLINE OPJ_UINT32 opj_uint_max(OPJ_UINT32  a, OPJ_UINT32  b) {
        return (a > b) ? a : b;
 }
 
+/**
+ Get the saturated sum of two unsigned integers
+ @return Returns saturated sum of a+b
+ */
+static INLINE OPJ_UINT32 opj_uint_adds(OPJ_UINT32 a, OPJ_UINT32 b) {
+       OPJ_UINT64 sum = (OPJ_UINT64)a + (OPJ_UINT64)b;
+       return -(OPJ_UINT32)(sum >> 32) | (OPJ_UINT32)sum;
+}
+
 /**
 Clamp an integer inside an interval
 @return
index 66e5b05c89549d4a3f3034407def55e44d33687c..393a1e5540715d6d9b12a1148d66189935ea3cd0 100644 (file)
@@ -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_INT32)opj_uint_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
-       *p_tx1 = (OPJ_INT32)opj_uint_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
-       *p_ty0 = (OPJ_INT32)opj_uint_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
-       *p_ty1 = (OPJ_INT32)opj_uint_min(p_cp->ty0 + (q + 1) * p_cp->tdy, 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;
index a041bc1e91734075b9d7e510db1ea6abfb9586a6..f62312027d9c0d47919f9031c8e0f14911e71dd8 100644 (file)
@@ -675,6 +675,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
        OPJ_UINT32 l_pdx, l_pdy;
        OPJ_UINT32 l_gain;
        OPJ_INT32 l_x0b, l_y0b;
+       OPJ_UINT32 l_tx0, l_ty0;
        /* extent of precincts , top left, bottom right**/
        OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
        /* number of precinct for a resolution */
@@ -701,10 +702,12 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
        /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
        
        /* 4 borders of the tile rescale on the image if necessary */
-       l_tile->x0 = (OPJ_INT32)opj_uint_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0);
-       l_tile->y0 = (OPJ_INT32)opj_uint_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0);
-       l_tile->x1 = (OPJ_INT32)opj_uint_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1);
-       l_tile->y1 = (OPJ_INT32)opj_uint_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1);
+       l_tx0 = l_cp->tx0 + p * l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
+       l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
+       l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx), l_image->x1);
+       l_ty0 = l_cp->ty0 + q * l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
+       l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
+       l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy), l_image->y1);
 
        /* testcase 1888.pdf.asan.35.988 */
        if (l_tccp->numresolutions == 0) {