From 2925ed8ac864ca866cf2e25a405817bebcf7a036 Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Thu, 26 Apr 2007 23:09:26 +0000 Subject: [PATCH] address http://jira.freeswitch.org/browse/FSCORE-6 We still need to test what the right number is for stack depth is for different platforms. We can get 13 out of msvc on my box (set the level to 12) but this is probably platform dependent and stack size dependent. How can we compute this? git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5024 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../languages/mod_spidermonkey/mod_spidermonkey.c | 12 ++++++++++++ .../languages/mod_spidermonkey/mod_spidermonkey.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c index 3984354b31..e957467dd4 100644 --- a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c +++ b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c @@ -623,6 +623,7 @@ JSObject *new_js_event(switch_event_t *event, char *name, JSContext * cx, JSObje return Event; } +#define MAX_STACK_DEPTH 12 static switch_status_t js_common_callback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen) { @@ -639,6 +640,8 @@ static switch_status_t js_common_callback(switch_core_session_t *session, void * return SWITCH_STATUS_FALSE; } + jss->stack_depth++; + switch (itype) { case SWITCH_INPUT_TYPE_EVENT: if ((event = (switch_event_t *) input)) { @@ -648,6 +651,7 @@ static switch_status_t js_common_callback(switch_core_session_t *session, void * } } if (!Event) { + jss->stack_depth--; return SWITCH_STATUS_FALSE; } break; @@ -662,10 +666,17 @@ static switch_status_t js_common_callback(switch_core_session_t *session, void * argv[argc++] = cb_state->arg; } + if (jss->stack_depth > MAX_STACK_DEPTH) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Maximum recursive callback limit %d reached.\n", MAX_STACK_DEPTH); + jss->stack_depth--; + return SWITCH_STATUS_FALSE; + } + JS_ResumeRequest(cb_state->cx, cb_state->saveDepth); JS_CallFunction(cb_state->cx, cb_state->obj, cb_state->function, argc, argv, &cb_state->ret); cb_state->saveDepth = JS_SuspendRequest(cb_state->cx); + jss->stack_depth--; return SWITCH_STATUS_SUCCESS; } @@ -2037,6 +2048,7 @@ static JSObject *new_js_session(JSContext * cx, JSObject * obj, switch_core_sess jss->flags = flags; jss->cx = cx; jss->obj = session_obj; + jss->stack_depth = 0; if ((JS_SetPrivate(cx, session_obj, jss) && JS_DefineProperties(cx, session_obj, session_props) && JS_DefineFunctions(cx, session_obj, session_methods))) { return session_obj; diff --git a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.h b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.h index c45fa12183..c625bf3290 100644 --- a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.h +++ b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.h @@ -129,6 +129,7 @@ struct js_session { unsigned int flags; switch_call_cause_t cause; JSFunction *on_hangup; + int stack_depth; switch_channel_state_t hook_state; };