Updated fix for 687461, C stack overflow quetly quis

Alex Cherepanov <[email protected]> Fri, 27 Aug 2004 16:36:50 -0400
Newsgroups gmane.comp.printing.ghostscript.patches
Organization Coscript Software
Message-ID <[email protected]>
Trap C stack overflow exception on Borland C and MSVC. Without the
trap the application silently terminates when C stack overflows.

[DETAILS]
Watcom C 1.2 doesn't need the trap. It is also the only compiler tested 
that converts trailing recursion to iteration. GCC 3.2 on Windows still 
generates programs that fail silently but I don't know how to fix this. 
GCC 2.95 on Linux reports the stack overflow.

_______________________________________________
gs-code-review mailing list
[email protected]
http://www.ghostscript.com/mailman/listinfo/gs-code-review
c-stack.diff (text/plain, 3.9 KB)
Index: gs/src/dwmain.c
===================================================================
RCS file: /cvs/ghostscript/gs/src/dwmain.c,v
retrieving revision 1.19
diff -b -u -r1.19 dwmain.c
--- gs/src/dwmain.c	19 Aug 2004 19:33:09 -0000	1.19
+++ gs/src/dwmain.c	27 Aug 2004 20:18:29 -0000
@@ -17,8 +17,7 @@
 /* $Id: dwmain.c,v 1.19 2004/08/19 19:33:09 stefan Exp $ */
 /* Ghostscript DLL loader for Windows */
 
-#define STRICT
-#include <windows.h>
+#include "windows_.h"
 #include <shellapi.h>
 #include <stdio.h>
 #include <string.h>
@@ -328,12 +327,21 @@
     nargv[2] = ddpi;
     memcpy(&nargv[3], &argv[1], argc * sizeof(char *));
 
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+    __try {
+#endif
     code = gsdll.init_with_args(instance, nargc, nargv);
     if (code == 0)
 	code = gsdll.run_string(instance, start_string, 0, &exit_code);
     code1 = gsdll.exit(instance);
     if (code == 0 || (code == e_Quit && code1 != 0))
 	code = code1;
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+    } __except(exception_code() == EXCEPTION_STACK_OVERFLOW) {
+        code = e_Fatal;
+        text_puts(tw, "*** C stack overflow. Quiting...\n");
+    }
+#endif
 
     gsdll.delete_instance(instance);
 
Index: gs/src/dwmainc.c
===================================================================
RCS file: /cvs/ghostscript/gs/src/dwmainc.c,v
retrieving revision 1.22
diff -b -u -r1.22 dwmainc.c
--- gs/src/dwmainc.c	19 Aug 2004 21:52:20 -0000	1.22
+++ gs/src/dwmainc.c	27 Aug 2004 20:18:30 -0000
@@ -17,7 +17,7 @@
 /* $Id: dwmainc.c,v 1.22 2004/08/19 21:52:20 ghostgum Exp $ */
 /* dwmainc.c */
 
-#include <windows.h>
+#include "windows_.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <io.h>
@@ -415,12 +415,21 @@
     nargv[2] = ddpi;
     memcpy(&nargv[3], &argv[1], argc * sizeof(char *));
 
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+    __try {
+#endif
     code = gsdll.init_with_args(instance, nargc, nargv);
     if (code == 0)
 	code = gsdll.run_string(instance, start_string, 0, &exit_code);
     code1 = gsdll.exit(instance);
     if (code == 0 || (code == e_Quit && code1 != 0))
 	code = code1;
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+    } __except(exception_code() == EXCEPTION_STACK_OVERFLOW) {
+        code = e_Fatal;
+        fprintf(stderr, "*** C stack overflow. Quiting...\n");
+    }
+#endif
 
     gsdll.delete_instance(instance);
 
Index: gs/src/windows_.h
===================================================================
RCS file: /cvs/ghostscript/gs/src/windows_.h,v
retrieving revision 1.6
diff -b -u -r1.6 windows_.h
--- gs/src/windows_.h	8 Apr 2004 16:18:25 -0000	1.6
+++ gs/src/windows_.h	27 Aug 2004 20:18:30 -0000
@@ -46,4 +46,8 @@
 #  define _fstrtok(str, set) strtok(str, set)
 #endif
 
+#if defined(__BORLANDC__)
+#  define exception_code() __exception_code  
+#endif
+
 #endif /* windows__INCLUDED */
Index: gs/src/winint.mak
===================================================================
RCS file: /cvs/ghostscript/gs/src/winint.mak,v
retrieving revision 1.21
diff -b -u -r1.21 winint.mak
--- gs/src/winint.mak	30 Jan 2004 08:40:57 -0000	1.21
+++ gs/src/winint.mak	27 Aug 2004 20:18:30 -0000
@@ -139,7 +139,7 @@
 
 OBJCNO=$(PSOBJ)dwmainc.obj $(PSOBJ)dwnodllc.obj $(GLOBJ)dwimg.obj $(DWTRACE) $(GLOBJ)dwreg.obj
 
-$(PSOBJ)dwmainc.obj: $(PSSRC)dwmainc.c $(AK) $(fcntl__h) $(unistd__h) \
+$(PSOBJ)dwmainc.obj: $(PSSRC)dwmainc.c $(AK) $(windows__h) $(fcntl__h) $(unistd__h) \
   $(iapi_h) $(vdtrace_h) $(gdevdsp_h) $(dwdll_h) $(dwimg_h) $(dwtrace_h)
 	$(PSCCWIN) $(COMPILE_FOR_CONSOLE_EXE) $(PSO_)dwmainc.obj $(C_) $(PSSRC)dwmainc.c
 
@@ -169,7 +169,7 @@
  $(gscdefs_h) $(stdpre_h) $(gsdll_h) $(vdtrace_h)
 	$(GLCPP) $(COMPILE_FOR_EXE) $(GLO_)dwtrace.obj $(C_) $(GLSRC)dwtrace.c
 
-$(PSOBJ)dwmain.obj: $(PSSRC)dwmain.c $(AK)\
+$(PSOBJ)dwmain.obj: $(PSSRC)dwmain.c $(AK)  $(windows__h) \
  $(iapi_h) $(vdtrace_h) $(dwmain_h) $(dwdll_h) $(dwtext_h) $(dwimg_h) $(dwtrace_h) \
  $(dwreg_h) $(gdevdsp_h)
 	$(PSCCWIN) $(COMPILE_FOR_EXE) $(PSO_)dwmain.obj $(C_) $(PSSRC)dwmain.c