example.txt: Avoid undefined setjmp() behavior
Modifying a locally-defined non-volatile variable below the setjmp()
return point results in undefined behavior whereby the variable may not
have the expected value after setjmp() returns.
Fixes #379
diff --git a/example.txt b/example.txt
index 04c11fe..bc0ba49 100644
--- a/example.txt
+++ b/example.txt
@@ -288,12 +288,14 @@
}
+METHODDEF(int) do_read_JPEG_file(struct jpeg_decompress_struct *cinfo,
+ char *filename);
+
/*
* Sample routine for JPEG decompression. We assume that the source file name
* is passed in. We want to return 1 on success, 0 on error.
*/
-
GLOBAL(int)
read_JPEG_file(char *filename)
{
@@ -301,6 +303,21 @@
* working space (which is allocated as needed by the JPEG library).
*/
struct jpeg_decompress_struct cinfo;
+
+ return do_read_JPEG_file(&cinfo, filename);
+}
+
+/*
+ * We call the libjpeg API from within a separate function, because modifying
+ * the local non-volatile jpeg_decompress_struct instance below the setjmp()
+ * return point and then accessing the instance after setjmp() returns would
+ * return in undefined behavior that may potentially overwrite all or part of
+ * the structure.
+ */
+
+METHODDEF(int)
+do_read_JPEG_file(struct jpeg_decompress_struct *cinfo, char *filename)
+{
/* We use our private extension JPEG error handler.
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
@@ -325,27 +342,27 @@
/* Step 1: allocate and initialize JPEG decompression object */
/* We set up the normal JPEG error routines, then override error_exit. */
- cinfo.err = jpeg_std_error(&jerr.pub);
+ cinfo->err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
/* Establish the setjmp return context for my_error_exit to use. */
if (setjmp(jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error.
* We need to clean up the JPEG object, close the input file, and return.
*/
- jpeg_destroy_decompress(&cinfo);
+ jpeg_destroy_decompress(cinfo);
fclose(infile);
return 0;
}
/* Now we can initialize the JPEG decompression object. */
- jpeg_create_decompress(&cinfo);
+ jpeg_create_decompress(cinfo);
/* Step 2: specify data source (eg, a file) */
- jpeg_stdio_src(&cinfo, infile);
+ jpeg_stdio_src(cinfo, infile);
/* Step 3: read file parameters with jpeg_read_header() */
- (void)jpeg_read_header(&cinfo, TRUE);
+ (void)jpeg_read_header(cinfo, TRUE);
/* We can ignore the return value from jpeg_read_header since
* (a) suspension is not possible with the stdio data source, and
* (b) we passed TRUE to reject a tables-only JPEG file as an error.
@@ -360,7 +377,7 @@
/* Step 5: Start decompressor */
- (void)jpeg_start_decompress(&cinfo);
+ (void)jpeg_start_decompress(cinfo);
/* We can ignore the return value since suspension is not possible
* with the stdio data source.
*/
@@ -372,30 +389,30 @@
* In this example, we need to make an output work buffer of the right size.
*/
/* JSAMPLEs per row in output buffer */
- row_stride = cinfo.output_width * cinfo.output_components;
+ row_stride = cinfo->output_width * cinfo->output_components;
/* Make a one-row-high sample array that will go away when done with image */
- buffer = (*cinfo.mem->alloc_sarray)
- ((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1);
+ buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr)cinfo, JPOOL_IMAGE, row_stride, 1);
/* Step 6: while (scan lines remain to be read) */
/* jpeg_read_scanlines(...); */
- /* Here we use the library's state variable cinfo.output_scanline as the
+ /* Here we use the library's state variable cinfo->output_scanline as the
* loop counter, so that we don't have to keep track ourselves.
*/
- while (cinfo.output_scanline < cinfo.output_height) {
+ while (cinfo->output_scanline < cinfo->output_height) {
/* jpeg_read_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could ask for
* more than one scanline at a time if that's more convenient.
*/
- (void)jpeg_read_scanlines(&cinfo, buffer, 1);
+ (void)jpeg_read_scanlines(cinfo, buffer, 1);
/* Assume put_scanline_someplace wants a pointer and sample count. */
put_scanline_someplace(buffer[0], row_stride);
}
/* Step 7: Finish decompression */
- (void)jpeg_finish_decompress(&cinfo);
+ (void)jpeg_finish_decompress(cinfo);
/* We can ignore the return value since suspension is not possible
* with the stdio data source.
*/
@@ -403,7 +420,7 @@
/* Step 8: Release JPEG decompression object */
/* This is an important step since it will release a good deal of memory. */
- jpeg_destroy_decompress(&cinfo);
+ jpeg_destroy_decompress(cinfo);
/* After finish_decompress, we can close the input file.
* Here we postpone it until after no more JPEG errors are possible,