vector: Prevent NULL argument to memcpy.
[asterisk/asterisk.git] / utils / check_expr.c
old mode 100755 (executable)
new mode 100644 (file)
index 531af49..201fa04
  * at the top of the source tree.
  */
 
-#include <stdio.h>
-#include <stddef.h>
-#include <stdarg.h>
-#include <string.h>
-#include <stdlib.h>
-#include <../include/asterisk/ast_expr.h>
-
-int global_lineno = 1;
-int global_expr_count = 0;
-int global_expr_max_size = 0;
-int global_expr_tot_size = 0;
-int global_warn_count = 0;
-int global_OK_count = 0;
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/ast_expr.h"
+
+#define AST_API_MODULE 1
+#include "asterisk/inline_api.h"
+
+#define AST_API_MODULE 1
+#include "asterisk/lock.h"
+
+#ifndef DEBUG_THREADS
+enum ast_lock_type {
+               AST_MUTEX,
+               AST_RDLOCK,
+               AST_WRLOCK,
+};
+#endif
+#ifdef DEBUG_THREADLOCALS
+#define MALLOC_FAILURE_MSG \
+       ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);
+
+void * attribute_malloc __ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func);
+
+void * attribute_malloc __ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func)
+{
+       void *p;
+
+       if (!(p = calloc(num, len)))
+               MALLOC_FAILURE_MSG;
+
+       return p;
+}
+#endif
+
+#ifdef DEBUG_THREADS
+#ifdef HAVE_BKTR
+void ast_store_lock_info(enum ast_lock_type type, const char *filename,
+                       int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt);
+void ast_store_lock_info(enum ast_lock_type type, const char *filename,
+                       int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt)
+{
+    /* not a lot to do in a standalone w/o threading! */
+}
+
+void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt);
+void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
+{
+    /* not a lot to do in a standalone w/o threading! */
+}
+
+int __ast_bt_get_addresses(struct ast_bt *bt);
+int __ast_bt_get_addresses(struct ast_bt *bt)
+{
+       /* Suck it, you stupid utils directory! */
+       return 0;
+}
+char **__ast_bt_get_symbols(void **addresses, size_t num_frames);
+char **__ast_bt_get_symbols(void **addresses, size_t num_frames)
+{
+       char **foo = calloc(num_frames, sizeof(char *) + 1);
+       if (foo) {
+               int i;
+               for (i = 0; i < num_frames; i++) {
+                       foo[i] = (char *) foo + sizeof(char *) * num_frames;
+               }
+       }
+       return foo;
+}
+#else
+void ast_store_lock_info(enum ast_lock_type type, const char *filename,
+                       int line_num, const char *func, const char *lock_name, void *lock_addr);
+void ast_store_lock_info(enum ast_lock_type type, const char *filename,
+                       int line_num, const char *func, const char *lock_name, void *lock_addr)
+{
+    /* not a lot to do in a standalone w/o threading! */
+}
+
+void ast_remove_lock_info(void *lock_addr);
+void ast_remove_lock_info(void *lock_addr)
+{
+    /* not a lot to do in a standalone w/o threading! */
+}
+#endif /* HAVE_BKTR */
+
+void ast_suspend_lock_info(void *lock_addr)
+{
+}
+void ast_restore_lock_info(void *lock_addr)
+{
+}
+void ast_mark_lock_acquired(void *);
+void ast_mark_lock_acquired(void *foo)
+{
+    /* not a lot to do in a standalone w/o threading! */
+}
+#endif /* DEBUG_THREADS */
+
+
+static int global_lineno = 1;
+static int global_expr_count=0;
+static int global_expr_max_size=0;
+static int global_expr_tot_size=0;
+static int global_warn_count=0;
+static int global_OK_count=0;
 
 struct varz
 {
@@ -41,13 +136,13 @@ struct varz *global_varlist;
 
 /* Our own version of ast_log, since the expr parser uses it. */
 
-void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
+void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf,5,6)));
 
 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
 {
        va_list vars;
        va_start(vars,fmt);
-       
+
        printf("LOG: lev:%d file:%s  line:%d func: %s  ",
                   level, file, line, function);
        vprintf(fmt, vars);
@@ -55,6 +150,21 @@ void ast_log(int level, const char *file, int line, const char *function, const
        va_end(vars);
 }
 
+char *find_var(const char *varname);
+void set_var(const char *varname, const char *varval);
+unsigned int check_expr(char* buffer, char* error_report);
+int check_eval(char *buffer, char *error_report);
+void parse_file(const char *fname);
+
+int ast_add_profile(const char *x, uint64_t scale) { return 0;}
+int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
+{
+        int ret;
+        ret = *p;
+        *p += v;
+        return ret;
+}
+
 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
 {
        struct varz *t;
@@ -66,84 +176,101 @@ char *find_var(const char *varname) /* the list should be pretty short, if there
        return 0;
 }
 
+void set_var(const char *varname, const char *varval);
+
 void set_var(const char *varname, const char *varval)
 {
-       struct varz *t = calloc(1,sizeof(struct varz));
+       struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
+       if (!t)
+               return;
        strcpy(t->varname, varname);
        strcpy(t->varval, varval);
        t->next = global_varlist;
        global_varlist = t;
 }
 
-int check_expr(char *buffer, char *error_report)
+unsigned int check_expr(char* buffer, char* error_report)
 {
-       char *cp;
-       int oplen = 0;
-       int warn_found = 0;
+       char* cp;
+       unsigned int warn_found = 0;
 
        error_report[0] = 0;
-       
-       for (cp=buffer;*cp;cp++) {
-               
-               if (*cp == '|' 
-                       || *cp == '&'
-                       || *cp == '='
-                       || *cp == '>'
-                       || *cp == '<'
-                       || *cp == '+'
-                       || *cp == '-'
-                       || *cp == '*'
-                       || *cp == '/'
-                       || *cp == '%'
-                       || *cp == '?'
-                       || *cp == ':'
-                       /*      || *cp == '('
-                               || *cp == ')' These are pretty hard to track, as they are in funcalls, etc. */
-                       || *cp == '"') {
-                       if (*cp == '"') {
+
+       for (cp = buffer; *cp; ++cp)
+       {
+               switch (*cp)
+               {
+                       case '"':
                                /* skip to the other end */
-                               cp++;
-                               while (*cp && *cp != '"')
-                                       cp++;
-                               if (*cp == 0) {
-                                       fprintf(stderr,"Trouble? Unterminated double quote found at line %d\n",
-                                                       global_lineno);
-                               }
-                       }
-                       else {
-                               if ((*cp == '>'||*cp == '<' ||*cp=='!') && (*(cp+1) == '=')) {
-                                       oplen = 2;
+                               while (*(++cp) && *cp != '"') ;
+
+                               if (*cp == 0)
+                               {
+                                       fprintf(stderr,
+                                               "Trouble? Unterminated double quote found at line %d\n",
+                                               global_lineno);
                                }
-                               else {
-                                       oplen = 1;
+                               break;
+
+                       case '>':
+                       case '<':
+                       case '!':
+                               if (   (*(cp + 1) == '=')
+                                       && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
+                               {
+                                       char msg[200];
+                                       snprintf(msg,
+                                               sizeof(msg),
+                                               "WARNING: line %d: '%c%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check!\n",
+                                               global_lineno, *cp, *(cp + 1));
+                                       strcat(error_report, msg);
+                                       ++global_warn_count;
+                                       ++warn_found;
                                }
-                               
-                               if ((cp > buffer && *(cp-1) != ' ') || *(cp+oplen) != ' ') {
-                                       char tbuf[1000];
-                                       if (oplen == 1)
-                                               sprintf(tbuf,"WARNING: line %d,  '%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check!\n",
-                                                               global_lineno, *cp);
-                                       else
-                                               sprintf(tbuf,"WARNING: line %d,  '%c%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check!\n",
-                                                               global_lineno, *cp, *(cp+1));
-                                       strcat(error_report,tbuf);
-
-                                       global_warn_count++;
-                                       warn_found++;
+                               break;
+
+                       case '|':
+                       case '&':
+                       case '=':
+                       case '+':
+                       case '-':
+                       case '*':
+                       case '/':
+                       case '%':
+                       case '?':
+                       case ':':
+                               if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
+                               {
+                                       char msg[200];
+                                       snprintf(msg,
+                                               sizeof(msg),
+                                               "WARNING: line %d: '%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check!\n",
+                                               global_lineno, *cp );
+                                       strcat(error_report, msg);
+                                       ++global_warn_count;
+                                       ++warn_found;
                                }
-                       }
+                               break;
                }
        }
+
        return warn_found;
 }
 
+int check_eval(char *buffer, char *error_report);
+
+struct ast_custom_function *ast_custom_function_find(const char *name);
+
+struct ast_custom_function *ast_custom_function_find(const char *name)
+{
+       return 0;
+}
+
 int check_eval(char *buffer, char *error_report)
 {
-       char *cp, *ep, *xp;
+       char *cp, *ep;
        char s[4096];
        char evalbuf[80000];
-       int oplen = 0;
-       int warn_found = 0;
        int result;
 
        error_report[0] = 0;
@@ -153,13 +280,13 @@ int check_eval(char *buffer, char *error_report)
                if (*cp == '$' && *(cp+1) == '{') {
                        int brack_lev = 1;
                        char *xp= cp+2;
-                       
+
                        while (*xp) {
                                if (*xp == '{')
                                        brack_lev++;
                                else if (*xp == '}')
                                        brack_lev--;
-                               
+
                                if (brack_lev == 0)
                                        break;
                                xp++;
@@ -167,7 +294,7 @@ int check_eval(char *buffer, char *error_report)
                        if (*xp == '}') {
                                char varname[200];
                                char *val;
-                               
+
                                strncpy(varname,cp+2, xp-cp-2);
                                varname[xp-cp-2] = 0;
                                cp = xp;
@@ -199,7 +326,7 @@ int check_eval(char *buffer, char *error_report)
        *ep++ = 0;
 
        /* now, run the test */
-       result = ast_expr(evalbuf, s, sizeof(s));
+       result = ast_expr(evalbuf, s, sizeof(s),NULL);
        if (result) {
                sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
                return 1;
@@ -210,6 +337,8 @@ int check_eval(char *buffer, char *error_report)
 }
 
 
+void parse_file(const char *fname);
+
 void parse_file(const char *fname)
 {
        FILE *f = fopen(fname,"r");
@@ -217,18 +346,18 @@ void parse_file(const char *fname)
        int c1;
        char last_char= 0;
        char buffer[30000]; /* I sure hope no expr gets this big! */
-       
+
        if (!f) {
-               fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n");
+               fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
                exit(20);
        }
        if (!l) {
                fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
                exit(21);
        }
-       
+
        global_lineno = 1;
-       
+
        while ((c1 = fgetc(f)) != EOF) {
                if (c1 == '\n')
                        global_lineno++;
@@ -239,7 +368,7 @@ void parse_file(const char *fname)
                                int bufcount = 0;
                                int retval;
                                char error_report[30000];
-                               
+
                                while ((c1 = fgetc(f)) != EOF) {
                                        if (c1 == '[')
                                                bracklev++;
@@ -251,7 +380,7 @@ void parse_file(const char *fname)
                                                fclose(l);
                                                printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
                                        }
-                                       
+
                                        if (bracklev == 0)
                                                break;
                                        buffer[bufcount++] = c1;
@@ -263,18 +392,18 @@ void parse_file(const char *fname)
                                        printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
                                        exit(22);
                                }
-                               
+
                                buffer[bufcount] = 0;
                                /* update stats */
                                global_expr_tot_size += bufcount;
                                global_expr_count++;
                                if (bufcount > global_expr_max_size)
                                        global_expr_max_size = bufcount;
-                               
+
                                retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
                                if (retval != 0) {
                                        /* print error report */
-                                       printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n", 
+                                       printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
                                                   global_lineno, buffer);
                                        fprintf(l, "%s", error_report);
                                }
@@ -295,19 +424,29 @@ void parse_file(const char *fname)
                   global_warn_count,
                   global_expr_max_size,
                   (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
-       
+
        fclose(f);
        fclose(l);
 }
 
 
-main(int argc,char **argv)
+int main(int argc,char **argv)
 {
        int argc1;
        char *eq;
-       
+
        if (argc < 2) {
+               printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
+               printf("              and run them thru the parser, looking for problems\n");
                printf("Hey-- give me a path to an extensions.conf file!\n");
+               printf(" You can also follow the file path with a series of variable decls,\n");
+               printf("     of the form, varname=value, each separated from the next by spaces.\n");
+               printf("     (this might allow you to avoid division by zero messages, check that math\n");
+               printf("      is being done correctly, etc.)\n");
+               printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
+               printf("  you to possible problems where you might be expecting those operators as part of a string.\n");
+        printf("  (to include operators in a string, wrap with double quotes!)\n");
+
                exit(19);
        }
        global_varlist = 0;
@@ -319,6 +458,7 @@ main(int argc,char **argv)
        }
 
        /* parse command args for x=y and set varz */
-       
+
        parse_file(argv[1]);
+       return 0;
 }