[Asterisk-cvs] asterisk/apps app_macro.c,1.26,1.27

kpfleming kpfleming
Wed Sep 7 16:39:01 CDT 2005


Update of /usr/cvsroot/asterisk/apps
In directory mongoose.digium.com:/tmp/cvs-serv31210/apps

Modified Files:
	app_macro.c 
Log Message:
put a limit on Macro depth (to combat recursion) (issue #5114)


Index: app_macro.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_macro.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -d -r1.26 -r1.27
--- app_macro.c	10 Jul 2005 23:49:57 -0000	1.26
+++ app_macro.c	7 Sep 2005 20:39:20 -0000	1.27
@@ -87,10 +87,10 @@
 	int res=0;
 	char oldexten[256]="";
 	int oldpriority;
-	char pc[80];
+	char pc[80], depthc[12];
 	char oldcontext[AST_MAX_CONTEXT] = "";
 	char *offsets;
-	int offset;
+	int offset, depth;
 	int setmacrocontext=0;
 	int autoloopflag;
   
@@ -105,6 +105,21 @@
 		return 0;
 	}
 
+	/* Count how many levels deep the rabbit hole goes */
+	tmp = pbx_builtin_getvar_helper(chan, "MACRO_DEPTH");
+	if (tmp) {
+		sscanf(tmp, "%d", &depth);
+	} else {
+		depth = 0;
+	}
+
+	if (depth >= 7) {
+		ast_log(LOG_ERROR, "Macro():  possible infinite loop detected.  Returning early.\n");
+		return 0;
+	}
+	snprintf(depthc, sizeof(depthc), "%d", depth + 1);
+	pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
+
 	tmp = ast_strdupa((char *) data);
 	rest = tmp;
 	macro = strsep(&rest, "|");
@@ -174,6 +189,8 @@
 	autoloopflag = ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP);
 	ast_set_flag(chan, AST_FLAG_IN_AUTOLOOP);
 	while(ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num)) {
+		/* Reset the macro depth, if it was changed in the last iteration */
+		pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
 		if ((res = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num))) {
 			/* Something bad happened, or a hangup has been requested. */
 			if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
@@ -215,6 +232,10 @@
 		chan->priority++;
   	}
 	out:
+	/* Reset the depth back to what it was when the routine was entered (like if we called Macro recursively) */
+	snprintf(depthc, sizeof(depthc), "%d", depth);
+	pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
+
 	ast_set2_flag(chan, autoloopflag, AST_FLAG_IN_AUTOLOOP);
   	for (x=1; x<argc; x++) {
   		/* Restore old arguments and delete ours */




More information about the svn-commits mailing list