add: if (cinfo.image_width < 1 || cinfo.image_height < 1) { RErrorCode = RERR_BADIMAGEFILE; goto bye; }
context: RLoadJPEG(RContext *context, char *file_name, int index){    RImage *image = NULL;    struct jpeg_decompress_struct cinfo;    int i;    unsigned char *ptr;    JSAMPROW buffer[1], bptr;    FILE *file;    /* 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.     */    struct my_error_mgr jerr;    file = fopen(file_name, "rb");    if (!file) {	RErrorCode = RERR_OPEN;	return NULL;    }    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);       fclose(file);       return NULL;    }    jpeg_create_decompress(&cinfo);    jpeg_stdio_src(&cinfo, file);      jpeg_read_header(&cinfo, TRUE);    bptr = buffer[0] = (JSAMPROW)malloc(cinfo.image_width*cinfo.num_components);    if (!buffer[0]) {	RErrorCode = RERR_NOMEMORY;	goto bye;    }    if(cinfo.jpeg_color_space==JCS_GRAYSCALE) {       cinfo.out_color_space=JCS_GRAYSCALE;    } else        cinfo.out_color_space = JCS_RGB;    cinfo.quantize_colors = FALSE;    cinfo.do_fancy_upsampling = FALSE;    cinfo.do_block_smoothing = FALSE;    jpeg_calc_output_dimensions(&cinfo);    if (context->flags.optimize_for_speed)	image = RCreateImage(cinfo.image_width, cinfo.image_height, True);    else	image = RCreateImage(cinfo.image_width, cinfo.image_height, False);    if (!image) {       RErrorCode = RERR_NOMEMORY;       goto bye;    }    jpeg_start_decompress(&cinfo);    ptr = image->data;        if (cinfo.out_color_space==JCS_RGB) {	if (context->flags.optimize_for_speed) {	    while (cinfo.output_scanline < cinfo.output_height) {		jpeg_read_scanlines(&cinfo, buffer,(JDIMENSION) 1);		bptr = buffer[0];		for (i=0; i<cinfo.image_width; i++) {		    *ptr++ = *bptr++;		    *ptr++ = *bptr++;		    *ptr++ = *bptr++;		    ptr++; /* skip alpha channel */		}	    }	} else {	    while (cinfo.output_scanline < cinfo.output_height) {		jpeg_read_scanlines(&cinfo, buffer,(JDIMENSION) 1);		bptr = buffer[0];		memcpy(ptr, bptr, cinfo.image_width*3);		ptr += cinfo.image_width*3;	    }	}    } else {	while (cinfo.output_scanline < cinfo.output_height) {	    jpeg_read_scanlines(&cinfo, buffer,(JDIMENSION) 1);	    bptr = buffer[0];	    for (i=0; i<cinfo.image_width; i++) {                *ptr++ = *bptr;                *ptr++ = *bptr;                *ptr++ = *bptr++;	    }	}    }    jpeg_finish_decompress(&cinfo);  bye:    jpeg_destroy_decompress(&cinfo);    fclose(file);    if (buffer[0])	free(buffer[0]);      return image;}