security_events: Fix error caused by DTD validation error
[asterisk/asterisk.git] / res / ael / pval.c
index 5414e79..70bd689 100644 (file)
  * 
  */
 
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
+
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
@@ -57,6 +61,7 @@ extern int localized_pbx_load_module(void);
 
 static char expr_output[2096];
 #define AST_PBX_MAX_STACK  128
+#define BUF_SIZE 2000
 
 /* these functions are in ../ast_expr2.fl */
 
@@ -1159,7 +1164,11 @@ static pval *get_goto_target(pval *item)
        pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
        pval *curr_cont;
        
-       if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
+       if (!item->u1.list) {
+               return NULL;
+       }
+
+       if (!item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
                struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
                        return x;
        }
@@ -1218,21 +1227,24 @@ static pval *get_goto_target(pval *item)
                        return x;
                }
        }
-       return 0;
+       return NULL;
 }
 
 static void check_goto(pval *item)
 {
+       if (!item->u1.list) {
+               return;
+       }
+
        /* check for the target of the goto-- does it exist? */
        if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
                ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto:  empty label reference found!\n",
                                item->filename, item->startline, item->endline);
                errs++;
        }
-       
+
        /* just one item-- the label should be in the current extension */
-       
-       if (item->u1.list && !item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
+       if (!item->u1.list->next && !strstr(item->u1.list->u1.str,"${")) {
                struct pval *z = get_extension_or_contxt(item);
                struct pval *x = 0;
                if (z)
@@ -2890,7 +2902,7 @@ void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_not
        if (!item)
                return; /* don't check an empty tree */
 #ifdef AAL_ARGCHECK
-       rfilename = alloca(10 + strlen(ast_config_AST_VAR_DIR));
+       rfilename = ast_alloca(10 + strlen(ast_config_AST_VAR_DIR));
        sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
        
        apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
@@ -3334,7 +3346,7 @@ int contains_switch(pval *item)
 }
 
 
-static void gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
+static int gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
 {
        pval *p,*p2,*p3;
        struct ael_priority *pr;
@@ -3345,16 +3357,26 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
 #ifdef OLD_RAND_ACTION
        struct ael_priority *rand_test, *rand_end, *rand_skip;
 #endif
-       char buf1[2000];
-       char buf2[2000];
+       char *buf1;
+       char *buf2;
+       char *new_label;
        char *strp, *strp2;
-       char new_label[2000];
        int default_exists;
        int local_control_statement_count;
        int first;
        struct ael_priority *loop_break_save;
        struct ael_priority *loop_continue_save;
        struct ael_extension *switch_case,*switch_null;
+
+       if (!(buf1 = malloc(BUF_SIZE))) {
+               return -1;
+       }
+       if (!(buf2 = malloc(BUF_SIZE))) {
+               return -1;
+       }
+       if (!(new_label = malloc(BUF_SIZE))) {
+               return -1;
+       }
        
        if ((mother_exten && !mother_exten->checked_switch) || (exten && !exten->checked_switch)) {
                if (contains_switch(statement)) { /* only run contains_switch if you haven't checked before */
@@ -3369,7 +3391,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        switch_set->app = strdup("Set");
                                                }
                                                /* Are we likely inside a gosub subroutine? */
-                                               if (!strcmp(mother_exten->name, "s") && first) {
+                                               if (!strcmp(mother_exten->name, "~~s~~") && first) {
                                                        /* If we're not actually within a gosub, this will fail, but the
                                                         * second time through, it will get set.  If we are within gosub,
                                                         * the second time through is redundant, but acceptable. */
@@ -3398,7 +3420,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        switch_set->app = strdup("Set");
                                                }
                                                /* Are we likely inside a gosub subroutine? */
-                                               if (!strcmp(exten->name, "s")) {
+                                               if (!strcmp(exten->name, "~~s~~")) {
                                                        /* If we're not actually within a gosub, this will fail, but the
                                                         * second time through, it will get set.  If we are within gosub,
                                                         * the second time through is redundant, but acceptable. */
@@ -3431,7 +3453,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                case PV_VARDEC:
                        pr = new_prio();
                        pr->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"%s=$[%s]", p->u1.str, p->u2.val);
+                       snprintf(buf1, BUF_SIZE, "%s=$[%s]", p->u1.str, p->u2.val);
                        if (!ast_compat_app_set) {
                                pr->app = strdup("MSet");
                        } else {
@@ -3446,7 +3468,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                case PV_LOCALVARDEC:
                        pr = new_prio();
                        pr->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
+                       snprintf(buf1, BUF_SIZE, "LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
                        if (!ast_compat_app_set) {
                                pr->app = strdup("MSet");
                        } else {
@@ -3471,16 +3493,16 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                if (!mother_exten)
                                        pr->appargs = strdup(p->u1.list->u1.str);
                                else {  /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */ 
-                                       snprintf(buf1,sizeof(buf1),"%s,%s", mother_exten->name, p->u1.list->u1.str);
+                                       snprintf(buf1, BUF_SIZE, "%s,%s", mother_exten->name, p->u1.list->u1.str);
                                        pr->appargs = strdup(buf1);
                                }
                                
                        } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
-                               snprintf(buf1,sizeof(buf1),"%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
+                               snprintf(buf1, BUF_SIZE, "%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
                                pr->app = strdup("Goto");
                                pr->appargs = strdup(buf1);
                        } else if (p->u1.list->next && p->u1.list->next->next) {
-                               snprintf(buf1,sizeof(buf1),"%s,%s,%s", p->u1.list->u1.str, 
+                               snprintf(buf1, BUF_SIZE, "%s,%s,%s", p->u1.list->u1.str, 
                                                p->u1.list->next->u1.str,
                                                p->u1.list->next->next->u1.str);
                                pr->app = strdup("Goto");
@@ -3502,7 +3524,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        control_statement_count++;
                        loop_break_save = exten->loop_break; /* save them, then restore before leaving */
                        loop_continue_save = exten->loop_continue;
-                       snprintf(new_label,sizeof(new_label),"for-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "for_%s_%d", label, control_statement_count);
                        for_init = new_prio();
                        for_inc = new_prio();
                        for_test = new_prio();
@@ -3527,7 +3549,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                strp2 = strchr(p->u1.for_init, '=');
                                *(strp+1) = 0;
                                strcat(buf2,"$[");
-                               strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
+                               strncat(buf2,strp2+1, BUF_SIZE-strlen(strp2+1)-2);
                                strcat(buf2,"]");
                                for_init->appargs = strdup(buf2);
                        } else {
@@ -3576,7 +3598,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                strp2 = strchr(p->u3.for_inc, '=');
                                *(strp+1) = 0;
                                strcat(buf2,"$[");
-                               strncat(buf2,strp2+1, sizeof(buf2)-strlen(strp2+1)-2);
+                               strncat(buf2,strp2+1, BUF_SIZE-strlen(strp2+1)-2);
                                strcat(buf2,"]");
                                for_inc->appargs = strdup(buf2);
                                if (!ast_compat_app_set) {
@@ -3618,11 +3640,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                        }
                                }
                        }
-                       snprintf(buf1,sizeof(buf1),"$[%s]",p->u2.for_test);
+                       snprintf(buf1, BUF_SIZE, "$[%s]",p->u2.for_test);
                        for_test->app = 0;
                        for_test->appargs = strdup(buf1);
                        for_loop->goto_true = for_test;
-                       snprintf(buf1,sizeof(buf1),"Finish for-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish for_%s_%d", label, control_statement_count);
                        for_end->app = strdup("NoOp");
                        for_end->appargs = strdup(buf1);
                        /* link & load! */
@@ -3633,7 +3655,9 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        exten->loop_break = for_end;
                        exten->loop_continue = for_inc;
                        
-                       gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context); /* this will link in all the statements here */
+                       if (gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                               return -1;
+                       }
                        
                        linkprio(exten, for_inc, mother_exten);
                        linkprio(exten, for_loop, mother_exten);
@@ -3649,7 +3673,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        control_statement_count++;
                        loop_break_save = exten->loop_break; /* save them, then restore before leaving */
                        loop_continue_save = exten->loop_continue;
-                       snprintf(new_label,sizeof(new_label),"while-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "while_%s_%d", label, control_statement_count);
                        while_test = new_prio();
                        while_loop = new_prio();
                        while_end = new_prio();
@@ -3657,11 +3681,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        while_test->goto_false = while_end;
                        while_loop->type = AEL_CONTROL1; /* simple goto */
                        while_end->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
+                       snprintf(buf1, BUF_SIZE, "$[%s]",p->u1.str);
                        while_test->app = 0;
                        while_test->appargs = strdup(buf1);
                        while_loop->goto_true = while_test;
-                       snprintf(buf1,sizeof(buf1),"Finish while-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish while_%s_%d", label, control_statement_count);
                        while_end->app = strdup("NoOp");
                        while_end->appargs = strdup(buf1);
 
@@ -3671,7 +3695,9 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        exten->loop_break = while_end;
                        exten->loop_continue = while_test;
                        
-                       gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the while body statements here */
+                       if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the while body statements here */
+                               return -1;
+                       }
 
                        linkprio(exten, while_loop, mother_exten);
                        linkprio(exten, while_end, mother_exten);
@@ -3687,15 +3713,15 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        local_control_statement_count = control_statement_count;
                        loop_break_save = exten->loop_break; /* save them, then restore before leaving */
                        loop_continue_save = exten->loop_continue;
-                       snprintf(new_label,sizeof(new_label),"sw-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "sw_%s_%d", label, control_statement_count);
                        switch_test = new_prio();
                        switch_end = new_prio();
                        switch_test->type = AEL_APPCALL;
                        switch_end->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",control_statement_count, p->u1.str);
+                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", control_statement_count, p->u1.str);
                        switch_test->app = strdup("Goto");
                        switch_test->appargs = strdup(buf1);
-                       snprintf(buf1,sizeof(buf1),"Finish switch-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish switch_%s_%d", label, control_statement_count);
                        switch_end->app = strdup("NoOp");
                        switch_end->appargs = strdup(buf1);
                        switch_end->origin = p;
@@ -3728,11 +3754,13 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                        switch_case->loop_continue = exten->loop_continue;
                                        
                                        linkexten(exten,switch_case);
-                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s", local_control_statement_count, p2->u1.str);
+                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s", local_control_statement_count, p2->u1.str);
                                        switch_case->name = strdup(buf1);
-                                       snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, p2->u1.str, local_control_statement_count);
+                                       snprintf(new_label, BUF_SIZE, "sw_%s_%s_%d", label, p2->u1.str, local_control_statement_count);
                                        
-                                       gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the case body statements here */
+                                       if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the case body statements here */
+                                               return -1;
+                                       }
 
                                        /* here is where we write code to "fall thru" to the next case... if there is one... */
                                        for (p3=p2->u2.statements; p3; p3=p3->next) {
@@ -3746,7 +3774,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, p2->next->u1.str);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_PATTERN) {
@@ -3754,14 +3782,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
                                                        gen_match_to_pattern(p2->next->u1.str, buf2);
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10", local_control_statement_count, buf2);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_DEFAULT) {
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-.,10",local_control_statement_count);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (!p2->next) {
@@ -3777,7 +3805,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                struct ael_priority *np2 = new_prio();
                                                np2->type = AEL_APPCALL;
                                                np2->app = strdup("NoOp");
-                                               snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
+                                               snprintf(buf, BUF_SIZE, "End of Extension %s", switch_case->name);
                                                np2->appargs = strdup(buf);
                                                linkprio(switch_case, np2, mother_exten);
                                                switch_case-> return_target = np2;
@@ -3798,13 +3826,15 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                        /* the break/continue locations are inherited from parent */
                                        switch_case->loop_break = exten->loop_break;
                                        switch_case->loop_continue = exten->loop_continue;
-                                       
+
                                        linkexten(exten,switch_case);
-                                       snprintf(buf1,sizeof(buf1),"_sw-%d-%s", local_control_statement_count, p2->u1.str);
+                                       snprintf(buf1, BUF_SIZE, "_sw_%d_%s", local_control_statement_count, p2->u1.str);
                                        switch_case->name = strdup(buf1);
-                                       snprintf(new_label,sizeof(new_label),"sw-%s-%s-%d", label, p2->u1.str, local_control_statement_count);
-                                       
-                                       gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the while body statements here */
+                                       snprintf(new_label, BUF_SIZE, "sw_%s_%s_%d", label, p2->u1.str, local_control_statement_count);
+
+                                       if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the while body statements here */
+                                               return -1;
+                                       }
                                        /* here is where we write code to "fall thru" to the next case... if there is one... */
                                        for (p3=p2->u2.statements; p3; p3=p3->next) {
                                                if (!p3->next)
@@ -3817,7 +3847,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, p2->next->u1.str);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_PATTERN) {
@@ -3825,14 +3855,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
                                                        gen_match_to_pattern(p2->next->u1.str, buf2);
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, buf2);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_DEFAULT) {
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-.,10",local_control_statement_count);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (!p2->next) {
@@ -3884,11 +3914,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                        switch_null->context = this_context;
                                        switch_null->is_switch = 1;
                                        switch_empty = new_prio();
-                                       snprintf(buf1,sizeof(buf1),"sw-%d-.,10",local_control_statement_count);
+                                       snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
                                        switch_empty->app = strdup("Goto");
                                        switch_empty->appargs = strdup(buf1);
                                        linkprio(switch_null, switch_empty, mother_exten);
-                                       snprintf(buf1,sizeof(buf1),"sw-%d-", local_control_statement_count);
+                                       snprintf(buf1, BUF_SIZE, "sw_%d_", local_control_statement_count);
                                        switch_null->name = strdup(buf1);
                                        switch_null->loop_break = exten->loop_break;
                                        switch_null->loop_continue = exten->loop_continue;
@@ -3898,12 +3928,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                        switch_case->loop_break = exten->loop_break;
                                        switch_case->loop_continue = exten->loop_continue;
                                        linkexten(exten,switch_case);
-                                       snprintf(buf1,sizeof(buf1),"_sw-%d-.", local_control_statement_count);
+                                       snprintf(buf1, BUF_SIZE, "_sw_%d_.", local_control_statement_count);
                                        switch_case->name = strdup(buf1);
                                        
-                                       snprintf(new_label,sizeof(new_label),"sw-%s-default-%d", label, local_control_statement_count);
+                                       snprintf(new_label, BUF_SIZE, "sw_%s_default_%d", label, local_control_statement_count);
                                        
-                                       gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context); /* this will link in all the default:  body statements here */
+                                       if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the default:  body statements here */
+                                               return -1;
+                                       }
                                        
                                        /* here is where we write code to "fall thru" to the next case... if there is one... */
                                        for (p3=p2->u2.statements; p3; p3=p3->next) {
@@ -3917,7 +3949,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, p2->next->u1.str);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_PATTERN) {
@@ -3925,14 +3957,14 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
                                                        gen_match_to_pattern(p2->next->u1.str, buf2);
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-%s,10",local_control_statement_count, buf2);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (p2->next && p2->next->type == PV_DEFAULT) {
                                                        fall_thru = new_prio();
                                                        fall_thru->type = AEL_APPCALL;
                                                        fall_thru->app = strdup("Goto");
-                                                       snprintf(buf1,sizeof(buf1),"sw-%d-.,10",local_control_statement_count);
+                                                       snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
                                                        fall_thru->appargs = strdup(buf1);
                                                        linkprio(switch_case, fall_thru, mother_exten);
                                                } else if (!p2->next) {
@@ -3967,7 +3999,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                case PV_MACRO_CALL:
                        pr = new_prio();
                        pr->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"%s,s,1", p->u1.str);
+                       snprintf(buf1, BUF_SIZE, "%s,~~s~~,1", p->u1.str);
                        first = 1;
                        for (p2 = p->u2.arglist; p2; p2 = p2->next) {
                                if (first)
@@ -4031,11 +4063,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
 
                case PV_IFTIME:
                        control_statement_count++;
-                       snprintf(new_label,sizeof(new_label),"iftime-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "iftime_%s_%d", label, control_statement_count);
                        
                        if_test = new_prio();
                        if_test->type = AEL_IFTIME_CONTROL;
-                       snprintf(buf1,sizeof(buf1),"%s,%s,%s,%s",
+                       snprintf(buf1, BUF_SIZE, "%s,%s,%s,%s",
                                         p->u1.list->u1.str, 
                                         p->u1.list->next->u1.str, 
                                         p->u1.list->next->next->u1.str, 
@@ -4046,7 +4078,7 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
 
                        if_end = new_prio();
                        if_end->type = AEL_APPCALL;
-                       snprintf(buf1,sizeof(buf1),"Finish iftime-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish iftime_%s_%d", label, control_statement_count);
                        if_end->app = strdup("NoOp");
                        if_end->appargs = strdup(buf1);
 
@@ -4076,12 +4108,15 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        
                        /* now, put the body of the if here */
                        
-                       gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
+                       if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                               return -1;
+                       }
                        
                        if (p->u3.else_statements) {
                                linkprio(exten, if_skip, mother_exten);
-                               gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
-
+                               if (gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                                       return -1;
+                               }
                        }
                        
                        linkprio(exten, if_end, mother_exten);
@@ -4091,19 +4126,19 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                case PV_RANDOM:
                case PV_IF:
                        control_statement_count++;
-                       snprintf(new_label,sizeof(new_label),"if-%s-%d", label, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "if_%s_%d", label, control_statement_count);
                        
                        if_test = new_prio();
                        if_end = new_prio();
                        if_test->type = AEL_IF_CONTROL;
                        if_end->type = AEL_APPCALL;
                        if ( p->type == PV_RANDOM )
-                               snprintf(buf1,sizeof(buf1),"$[${RAND(0,99)} < (%s)]",p->u1.str);
+                               snprintf(buf1, BUF_SIZE, "$[${RAND(0,99)} < (%s)]", p->u1.str);
                        else
-                               snprintf(buf1,sizeof(buf1),"$[%s]",p->u1.str);
+                               snprintf(buf1, BUF_SIZE, "$[%s]", p->u1.str);
                        if_test->app = 0;
                        if_test->appargs = strdup(buf1);
-                       snprintf(buf1,sizeof(buf1),"Finish if-%s-%d", label, control_statement_count);
+                       snprintf(buf1, BUF_SIZE, "Finish if_%s_%d", label, control_statement_count);
                        if_end->app = strdup("NoOp");
                        if_end->appargs = strdup(buf1);
                        if_test->origin = p;
@@ -4123,12 +4158,15 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        
                        /* now, put the body of the if here */
                        
-                       gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context); /* this will link in all the statements here */
+                       if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                               return -1;
+                       }
                        
                        if (p->u3.else_statements) {
                                linkprio(exten, if_skip, mother_exten);
-                               gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context); /* this will link in all the statements here */
-
+                               if (gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context)) { /* this will link in all the statements here */
+                                       return -1;
+                               }
                        }
                        
                        linkprio(exten, if_end, mother_exten);
@@ -4136,7 +4174,9 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        break;
 
                case PV_STATEMENTBLOCK:
-                       gen_prios(exten, label, p->u1.list, mother_exten, this_context ); /* recurse into the block */
+                       if (gen_prios(exten, label, p->u1.list, mother_exten, this_context)) { /* recurse into the block */
+                               return -1;
+                       }
                        break;
 
                case PV_CATCH:
@@ -4156,9 +4196,11 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        switch_case->context = this_context;
                        linkexten(exten,switch_case);
                        switch_case->name = strdup(p->u1.str);
-                       snprintf(new_label,sizeof(new_label),"catch-%s-%d",p->u1.str, control_statement_count);
+                       snprintf(new_label, BUF_SIZE, "catch_%s_%d",p->u1.str, control_statement_count);
                        
-                       gen_prios(switch_case, new_label, p->u2.statements,mother_exten,this_context); /* this will link in all the catch body statements here */
+                       if (gen_prios(switch_case, new_label, p->u2.statements, mother_exten,this_context)) { /* this will link in all the catch body statements here */
+                               return -1;
+                       }
                        if (switch_case->return_needed) { /* returns now generate a Return() app call, no longer a goto to the end of the exten */
                                char buf[2000];
                                struct ael_priority *np2 = new_prio();
@@ -4175,6 +4217,10 @@ static void gen_prios(struct ael_extension *exten, char *label, pval *statement,
                        break;
                }
        }
+       free(buf1);
+       free(buf2);
+       free(new_label);
+       return 0;
 }
 
 void set_priorities(struct ael_extension *exten)
