Get rid of the depth argument to dfilter_macro_apply(); have an internal
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Tue, 15 Nov 2011 04:26:38 +0000 (04:26 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Tue, 15 Nov 2011 04:26:38 +0000 (04:26 +0000)
routine that does all the work and that takes a depth argumen, and an
external routine that calls that internal routine with a depth argument
of 0.  The depth is only of use internally, to avoid infinite recursion.

When recursing with that routine, pass depth+1 as the depth value,
rather than passing depth and incrementing it afterwards; the latter
doesn't prevent infinite recursion.  (Thanks and a tip of the hat to
Clang Cat for catching this.)

Squelch some other (harmless) warnings from Clang Cat.

Clean up indentation.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@39838 f5534014-38df-0310-8fa8-9805f1628bb7

epan/dfilter/dfilter-macro.c
epan/dfilter/dfilter-macro.h
epan/dfilter/dfilter.c

index c2699f8dd74cbbe660e22188a9f9ddf28ab4fd09..a0933fd6927bfdc2ad97fb06e6e9fb352392dfda 100644 (file)
@@ -217,7 +217,7 @@ static gchar* dfilter_macro_resolve(gchar* name, gchar** args, const gchar** err
 }
 
 
-gchar* dfilter_macro_apply(const gchar* text, guint depth, const gchar** error) {
+static gchar* dfilter_macro_apply_recurse(const gchar* text, guint depth, const gchar** error) {
        enum { OUTSIDE, STARTING, NAME, ARGS } state = OUTSIDE;
        GString* out;
        GString* name = NULL;
@@ -241,150 +241,156 @@ gchar* dfilter_macro_apply(const gchar* text, guint depth, const gchar** error)
                if (args) { \
                        while(args->len) { void* p = g_ptr_array_remove_index_fast(args,0); if (p) g_free(p); } \
                        g_ptr_array_free(args,TRUE); \
-                       args = NULL; } } while(0)
+                       args = NULL; \
+               } \
+       } while(0)
 
