]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
mod_limit: Add more error checking to hash api/app
authorMathieu Rene <mrene@avgs.ca>
Sun, 12 Apr 2009 18:35:09 +0000 (18:35 +0000)
committerMathieu Rene <mrene@avgs.ca>
Sun, 12 Apr 2009 18:35:09 +0000 (18:35 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@13007 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/applications/mod_limit/mod_limit.c

index 1266de65627b2960941199b4e50f21048e4f9d27..3cab89dd93648cae5fedd77a9c19bf5c675b54c8 100644 (file)
@@ -537,6 +537,8 @@ SWITCH_STANDARD_APP(hash_function)
        char *hash_key = NULL;
        char *value = NULL;
        
+       switch_mutex_lock(globals.db_hash_mutex);
+       
        if (!switch_strlen_zero(data)) {
                mydata = strdup(data);
                switch_assert(mydata);
@@ -544,14 +546,15 @@ SWITCH_STANDARD_APP(hash_function)
        }
        
        if (argc < 3 || !argv[0]) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "USAGE: hash %s\n", HASH_USAGE);
-               goto end;
+               goto usage;
        }
        
        hash_key = switch_mprintf("%s_%s", argv[1], argv[2]);
        
-       switch_mutex_lock(globals.db_hash_mutex);
        if (!strcasecmp(argv[0], "insert")) {
+               if (argc < 4) {
+                       goto usage;
+               }
                if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
                        free(value);
                        switch_core_hash_delete(globals.db_hash, hash_key);
@@ -564,10 +567,17 @@ SWITCH_STANDARD_APP(hash_function)
                        switch_safe_free(value);
                        switch_core_hash_delete(globals.db_hash, hash_key);
                }
+       } else {
+               goto usage;
        }
-       switch_mutex_unlock(globals.db_hash_mutex);
        
-end:
+       goto done;
+
+usage:
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "USAGE: hash %s\n", HASH_USAGE);
+
+done:
+       switch_mutex_unlock(globals.db_hash_mutex);
        switch_safe_free(mydata);
        switch_safe_free(hash_key);
 }
@@ -590,16 +600,14 @@ SWITCH_STANDARD_API(hash_api_function)
        }
        
        if (argc < 3 || !argv[0]) {
-               stream->write_function(stream, "-ERR Usage: hash %s\n", HASH_API_USAGE);
-               goto end;
+               goto usage;
        }
        
        hash_key = switch_mprintf("%s_%s", argv[1], argv[2]);
        
        if (!strcasecmp(argv[0], "insert")) {
                if (argc < 4) {
-                       stream->write_function(stream, "-ERR Usage: hash %s\n", HASH_API_USAGE);
-                       goto end;
+                       goto usage;
                }
                if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
                        switch_safe_free(value);
@@ -613,15 +621,24 @@ SWITCH_STANDARD_API(hash_api_function)
                if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
                        switch_safe_free(value);
                        switch_core_hash_delete(globals.db_hash, hash_key);
+                       stream->write_function(stream, "+OK\n");
+               } else {
+                       stream->write_function(stream, "-ERR Not found\n");
                }
-               stream->write_function(stream, "+OK\n");
        } else if (!strcasecmp(argv[0], "select")) {
                if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
                        stream->write_function(stream, "%s", value);
                }
+       } else {
+               goto usage;
        }
        
-end:
+       goto done;
+       
+usage:
+       stream->write_function(stream, "-ERR Usage: hash %s\n", HASH_API_USAGE);
+       
+done:
        switch_mutex_unlock(globals.db_hash_mutex);
        switch_safe_free(mydata);
        switch_safe_free(hash_key);