Index: bregexec.cpp
===================================================================
--- bregexec.cpp	(revision 1112)
+++ bregexec.cpp	(working copy)
@@ -77,7 +77,21 @@
 #include <string.h>
 #include <ctype.h>
 
+//	From Here 
+/*
+	2007.06.23 genta : SEH support
+	These inclusion must be before "sv.h" because
+	"sv.h" include definition of BOOL that conflicts
+	typdef in "windows.h"
+*/	
+#include <windows.h>
+#include <malloc.h>
 
+#if _MSC_VER > 1000
+#define __STR2__(x) #x
+#define __STR1__(x) __STR2__(x)
+#define __LOC__ __FILE__ "("__STR1__(__LINE__)") : warning MESSAGE: "
+#endif
 
 #define KANJI
 
@@ -147,14 +161,21 @@
  */
 
 /*
- - bregexec - match a regexp against a string
+ - bregexec_main - match a regexp against a string
+   renamed from bregexp
+
+	This function catch stack overflow exception
+	and rewind the stack area.
+	because regmatch() use recursive call,
+	stack area can run out.
  */
-BOOL bregexec(register regexp *prog,char *stringarg,
+static BOOL bregexec_main(register regexp *prog,char *stringarg,
 register char *strend,	/* pointer to null at end of string */
 char *strbeg,	/* real beginning of string */
 int minend,		/* end of match must be at least minend after stringarg */
 int safebase,	/* no need to remember string in subbase */
-char *msg)		/* fatal error message */
+char *msg,      /* fatal error message */
+GLOBAL *globalp)/* global workarea */
 {
     register char *s;
     register int i;
@@ -166,13 +187,8 @@
 
 	if (startpos == strend)	// length 0  bye bye
 		return 0;
+	//	initialization of global has been moved.
 
-
-	GLOBAL global;
-	GLOBAL *globalp;
-	globalp = &global;
-	memset((void*)globalp,0,sizeof(GLOBAL));
-
 	multiline = prog->pmflags & PMf_MULTILINE;
 	kanji_mode_flag = prog->pmflags & PMf_KANJI;	// using KANJI_MODE macro
     int minlen = 0;		/* must match at least this many chars */
@@ -191,7 +207,7 @@
     }
 
 	////////// exception started
-	try {
+	__try {
 
 
 
@@ -591,8 +607,7 @@
 //	    Safefree(startpos);
 	    delete [] startpos;
     }
-	if (savestack)
-		delete [] savestack;
+    //	2007.06.23 savestack is moved to outer scope
     return 1;
 
 phooey:
@@ -600,33 +615,75 @@
 //		Safefree(startpos);
 		delete [] startpos;
 
-	if (savestack)
-		delete [] savestack;
+    //	2007.06.23 savestack is moved to outer scope
 	
 	return 0;
 
 }		// error handler start
-	catch (const char* err){
-		strcpy(msg,err);
-
-		if (savestack)
-			delete [] savestack;
-
+#if _MSC_VER >= 1310
+	__except ( (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)?
+				EXCEPTION_EXECUTE_HANDLER: EXCEPTION_CONTINUE_SEARCH ){
+		_resetstkoflw();	// recovers from a stack overflow condition
+		strcpy(msg,"stack overflow");
 		return 0;
 	}
-	catch (...){
-		strcpy(msg,"fatal error");
-
-		if (savestack)
-			delete [] savestack;
-
+#else
+#if _MSC_VER > 1000
+#pragma message( __LOC__ "Stack overflow recovery will not work. The _resetstkoflw() function is declared in malloc.h for VC7.1 or lator." )
+#endif
+	__except ( EXCEPTION_CONTINUE_SEARCH ){
+		// We'll never get here.
 		return 0;
 	}
+#endif
 
 
 
 
 }
+
+
+/*
+	bregexec : C++ exception wrapper for bregexec_main
+
+	@author genta
+	@date 2007.06.23 created
+ */
+BOOL bregexec(register regexp *prog,char *stringarg,
+register char *strend,	/* pointer to null at end of string */
+char *strbeg,	/* real beginning of string */
+int minend,		/* end of match must be at least minend after stringarg */
+int safebase,	/* no need to remember string in subbase */
+char *msg)		/* fatal error message */
+{
+	BOOL ret;
+	GLOBAL global_body;
+	memset((void*)&global_body,0,sizeof(global_body));
+
+	GLOBAL *globalp = &global_body;
+
+	try {
+		/* memory management is moved from bregexec_main()
+		   to ensure memory release even in case of stack overflow
+		*/
+		ret = bregexec_main( prog, stringarg, strend, strbeg,
+			minend, safebase, msg, &global_body );
+	}
+	catch (const char* err){
+		strcpy(msg,err);
+		ret = 0;
+	}
+	catch (...){
+		strcpy(msg,"fatal error");
+		ret = 0;
+	}
+
+	// ensure 
+	if (savestack)
+		delete [] savestack;
+	return ret;
+}
+
 ////////////////////////////////XXXXXXXXXXXXX
 /*
  - regmatch - main matching routine
Index: BREGEXP.rc
===================================================================
--- BREGEXP.rc	(revision 1112)
+++ BREGEXP.rc	(working copy)
@@ -7,7 +7,8 @@
 //
 // Generated from the TEXTINCLUDE 2 resource.
 //
-#include "afxres.h"
+//#include "afxres.h"
+#include "windows.h"
 
 /////////////////////////////////////////////////////////////////////////////
 #undef APSTUDIO_READONLY_SYMBOLS
@@ -28,7 +29,7 @@
 //
 
 VS_VERSION_INFO VERSIONINFO
- FILEVERSION 2,0,4,291
+ FILEVERSION 2,0,4,292
  PRODUCTVERSION 2,0,4,29
  FILEFLAGSMASK 0x3fL
 #ifdef _DEBUG
@@ -47,7 +48,7 @@
             VALUE "Comments", "Regular Expression Engine for SAKURA\0"
             VALUE "CompanyName", "Tatsuo Baba\0"
             VALUE "FileDescription", "BREGEXP\0"
-            VALUE "FileVersion", "2, 0, 4, 291\0"
+            VALUE "FileVersion", "2, 0, 4, 292\0"
             VALUE "InternalName", "BREGEXP\0"
             VALUE "LegalCopyright", "Copyright (C) 1999-2000 Tatsuo Baba\0"
             VALUE "LegalTrademarks", "\0"
Index: main.cpp
===================================================================
--- main.cpp	(revision 1112)
+++ main.cpp	(working copy)
@@ -28,7 +28,7 @@
 {
 	//	2003.11.01 Karoto : Change version info
 	//	2006.07.14 genta : Change version info
-	static char version[] = "Bregexp.dll V1.02 for SAKURA "
+	static char version[] = "Bregexp.dll No stack overflow "
 __DATE__;
 
 	return version;
