Index: apps/app_stack.c =================================================================== --- apps/app_stack.c (revision 47708) +++ apps/app_stack.c (working copy) @@ -95,6 +95,7 @@ static int gosub_exec(struct ast_channel *chan, void *data) { char newlabel[AST_MAX_EXTENSION * 2 + 3 + 11]; + const char *macrocontext, *macroexten, *macropriority; struct localuser *u; if (ast_strlen_zero(data)) { @@ -110,6 +111,19 @@ return -1; } + /* If called from a macro and the contexts are different, then Macro will + * immediately exit, invalidating our saved label. If there's more than + * one Macro exiting, this still might do the wrong thing, but there's no + * way to save the right return address in that case (it's kept on a local + * C stack and is not accessible). */ + macrocontext = pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT"); + if (macrocontext && strcmp(macrocontext, chan->context)) { + ast_log(LOG_WARNING, "Calling Gosub to a destination outside of a macro is probably not what you want. Macro is about to exit.\n"); + macroexten = pbx_builtin_getvar_helper(chan, "MACRO_EXTEN"); + macropriority = pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY"); + snprintf(newlabel, sizeof(newlabel), "%s|%s|%d", macrocontext, macroexten, macropriority); + } + pbx_builtin_pushvar_helper(chan, STACKVAR, newlabel); LOCAL_USER_REMOVE(u);