Skip to content

Commit

Permalink
add try-except to msLoadMapFromString
Browse files Browse the repository at this point in the history
  • Loading branch information
szekerest committed May 3, 2024
1 parent 6221f9e commit d8bc719
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
57 changes: 57 additions & 0 deletions mapscript/csharp/csmodule.i
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ inner exceptions. Otherwise the exception message will be concatenated*/
* C# exception redefinition
*****************************************************************************/
#ifdef ALLOW_INNER_EXCEPTIONS
%exception msLoadMapFromString
{
errorObj *ms_error;
__try {
$action
}
__except(1 /*EXCEPTION_EXECUTE_HANDLER, catch every exception so it doesn't crash IIS*/) {
msSetError(MS_MISCERR, "Unhandled exception in msLoadMapFromString 0x%08x", "msLoadMapFromString()", GetExceptionCode());
}
ms_error = msGetErrorObj();
if (ms_error != NULL && ms_error->code != MS_NOERR) {
if (ms_error->code != MS_NOTFOUND && ms_error->code != -1) {
int ms_errorcode = ms_error->code;
while (ms_error!=NULL && ms_error->code != MS_NOERR) {
char* msg = msAddErrorDisplayString(NULL, ms_error);
if (msg) {
SWIG_CSharpException(SWIG_SystemError, msg);
free(msg);
}
else SWIG_CSharpException(SWIG_SystemError, "MapScript unknown error");
ms_error = ms_error->next;
}
msResetErrorList();
return $null;
}
msResetErrorList();
}
}
%exception
{
errorObj *ms_error;
Expand All @@ -100,6 +128,30 @@ inner exceptions. Otherwise the exception message will be concatenated*/
}
}
#else
%exception msLoadMapFromString
{
errorObj *ms_error;
__try {
$action
}
__except(1 /*EXCEPTION_EXECUTE_HANDLER, catch every exception so it doesn't crash IIS*/) {
msSetError(MS_MISCERR, "Unhandled exception in msLoadMapFromString 0x%08x", "msLoadMapFromString()", GetExceptionCode());
}
ms_error = msGetErrorObj();
if (ms_error != NULL && ms_error->code != MS_NOERR) {
if (ms_error->code != MS_NOTFOUND && ms_error->code != -1) {
char* msg = msGetErrorString(";");
if (msg) {
SWIG_CSharpException(SWIG_SystemError, msg);
free(msg);
}
else SWIG_CSharpException(SWIG_SystemError, "MapScript unknown error");
msResetErrorList();
return $null;
}
msResetErrorList();
}
}
%exception
{
errorObj *ms_error;
Expand Down Expand Up @@ -478,3 +530,8 @@ DllExport void SWIGSTDCALL SWIGRegisterByteArrayCallback_$module(SWIG_CSharpByte
$csclassname ret = (cPtr == System.IntPtr.Zero) ? null : new $csclassname(cPtr, $owner, ThisOwn_false());$excode
return ret;
}

%typemap(in) msLoadMapFromString %{
// test
$1 = ($1_ltype)$input;
%}
17 changes: 0 additions & 17 deletions mapscript/mapscript.i
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@
%ignore layerObj::extent;
#endif

#if !defined(WIN32) || !defined(SWIGCSHARP)
%newobject msLoadMapFromString;
#endif

%{
#include "../../mapserver.h"
Expand Down Expand Up @@ -335,19 +333,4 @@ typedef struct {
%include "rbextend.i"
#endif

#if defined(WIN32) && defined(SWIGCSHARP)
%inline %{
// Wrapper function with exception handling
mapObj *msLoadMapFromString_wrapper(char *buffer, char *new_mappath, const configObj* config) {
__try {
return msLoadMapFromString(buffer, new_mappath, config);
}
__except(EXCEPTION_EXECUTE_HANDLER) {
// Handle the exception here, e.g., set an error message
return NULL;
}
}
%}
%rename(msLoadMapFromString) msLoadMapFromString_wrapper;
#endif

18 changes: 18 additions & 0 deletions mapscript/swiginc/map.i
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,30 @@

mapObj(char *mapText, int isMapText /*used as signature only to differentiate this constructor from default constructor*/ )
{
#if defined(WIN32) && defined(SWIGCSHARP)
__try {
return msLoadMapFromString(mapText, NULL, NULL);
}
__except(1 /*EXCEPTION_EXECUTE_HANDLER, catch every exception so it doesn't crash IIS*/) {
msSetError(MS_MISCERR, "Unhandled exception in msLoadMapFromString 0x%08x", "msLoadMapFromString()", GetExceptionCode());
}
#else
return msLoadMapFromString(mapText, NULL, NULL);
#endif
}

mapObj(char *mapText, int isMapText, configObj *config)
{
#if defined(WIN32) && defined(SWIGCSHARP)
__try {
return msLoadMapFromString(mapText, NULL, config);
}
__except(1 /*EXCEPTION_EXECUTE_HANDLER, catch every exception so it doesn't crash IIS*/) {
msSetError(MS_MISCERR, "Unhandled exception in msLoadMapFromString 0x%08x", "msLoadMapFromString()", GetExceptionCode());
}
#else
return msLoadMapFromString(mapText, NULL, config);
#endif
}

#endif
Expand Down

0 comments on commit d8bc719

Please sign in to comment.