diff --git a/tsk/img/img_io.c b/tsk/img/img_io.c index df783ed8e710402992798c77b573e5e24830577f..33c6664ceee4297014271e3dc00651f85ccd1c6b 100644 --- a/tsk/img/img_io.c +++ b/tsk/img/img_io.c @@ -211,13 +211,18 @@ tsk_img_read(TSK_IMG_INFO * a_img_info, TSK_OFF_T a_off, // Determine the offset relative to the start of the cached data. rel_off = a_off - a_img_info->cache_off[cache_next]; + // Make sure we were able to read sufficient data into the cache. + if (rel_off > (TSK_OFF_T) read_count) { + len2 = 0; + } // Make sure not to copy more than is available in the cache. - if ((rel_off + (TSK_OFF_T) len2) > (TSK_OFF_T) read_count) { + else if ((rel_off + (TSK_OFF_T) len2) > (TSK_OFF_T) read_count) { len2 = (size_t) (read_count - rel_off); } - - memcpy(a_buf, &(a_img_info->cache[cache_next][rel_off]), len2); - + // Only copy data when we have something to copy. + if (len2 > 0) { + memcpy(a_buf, &(a_img_info->cache[cache_next][rel_off]), len2); + } read_count = (ssize_t) len2; } else {