comparison src/glyphs-eimage.c @ 3839:320acec37716

[xemacs-hg @ 2007-02-21 10:49:30 by stephent] Fix error handling in png_instantiate. <871wkjn5in.fsf@uwakimon.sk.tsukuba.ac.jp>
author stephent
date Wed, 21 Feb 2007 10:49:33 +0000
parents e1f65870f845
children a5ff7e67ac1b
comparison
equal deleted inserted replaced
3838:ee97c4f8f57d 3839:320acec37716
847 struct png_unwind_data *data = 847 struct png_unwind_data *data =
848 (struct png_unwind_data *) get_opaque_ptr (unwind_obj); 848 (struct png_unwind_data *) get_opaque_ptr (unwind_obj);
849 849
850 free_opaque_ptr (unwind_obj); 850 free_opaque_ptr (unwind_obj);
851 if (data->png_ptr) 851 if (data->png_ptr)
852 png_destroy_read_struct (&(data->png_ptr), &(data->info_ptr), (png_infopp)NULL); 852 {
853 /* ensure we can't get here again */
854 png_structp tmp = data->png_ptr;
855 data->png_ptr = NULL;
856 png_destroy_read_struct (&tmp, &(data->info_ptr), (png_infopp)NULL);
857 }
858
853 if (data->instream) 859 if (data->instream)
854 retry_fclose (data->instream); 860 retry_fclose (data->instream);
855 861
856 if (data->eimage) 862 if (data->eimage)
857 xfree(data->eimage, Binbyte *); 863 xfree(data->eimage, Binbyte *);
872 878
873 /* PNG variables */ 879 /* PNG variables */
874 png_structp png_ptr; 880 png_structp png_ptr;
875 png_infop info_ptr; 881 png_infop info_ptr;
876 882
883 xzero (unwind);
884 record_unwind_protect (png_instantiate_unwind, make_opaque_ptr (&unwind));
885
886 if (setjmp (png_err_stct.setjmp_buffer))
887 {
888 /* Something blew up:
889 just display the error (cleanup happens in the unwind) */
890 signal_image_error_2 ("Error decoding PNG",
891 build_string(png_err_stct.err_str),
892 instantiator);
893 }
894
877 /* Initialize all PNG structures */ 895 /* Initialize all PNG structures */
878 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, (void*)&png_err_stct, 896 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
897 (void *) &png_err_stct,
879 png_error_func, png_warning_func); 898 png_error_func, png_warning_func);
880 if (!png_ptr) 899 if (!png_ptr)
881 signal_image_error ("Error obtaining memory for png_read", instantiator); 900 signal_image_error ("Error obtaining memory for png_read", instantiator);
901 unwind.png_ptr = png_ptr;
902
882 info_ptr = png_create_info_struct (png_ptr); 903 info_ptr = png_create_info_struct (png_ptr);
883 if (!info_ptr) 904 if (!info_ptr)
884 { 905 {
906 unwind.png_ptr = NULL; /* avoid re-calling png_destroy_read_struct
907 when unwinding */
885 png_destroy_read_struct (&png_ptr, (png_infopp)NULL, (png_infopp)NULL); 908 png_destroy_read_struct (&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
886 signal_image_error ("Error obtaining memory for png_read", instantiator); 909 signal_image_error ("Error obtaining memory for png_read", instantiator);
887 } 910 }
888
889 xzero (unwind);
890 unwind.png_ptr = png_ptr;
891 unwind.info_ptr = info_ptr; 911 unwind.info_ptr = info_ptr;
892
893 record_unwind_protect (png_instantiate_unwind, make_opaque_ptr (&unwind));
894 912
895 /* This code is a mixture of stuff from Ben's GIF/JPEG stuff from 913 /* This code is a mixture of stuff from Ben's GIF/JPEG stuff from
896 this file, example.c from the libpng 0.81 distribution, and the 914 this file, example.c from the libpng 0.81 distribution, and the
897 pngtopnm sources. -WMP- 915 pngtopnm sources. -WMP-
898 */ 916 */
899 /* It has been further modified to handle the API changes for 0.96, 917 /* It has been further modified to handle the API changes for 0.96,
900 and is no longer usable for previous versions. jh 918 and is no longer usable for previous versions. jh
901 */ 919 */
902
903 /* Set the jmp_buf return context for png_error ... if this returns !0, then
904 we ran into a problem somewhere, and need to clean up after ourselves. */
905 if (setjmp (png_err_stct.setjmp_buffer))
906 {
907 /* Something blew up: just display the error (cleanup happens in the unwind) */
908 signal_image_error_2 ("Error decoding PNG",
909 build_string(png_err_stct.err_str),
910 instantiator);
911 }
912 920
913 /* Initialize the IO layer and read in header information */ 921 /* Initialize the IO layer and read in header information */
914 { 922 {
915 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data); 923 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
916 const Binbyte *bytes; 924 const Binbyte *bytes;