-               *error = NULL;
-               out = g_string_sized_new(64);
-
-               while(1) {
-                       c = *r++;
-
-                       switch(state) {
-                               case OUTSIDE: {
-                                       switch(c) {
-                                               case '\0': {
-                                                       goto finish;
-                                               } case '$': {
-                                                       state = STARTING;
-                                                       break;
-                                               } default: {
-                                                       g_string_append_c(out,c);
-                                                       break;
-                                               }
+       *error = NULL;
+       out = g_string_sized_new(64);
+
+       while(1) {
+               c = *r++;
+
+               switch(state) {
+                       case OUTSIDE: {
+                               switch(c) {
+                                       case '\0': {
+                                               goto finish;
+                                       } case '$': {
+                                               state = STARTING;
+                                               break;
+                                       } default: {
+                                               g_string_append_c(out,c);
+                                               break;
                                        }
-                                       break;
-                               } case STARTING: {
-                                       switch (c) {
-                                               case '{': {
-                                                       args = g_ptr_array_new();
-                                                       arg = g_string_sized_new(32);
-                                                       name = g_string_sized_new(32);
+                               }
+                               break;
+                       } case STARTING: {
+                               switch (c) {
+                                       case '{': {
+                                               args = g_ptr_array_new();
+                                               arg = g_string_sized_new(32);
+                                               name = g_string_sized_new(32);
 
-                                                       state = NAME;
+                                               state = NAME;
 
-                                                       break;
-                                               } case '\0': {
-                                                       g_string_append_c(out,'$');
+                                               break;
+                                       } case '\0': {
+                                               g_string_append_c(out,'$');
 
-                                                       goto finish;
-                                               } default: {
-                                                       g_string_append_c(out,'$');
-                                                       g_string_append_c(out,c);
+                                               goto finish;
+                                       } default: {
+                                               g_string_append_c(out,'$');
+                                               g_string_append_c(out,c);
 
-                                                       state = OUTSIDE;
+                                               state = OUTSIDE;
 
-                                                       break;
-                                               }
+                                               break;
                                        }
-                                       break;
-                               } case NAME: {
-                                       if ( isalnum((int)c) || c == '_' || c == '-' || c == '.' ) {
-                                               g_string_append_c(name,c);
-                                       } else if ( c == ':') {
-                                               state = ARGS;
-                                       } else if ( c == '}') {
-                                               gchar* resolved;
+                               }
+                               break;
+                       } case NAME: {
+                               if ( isalnum((int)c) || c == '_' || c == '-' || c == '.' ) {
+                                       g_string_append_c(name,c);
+                               } else if ( c == ':') {
+                                       state = ARGS;
+                               } else if ( c == '}') {
+                                       gchar* resolved;
 
-                                               g_ptr_array_add(args,NULL);
+                                       g_ptr_array_add(args,NULL);
 
-                                               resolved = dfilter_macro_resolve(name->str, (gchar**)args->pdata, error);
-                                               if (*error) goto on_error;
+                                       resolved = dfilter_macro_resolve(name->str, (gchar**)args->pdata, error);
+                                       if (*error) goto on_error;
 
-                                               changed = TRUE;
+                                       changed = TRUE;
 
-                                               g_string_append(out,resolved);
+                                       g_string_append(out,resolved);
 
-                                               FREE_ALL();
+                                       FREE_ALL();
 
-                                               state = OUTSIDE;
-                                       } else if ( c == '\0') {
+                                       state = OUTSIDE;
+                               } else if ( c == '\0') {
+                                       *error = "end of filter in the middle of a macro expression";
+                                       goto on_error;
+                               } else {
+                                       *error = "invalid char in macro name";
+                                       goto on_error;
+                               }
+                               break;
+                       } case ARGS: {
+                               switch(c) {
+                                       case '\0': {
                                                *error = "end of filter in the middle of a macro expression";
                                                goto on_error;
-                                       } else {
-                                               *error = "invalid char in macro name";
-                                               goto on_error;
-                                       }
-                                       break;
-                               } case ARGS: {
-                                       switch(c) {
-                                               case '\0': {
-                                                       *error = "end of filter in the middle of a macro expression";
-                                                       goto on_error;
-                                               } case ';': {
-                                                       g_ptr_array_add(args,arg->str);
-                                                       g_string_free(arg,FALSE);
+                                       } case ';': {
+                                               g_ptr_array_add(args,arg->str);
+                                               g_string_free(arg,FALSE);
 
-                                                       arg = g_string_sized_new(32);
-                                                       break;
-                                               } case '\\': {
-                                                       c = *r++;
-                                                       if (c) {
-                                                               g_string_append_c(arg,c);
-                                                               break;
-                                                       } else {
-                                                               *error = "end of filter in the middle of a macro expression";
-                                                               goto on_error;
-                                                       }
-                                               } default: {
+                                               arg = g_string_sized_new(32);
+                                               break;
+                                       } case '\\': {
+                                               c = *r++;
+                                               if (c) {
                                                        g_string_append_c(arg,c);
                                                        break;
-                                               } case '}': {
-                                                       gchar* resolved;
-                                                       g_ptr_array_add(args,arg->str);
-                                                       g_ptr_array_add(args,NULL);
+                                               } else {
+                                                       *error = "end of filter in the middle of a macro expression";
+                                                       goto on_error;
+                                               }
+                                       } default: {
+                                               g_string_append_c(arg,c);
+                                               break;
+                                       } case '}': {
+                                               gchar* resolved;
+                                               g_ptr_array_add(args,arg->str);
+                                               g_ptr_array_add(args,NULL);
 
-                                                       g_string_free(arg,FALSE);
-                                                       arg = NULL;
+                                               g_string_free(arg,FALSE);
+                                               arg = NULL;
 
-                                                       resolved = dfilter_macro_resolve(name->str, (gchar**)args->pdata, error);
-                                                       if (*error) goto on_error;
+                                               resolved = dfilter_macro_resolve(name->str, (gchar**)args->pdata, error);
+                                               if (*error) goto on_error;
 
-                                                       changed = TRUE;
+                                               changed = TRUE;
 
-                                                       g_string_append(out,resolved);
+                                               g_string_append(out,resolved);
 
-                                                       FREE_ALL();
+                                               FREE_ALL();
 
-                                                       state = OUTSIDE;
-                                                       break;
-                                               }
+                                               state = OUTSIDE;
+                                               break;
                                        }
-                                       break;
                                }
+                               break;
                        }
                }
+       }
 
 finish:
-               {
-                       FREE_ALL();
+       {
+               FREE_ALL();
 
-                       if (changed) {
-                               gchar* resolved = dfilter_macro_apply(out->str, depth++, error);
-                               g_string_free(out,TRUE);
-                               return (*error) ? NULL : resolved;
-                       } else {
-                               gchar* out_str = ep_strdup(out->str);
-                               g_string_free(out,TRUE);
-                               return out_str;
-                       }
-               }
-on_error:
-               {
-                       FREE_ALL();
-                       if (! *error) *error = "unknown error in macro expression";
+               if (changed) {
+                       gchar* resolved = dfilter_macro_apply_recurse(out->str, depth + 1, error);
                        g_string_free(out,TRUE);
-                       return NULL;
+                       return (*error) ? NULL : resolved;
+               } else {
+                       gchar* out_str = ep_strdup(out->str);
+                       g_string_free(out,TRUE);
+                       return out_str;
                }
+       }
+on_error:
+       {
+               FREE_ALL();
+               if (! *error) *error = "unknown error in macro expression";
+               g_string_free(out,TRUE);
+               return NULL;
+       }
+}
+
+gchar* dfilter_macro_apply(const gchar* text, const gchar** error) {
+       return dfilter_macro_apply_recurse(text, 0, error);
 }
 
 static void macro_update(void* mp, const gchar** error) {
@@ -429,7 +435,7 @@ static void macro_update(void* mp, const gchar** error) {
                                *(w++) = *(r++);
                                break;
                        case '\0':
-                               *(w++) = *(r++);
+                               *w = *r;
                                goto done;
                        case '\\':
                                *(w++) = *(++r);
index 9646570d7b964e8dc77806826dd6ec6453939a92..5e06aada38161443c2386e6cf78f794a8c033a5e 100644 (file)
@@ -48,7 +48,7 @@ void dfilter_macro_save(const gchar*, gchar**);
 void dfilter_macro_dump(void);
 
 /* applies all macros to the given text and returns the resulting string or NULL on failure */
-gchar* dfilter_macro_apply(const gchar* text, guint depth, const gchar** error);
+gchar* dfilter_macro_apply(const gchar* text, const gchar** error);
 
 void dfilter_macro_init(void);
 
index 143693123d4c3cc15bf002aa11395fbed7ce4ed8..668d8191ad374bd022d3eb1e0c7aa7c56e893eb8 100644 (file)
@@ -230,7 +230,7 @@ dfilter_compile(const gchar *text, dfilter_t **dfp)
 
        dfilter_error_msg = NULL;
 
-       if ( !( text = dfilter_macro_apply(text, 0, &dfilter_error_msg) ) ) {
+       if ( !( text = dfilter_macro_apply(text, &dfilter_error_msg) ) ) {
                return FALSE;
        }