@@ -4388,8 +4434,22 @@ static void fix_gotos_in_extensions(struct ael_extension *exten)
        }
 }
 
+static int context_used(struct ael_extension *exten_list, struct ast_context *context)
+{
+       struct ael_extension *exten;
+       /* Check the simple elements first */
+       if (ast_walk_context_extensions(context, NULL) || ast_walk_context_includes(context, NULL) || ast_walk_context_ignorepats(context, NULL) || ast_walk_context_switches(context, NULL)) {
+               return 1;
+       }
+       for (exten = exten_list; exten; exten = exten->next_exten) {
+               if (exten->context == context) {
+                       return 1;
+               }
+       }
+       return 0;
+}
 
-void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root)
+int ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root)
 {
        pval *p,*p2;
        struct ast_context *context;
@@ -4424,7 +4484,7 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                        
                        exten = new_exten();
                        exten->context = context;
-                       exten->name = strdup("s");
+                       exten->name = strdup("~~s~~");
                        argc = 1;
                        for (lp=p->u2.arglist; lp; lp=lp->next) {
                                /* for each arg, set up a "Set" command */
@@ -4442,7 +4502,9 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                        }
                        
                        /* CONTAINS APPCALLS, CATCH, just like extensions... */
-                       gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context );
+                       if (gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context)) {
+                               return -1;
+                       }
                        if (exten->return_needed) {  /* most likely, this will go away */
                                struct ael_priority *np2 = new_prio();
                                np2->type = AEL_APPCALL;
@@ -4484,7 +4546,9 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                                        if ( p2->u3.hints )
                                                exten->hints = strdup(p2->u3.hints);
                                        exten->regexten = p2->u4.regexten;
-                                       gen_prios(exten, p->u1.str, p2->u2.statements, 0, context );
+                                       if (gen_prios(exten, p->u1.str, p2->u2.statements, 0, context)) {
+                                               return -1;
+                                       }
                                        if (exten->return_needed) { /* returns don't generate a goto eoe (end of extension) any more, just a Return() app call) */
                                                struct ael_priority *np2 = new_prio();
                                                np2->type = AEL_APPCALL;
@@ -4565,6 +4629,71 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
                        
                }
        }
