|
| 1 | +diff --git a/src/lib/openjp2/cio.c b/src/lib/openjp2/cio.c |
| 2 | +index 4fde9fe..435948a 100644 |
| 3 | +--- a/src/lib/openjp2/cio.c |
| 4 | ++++ b/src/lib/openjp2/cio.c |
| 5 | +@@ -499,7 +499,7 @@ OPJ_OFF_T opj_stream_read_skip(opj_stream_private_t * p_stream, |
| 6 | + /* Check if we are going beyond the end of file. Most skip_fn do not */ |
| 7 | + /* check that, but we must be careful not to advance m_byte_offset */ |
| 8 | + /* beyond m_user_data_length, otherwise */ |
| 9 | +- /* opj_stream_get_number_byte_left() will assert. */ |
| 10 | ++ /* opj_stream_get_number_byte_left() will return -1. */ |
| 11 | + if ((OPJ_UINT64)(p_stream->m_byte_offset + l_skip_nb_bytes + p_size) > |
| 12 | + p_stream->m_user_data_length) { |
| 13 | + opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n"); |
| 14 | +@@ -583,8 +583,10 @@ OPJ_OFF_T opj_stream_tell(const opj_stream_private_t * p_stream) |
| 15 | + |
| 16 | + OPJ_OFF_T opj_stream_get_number_byte_left(const opj_stream_private_t * p_stream) |
| 17 | + { |
| 18 | +- assert(p_stream->m_byte_offset >= 0); |
| 19 | +- assert(p_stream->m_user_data_length >= (OPJ_UINT64)p_stream->m_byte_offset); |
| 20 | ++ if (p_stream->m_byte_offset < 0 || |
| 21 | ++ p_stream->m_user_data_length < (OPJ_UINT64)p_stream->m_byte_offset) { |
| 22 | ++ return (OPJ_OFF_T) - 1; |
| 23 | ++ } |
| 24 | + return p_stream->m_user_data_length ? |
| 25 | + (OPJ_OFF_T)(p_stream->m_user_data_length) - p_stream->m_byte_offset : |
| 26 | + 0; |
| 27 | +diff --git a/src/lib/openjp2/cio.h b/src/lib/openjp2/cio.h |
| 28 | +index 7caee30..94e4d97 100644 |
| 29 | +--- a/src/lib/openjp2/cio.h |
| 30 | ++++ b/src/lib/openjp2/cio.h |
| 31 | +@@ -320,7 +320,8 @@ OPJ_OFF_T opj_stream_tell(const opj_stream_private_t * p_stream); |
| 32 | + * |
| 33 | + * @param p_stream the stream to get the information from. |
| 34 | + * |
| 35 | +- * @return Number of bytes left before the end of the stream. |
| 36 | ++ * @return Number of bytes left before the end of the stream, or -1 if the |
| 37 | ++ * stream position is inconsistent (e.g. read past end). |
| 38 | + */ |
| 39 | + OPJ_OFF_T opj_stream_get_number_byte_left(const opj_stream_private_t * |
| 40 | + p_stream); |
| 41 | +diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c |
| 42 | +index ad76aca..38231f6 100644 |
| 43 | +--- a/src/lib/openjp2/j2k.c |
| 44 | ++++ b/src/lib/openjp2/j2k.c |
| 45 | +@@ -4995,12 +4995,18 @@ static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k, |
| 46 | + l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); |
| 47 | + |
| 48 | + if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) { |
| 49 | ++ OPJ_OFF_T l_bytes_left = opj_stream_get_number_byte_left(p_stream); |
| 50 | ++ if (l_bytes_left < 2) { |
| 51 | ++ opj_event_msg(p_manager, EVT_ERROR, |
| 52 | ++ "Not enough bytes left in stream for tile part\n"); |
| 53 | ++ return OPJ_FALSE; |
| 54 | ++ } |
| 55 | + /* opj_stream_get_number_byte_left returns OPJ_OFF_T |
| 56 | + // but we are in the last tile part, |
| 57 | + // so its result will fit on OPJ_UINT32 unless we find |
| 58 | + // a file with a single tile part of more than 4 GB...*/ |
| 59 | + p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)( |
| 60 | +- opj_stream_get_number_byte_left(p_stream) - 2); |
| 61 | ++ l_bytes_left - 2); |
| 62 | + } else { |
| 63 | + /* Check to avoid pass the limit of OPJ_UINT32 */ |
| 64 | + if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) { |
| 65 | +@@ -9768,7 +9774,7 @@ OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, |
| 66 | + /* Try to read until the Start Of Data is detected */ |
| 67 | + while (l_current_marker != J2K_MS_SOD) { |
| 68 | + |
| 69 | +- if (opj_stream_get_number_byte_left(p_stream) == 0) { |
| 70 | ++ if (opj_stream_get_number_byte_left(p_stream) <= 0) { |
| 71 | + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
| 72 | + break; |
| 73 | + } |
| 74 | +@@ -9792,7 +9798,7 @@ OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, |
| 75 | + |
| 76 | + /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */ |
| 77 | + if (l_current_marker == 0x8080 && |
| 78 | +- opj_stream_get_number_byte_left(p_stream) == 0) { |
| 79 | ++ opj_stream_get_number_byte_left(p_stream) <= 0) { |
| 80 | + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
| 81 | + break; |
| 82 | + } |
| 83 | +@@ -9903,7 +9909,7 @@ OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k, |
| 84 | + &l_current_marker, 2); |
| 85 | + } |
| 86 | + } |
| 87 | +- if (opj_stream_get_number_byte_left(p_stream) == 0 |
| 88 | ++ if (opj_stream_get_number_byte_left(p_stream) <= 0 |
| 89 | + && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
| 90 | + break; |
| 91 | + } |
| 92 | +@@ -10180,7 +10186,7 @@ OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k, |
| 93 | + p_j2k->m_specific_param.m_decoder.m_can_decode = 0; |
| 94 | + p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA); |
| 95 | + |
| 96 | +- if (opj_stream_get_number_byte_left(p_stream) == 0 |
| 97 | ++ if (opj_stream_get_number_byte_left(p_stream) <= 0 |
| 98 | + && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
| 99 | + return OPJ_TRUE; |
| 100 | + } |
| 101 | +@@ -10197,7 +10203,7 @@ OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k, |
| 102 | + p_j2k->m_current_tile_number = 0; |
| 103 | + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC; |
| 104 | + } else if (l_current_marker != J2K_MS_SOT) { |
| 105 | +- if (opj_stream_get_number_byte_left(p_stream) == 0) { |
| 106 | ++ if (opj_stream_get_number_byte_left(p_stream) <= 0) { |
| 107 | + p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; |
| 108 | + opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n"); |
| 109 | + return OPJ_TRUE; |
| 110 | +@@ -12123,7 +12129,7 @@ static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k, |
| 111 | + opj_event_msg(p_manager, EVT_INFO, |
| 112 | + "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1); |
| 113 | + |
| 114 | +- if (opj_stream_get_number_byte_left(p_stream) == 0 |
| 115 | ++ if (opj_stream_get_number_byte_left(p_stream) <= 0 |
| 116 | + && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) { |
| 117 | + break; |
| 118 | + } |
| 119 | +diff --git a/src/lib/openjp2/jp2.c b/src/lib/openjp2/jp2.c |
| 120 | +index 3d16d10..83405a4 100644 |
| 121 | +--- a/src/lib/openjp2/jp2.c |
| 122 | ++++ b/src/lib/openjp2/jp2.c |
| 123 | +@@ -496,6 +496,11 @@ static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box, |
| 124 | + |
| 125 | + if (box->length == 0) { /* last box */ |
| 126 | + const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio); |
| 127 | ++ if (bleft < 0) { |
| 128 | ++ opj_event_msg(p_manager, EVT_ERROR, |
| 129 | ++ "Stream position inconsistent while reading JP2 box\n"); |
| 130 | ++ return OPJ_FALSE; |
| 131 | ++ } |
| 132 | + if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) { |
| 133 | + opj_event_msg(p_manager, EVT_ERROR, |
| 134 | + "Cannot handle box sizes higher than 2^32\n"); |
0 commit comments