Fix bad return val when skipping past end of image
Fixes #439
diff --git a/ChangeLog.md b/ChangeLog.md
index 23469f1..0117f1f 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -17,6 +17,8 @@
- `jpeg_skip_scanlines()` now throws an error if two-pass color
quantization is enabled. Two-pass color quantization never worked properly
with `jpeg_skip_scanlines()`, and the issues could not readily be fixed.
+ - Fixed an issue whereby `jpeg_skip_scanlines()` always returned 0 when
+skipping past the end of an image.
2.0.5
diff --git a/djpeg.c b/djpeg.c
index e127cf7..fb4e7a6 100644
--- a/djpeg.c
+++ b/djpeg.c
@@ -5,7 +5,7 @@
* Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 2013 by Guido Vollbeding.
* libjpeg-turbo Modifications:
- * Copyright (C) 2010-2011, 2013-2017, D. R. Commander.
+ * Copyright (C) 2010-2011, 2013-2017, 2020, D. R. Commander.
* Copyright (C) 2015, Google, Inc.
* For conditions of distribution and use, see the accompanying README.ijg
* file.
@@ -708,7 +708,12 @@
dest_mgr->buffer_height);
(*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
}
- jpeg_skip_scanlines(&cinfo, skip_end - skip_start + 1);
+ if ((tmp = jpeg_skip_scanlines(&cinfo, skip_end - skip_start + 1)) !=
+ skip_end - skip_start + 1) {
+ fprintf(stderr, "%s: jpeg_skip_scanlines() returned %d rather than %d\n",
+ progname, tmp, skip_end - skip_start + 1);
+ exit(EXIT_FAILURE);
+ }
while (cinfo.output_scanline < cinfo.output_height) {
num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
dest_mgr->buffer_height);
@@ -744,13 +749,24 @@
cinfo.output_height = tmp;
/* Process data */
- jpeg_skip_scanlines(&cinfo, crop_y);
+ if ((tmp = jpeg_skip_scanlines(&cinfo, crop_y)) != crop_y) {
+ fprintf(stderr, "%s: jpeg_skip_scanlines() returned %d rather than %d\n",
+ progname, tmp, crop_y);
+ exit(EXIT_FAILURE);
+ }
while (cinfo.output_scanline < crop_y + crop_height) {
num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
dest_mgr->buffer_height);
(*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
}
- jpeg_skip_scanlines(&cinfo, cinfo.output_height - crop_y - crop_height);
+ if ((tmp =
+ jpeg_skip_scanlines(&cinfo,
+ cinfo.output_height - crop_y - crop_height)) !=
+ cinfo.output_height - crop_y - crop_height) {
+ fprintf(stderr, "%s: jpeg_skip_scanlines() returned %d rather than %d\n",
+ progname, tmp, cinfo.output_height - crop_y - crop_height);
+ exit(EXIT_FAILURE);
+ }
/* Normal full-image decompress */
} else {
diff --git a/jdapistd.c b/jdapistd.c
index c502909..38bd111 100644
--- a/jdapistd.c
+++ b/jdapistd.c
@@ -413,10 +413,11 @@
/* Do not skip past the bottom of the image. */
if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
+ num_lines = cinfo->output_height - cinfo->output_scanline;
cinfo->output_scanline = cinfo->output_height;
(*cinfo->inputctl->finish_input_pass) (cinfo);
cinfo->inputctl->eoi_reached = TRUE;
- return cinfo->output_height - cinfo->output_scanline;
+ return num_lines;
}
if (num_lines == 0)