+
+       /* Create default "h" bubble context */
+       if (ast_custom_function_find("DIALPLAN_EXISTS") && ast_custom_function_find("STACK_PEEK")) {
+               int i;
+               const char *h_context = "ael-builtin-h-bubble";
+               struct ael_priority *np;
+               struct {
+                       int priority;
+                       const char *app;
+                       const char *arg;
+               } steps[] = {
+                       /* Start high, to avoid conflict with existing h extensions */
+                       { 1, "Goto", "9991" },
+                       /* Save the context, because after the StackPop, it disappears */
+                       { 9991, "Set", "~~parentcxt~~=${STACK_PEEK(1,c,1)}" },
+                       /* If we're not in a Gosub frame, exit */
+                       { 9992, "GotoIf", "$[\"${~~parentcxt~~}\"=\"\"]?9996" },
+                       /* Check for an "h" extension in that context */
+                       { 9993, "GotoIf", "${DIALPLAN_EXISTS(${~~parentcxt~~},h,1)}?9994:9996" },
+                       /* Pop off the stack frame to prevent an infinite loop */
+                       { 9994, "StackPop", "" },
+                       /* Finally, go there. */
+                       { 9995, "Goto", "${~~parentcxt~~},h,1" },
+                       /* Just an empty priority for jumping out early */
+                       { 9996, "NoOp", "" }
+               };
+               context = ast_context_find_or_create(local_contexts, local_table, h_context, registrar);
+               if (context_used(exten_list, context)) {
+                       int found = 0;
+                       while (!found) {
+                               /* Pick a new context name that is not used. */
+                               char h_context_template[] = "/tmp/ael-builtin-h-bubble-XXXXXX";
+                               int fd = mkstemp(h_context_template);
+                               unlink(h_context_template);
+                               close(fd);
+                               context = ast_context_find_or_create(local_contexts, local_table, h_context_template + 5, registrar);
+                               found = !context_used(exten_list, context);
+                       }
+                       h_context = ast_get_context_name(context);
+               }
+               exten = new_exten();
+               exten->context = context;
+               exten->name = strdup("h");
+
+               for (i = 0; i < ARRAY_LEN(steps); i++) {
+                       np = new_prio();
+                       np->type = AEL_APPCALL;
+                       np->priority_num = steps[i].priority;
+                       np->app = strdup(steps[i].app);
+                       np->appargs = strdup(steps[i].arg);
+                       linkprio(exten, np, NULL);
+               }
+               attach_exten(&exten_list, exten);
+
+               /* Include the default "h" bubble context in each macro context */
+               for (exten = exten_list; exten; exten = exten->next_exten) {
+                       /* All macros contain a "~~s~~" extension, and it's the first created.  If
+                        * we perchance get a non-macro context, it's no big deal; the logic is
+                        * designed to exit out smoothly if not called from within a Gosub. */
+                       if (!strcmp(exten->name, "~~s~~")) {
+                               ast_context_add_include2(exten->context, h_context, registrar);
+                       }
+               }
+       }
+
        /* moved these from being done after a macro or extension were processed,
           to after all processing is done, for the sake of fixing gotos to labels inside cases... */
        /* I guess this would be considered 2nd pass of compiler now... */
@@ -4572,6 +4701,7 @@ void ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *l
        add_extensions(exten_list);   /* actually makes calls to create priorities in ast_contexts -- feeds dialplan to asterisk */
        destroy_extensions(exten_list);  /* all that remains is an empty husk, discard of it as is proper */
        
+       return 0;
 }
 
 
@@ -5763,7 +5893,7 @@ pval* pvalGlobalsWalkStatements( pval *p, pval **next_statement )
 {
        if (!pvalCheckType(p, "pvalGlobalsWalkStatements", PV_GLOBALS))
                return 0;
-       if (!next_statement) {
+       if (!*next_statement) {
                *next_statement = p;
                return p;
        } else {
@@ -5784,7 +5914,7 @@ void pvalTopLevAddObject( pval *p, pval *contextOrObj )
 
 pval *pvalTopLevWalkObjects(pval *p, pval **next_obj )
 {
-       if (!next_obj) {
+       if (!*next_obj) {
                *next_obj = p;
                return p;
        } else {