浏览代码

Merge pull request #21 from dalgaaf/wip-da-sca

Various fixes from SCA and Coverity
bel2125 11 年之前
父节点
当前提交
76813c8d0b
共有 7 个文件被更改,包括 39 次插入30 次删除
  1. 5 0
      .gitignore
  2. 1 1
      examples/chat/chat.c
  3. 1 1
      src/CivetServer.cpp
  4. 23 19
      src/civetweb.c
  5. 6 6
      src/main.c
  6. 1 1
      src/mod_lua.inl
  7. 2 2
      test/unit_test.c

+ 5 - 0
.gitignore

@@ -230,3 +230,8 @@ pip-log.txt
 ##########################
 ##########################
 requests.db
 requests.db
 
 
+##########################
+## Files created by ctags
+##########################
+tags
+

+ 1 - 1
examples/chat/chat.c

@@ -105,7 +105,7 @@ static char *messages_to_json(long last_id)
         // messages (it may be too small if the ringbuffer is full and all
         // messages (it may be too small if the ringbuffer is full and all
         // messages are large. in this case asserts will trigger).
         // messages are large. in this case asserts will trigger).
         len += snprintf(buf + len, sizeof(buf) - len,
         len += snprintf(buf + len, sizeof(buf) - len,
-                        "{user: '%s', text: '%s', timestamp: %lu, id: %lu},",
+                        "{user: '%s', text: '%s', timestamp: %lu, id: %ld},",
                         message->user, message->text, message->timestamp, message->id);
                         message->user, message->text, message->timestamp, message->id);
         assert(len > 0);
         assert(len > 0);
         assert((size_t) len < sizeof(buf));
         assert((size_t) len < sizeof(buf));

+ 1 - 1
src/CivetServer.cpp

@@ -150,7 +150,7 @@ CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst, bool i
 
 
     dst.clear();
     dst.clear();
     for (i = j = 0; i < (int)src_len; i++, j++) {
     for (i = j = 0; i < (int)src_len; i++, j++) {
-        if (src[i] == '%' && i < (int)src_len - 2 &&
+        if (i < (int)src_len - 2 && src[i] == '%' &&
             isxdigit(* (const unsigned char *) (src + i + 1)) &&
             isxdigit(* (const unsigned char *) (src + i + 1)) &&
             isxdigit(* (const unsigned char *) (src + i + 2))) {
             isxdigit(* (const unsigned char *) (src + i + 2))) {
             a = tolower(* (const unsigned char *) (src + i + 1));
             a = tolower(* (const unsigned char *) (src + i + 1));

+ 23 - 19
src/civetweb.c

@@ -363,7 +363,7 @@ static void DEBUG_TRACE_FUNC(const char *func, unsigned line, const char *fmt, .
 
 
   va_list args;
   va_list args;
   flockfile(stdout);
   flockfile(stdout);
-  printf("*** %lu.%p.%s.%d: ",
+  printf("*** %lu.%p.%s.%u: ",
          (unsigned long) time(NULL), (void *) pthread_self(),
          (unsigned long) time(NULL), (void *) pthread_self(),
          func, line);
          func, line);
   va_start(args, fmt);
   va_start(args, fmt);
@@ -398,7 +398,7 @@ static void * mg_malloc_ex(size_t size, const char * file, unsigned line) {
         memory = (void *)(((char*)data)+sizeof(size_t));
         memory = (void *)(((char*)data)+sizeof(size_t));
     }
     }
 
 
-    sprintf(mallocStr, "MEM: %p %5u alloc   %7u %4u --- %s:%u\n", memory, size, totalMemUsed, blockCount, file, line);
+    sprintf(mallocStr, "MEM: %p %5lu alloc   %7lu %4lu --- %s:%u\n", memory, (unsigned long)size, totalMemUsed, blockCount, file, line);
 #if defined(_WIN32)
 #if defined(_WIN32)
     OutputDebugStringA(mallocStr);
     OutputDebugStringA(mallocStr);
 #else
 #else
@@ -426,7 +426,7 @@ static void mg_free_ex(void * memory, const char * file, unsigned line) {
         size = *(size_t*)data;
         size = *(size_t*)data;
         totalMemUsed -= size;
         totalMemUsed -= size;
         blockCount--;
         blockCount--;
-        sprintf(mallocStr, "MEM: %p %5u free    %7u %4u --- %s:%u\n", memory, size, totalMemUsed, blockCount, file, line);
+        sprintf(mallocStr, "MEM: %p %5lu free    %7lu %4lu --- %s:%u\n", memory, (unsigned long)size, totalMemUsed, blockCount, file, line);
 #if defined(_WIN32)
 #if defined(_WIN32)
         OutputDebugStringA(mallocStr);
         OutputDebugStringA(mallocStr);
 #else
 #else
@@ -441,23 +441,25 @@ static void * mg_realloc_ex(void * memory, size_t newsize, const char * file, un
 
 
     char mallocStr[256];
     char mallocStr[256];
     void * data;
     void * data;
+    void * _realloc;
     size_t oldsize;
     size_t oldsize;
 
 
     if (newsize) {
     if (newsize) {
         if (memory) {
         if (memory) {
             data = (void *)(((char*)memory)-sizeof(size_t));
             data = (void *)(((char*)memory)-sizeof(size_t));
             oldsize = *(size_t*)data;
             oldsize = *(size_t*)data;
-            data = realloc(data, newsize+sizeof(size_t));
-            if (data) {
+            _realloc = realloc(data, newsize+sizeof(size_t));
+            if (_realloc) {
+                data = _realloc;
                 totalMemUsed -= oldsize;
                 totalMemUsed -= oldsize;
-                sprintf(mallocStr, "MEM: %p %5u r-free  %7u %4u --- %s:%u\n", memory, oldsize, totalMemUsed, blockCount, file, line);
+                sprintf(mallocStr, "MEM: %p %5lu r-free  %7lu %4lu --- %s:%u\n", memory, (unsigned long)oldsize, totalMemUsed, blockCount, file, line);
 #if defined(_WIN32)
 #if defined(_WIN32)
                 OutputDebugStringA(mallocStr);
                 OutputDebugStringA(mallocStr);
 #else
 #else
                 DEBUG_TRACE("%s", mallocStr);
                 DEBUG_TRACE("%s", mallocStr);
 #endif
 #endif
                 totalMemUsed += newsize;
                 totalMemUsed += newsize;
-                sprintf(mallocStr, "MEM: %p %5u r-alloc %7u %4u --- %s:%u\n", memory, newsize, totalMemUsed, blockCount, file, line);
+                sprintf(mallocStr, "MEM: %p %5lu r-alloc %7lu %4lu --- %s:%u\n", memory, (unsigned long)newsize, totalMemUsed, blockCount, file, line);
 #if defined(_WIN32)
 #if defined(_WIN32)
                 OutputDebugStringA(mallocStr);
                 OutputDebugStringA(mallocStr);
 #else
 #else
@@ -471,6 +473,7 @@ static void * mg_realloc_ex(void * memory, size_t newsize, const char * file, un
 #else
 #else
                 DEBUG_TRACE("MEM: realloc failed\n");
                 DEBUG_TRACE("MEM: realloc failed\n");
 #endif
 #endif
+                return _realloc;
             }
             }
         } else {
         } else {
             data = mg_malloc_ex(newsize, file, line);
             data = mg_malloc_ex(newsize, file, line);
@@ -2389,7 +2392,7 @@ int mg_url_decode(const char *src, int src_len, char *dst,
 #define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
 #define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
 
 
     for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
     for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
-        if (src[i] == '%' && i < src_len - 2 &&
+        if (i < src_len - 2 && src[i] == '%' &&
             isxdigit(* (const unsigned char *) (src + i + 1)) &&
             isxdigit(* (const unsigned char *) (src + i + 1)) &&
             isxdigit(* (const unsigned char *) (src + i + 2))) {
             isxdigit(* (const unsigned char *) (src + i + 2))) {
             a = tolower(* (const unsigned char *) (src + i + 1));
             a = tolower(* (const unsigned char *) (src + i + 1));
@@ -3308,11 +3311,11 @@ int mg_modify_passwords_file(const char *fname, const char *domain,
 
 
     /* Do not allow control characters like newline in user name and domain.
     /* Do not allow control characters like newline in user name and domain.
        Do not allow excessively long names either. */
        Do not allow excessively long names either. */
-    for (i=0; user[i]!=0 && i<255; i++) {
+    for (i=0; i<255 && user[i]!=0; i++) {
         if (iscntrl(user[i])) return 0;
         if (iscntrl(user[i])) return 0;
     }
     }
     if (user[i]) return 0;
     if (user[i]) return 0;
-    for (i=0; domain[i]!=0 && i<255; i++) {
+    for (i=0; i<255 && domain[i]!=0; i++) {
         if (iscntrl(domain[i])) return 0;
         if (iscntrl(domain[i])) return 0;
     }
     }
     if (domain[i]) return 0;
     if (domain[i]) return 0;
@@ -5386,6 +5389,7 @@ int mg_upload(struct mg_connection *conn, const char *destination_dir)
         assert(len >= 0 && len <= (int) sizeof(buf));
         assert(len >= 0 && len <= (int) sizeof(buf));
         while ((n = mg_read(conn, buf + len, sizeof(buf) - len)) > 0) {
         while ((n = mg_read(conn, buf + len, sizeof(buf) - len)) > 0) {
             len += n;
             len += n;
+            assert(len <= (int) sizeof(buf));
         }
         }
         if ((headers_len = get_request_len(buf, len)) <= 0) {
         if ((headers_len = get_request_len(buf, len)) <= 0) {
             break;
             break;
@@ -5513,7 +5517,7 @@ static void redirect_to_https_port(struct mg_connection *conn, int ssl_index)
 
 
 void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata)
 void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata)
 {
 {
-    struct mg_request_handler_info *tmp_rh, *lastref = 0;
+    struct mg_request_handler_info *tmp_rh, *lastref = NULL;
     size_t urilen = strlen(uri);
     size_t urilen = strlen(uri);
 
 
     /* first see it the uri exists */
     /* first see it the uri exists */
@@ -5754,9 +5758,9 @@ static void close_all_listening_sockets(struct mg_context *ctx)
         ctx->listening_sockets[i].sock = INVALID_SOCKET;
         ctx->listening_sockets[i].sock = INVALID_SOCKET;
     }
     }
     mg_free(ctx->listening_sockets);
     mg_free(ctx->listening_sockets);
-    ctx->listening_sockets=0;
+    ctx->listening_sockets = NULL;
     mg_free(ctx->listening_ports);
     mg_free(ctx->listening_ports);
-    ctx->listening_ports=0;
+    ctx->listening_ports = NULL;
 }
 }
 
 
 static int is_valid_port(unsigned int port)
 static int is_valid_port(unsigned int port)
@@ -5786,7 +5790,7 @@ static int parse_port_string(const struct vec *vec, struct socket *so)
         so->lsa.sin.sin_addr.s_addr = htonl((a << 24) | (b << 16) | (c << 8) | d);
         so->lsa.sin.sin_addr.s_addr = htonl((a << 24) | (b << 16) | (c << 8) | d);
         so->lsa.sin.sin_port = htons((uint16_t) port);
         so->lsa.sin.sin_port = htons((uint16_t) port);
 #if defined(USE_IPV6)
 #if defined(USE_IPV6)
-    } else if (sscanf(vec->ptr, "[%49[^]]]:%d%n", buf, &port, &len) == 2 &&
+    } else if (sscanf(vec->ptr, "[%49[^]]]:%u%n", buf, &port, &len) == 2 &&
                inet_pton(AF_INET6, buf, &so->lsa.sin6.sin6_addr)) {
                inet_pton(AF_INET6, buf, &so->lsa.sin6.sin6_addr)) {
         /* IPv6 address, e.g. [3ffe:2a00:100:7031::1]:8080 */
         /* IPv6 address, e.g. [3ffe:2a00:100:7031::1]:8080 */
         so->lsa.sin6.sin6_family = AF_INET6;
         so->lsa.sin6.sin6_family = AF_INET6;
@@ -6247,7 +6251,7 @@ void mg_close_connection(struct mg_connection *conn)
     mg_free(conn);
     mg_free(conn);
 }
 }
 
 
-struct mg_connection *mg_connect(const char *host, int port, int use_ssl,
+static struct mg_connection *mg_connect(const char *host, int port, int use_ssl,
                                  char *ebuf, size_t ebuf_len)
                                  char *ebuf, size_t ebuf_len)
 {
 {
     static struct mg_context fake_ctx;
     static struct mg_context fake_ctx;
@@ -6514,7 +6518,7 @@ static void *worker_thread_run(void *thread_func_param)
     assert(ctx->num_threads >= 0);
     assert(ctx->num_threads >= 0);
     (void) pthread_mutex_unlock(&ctx->mutex);
     (void) pthread_mutex_unlock(&ctx->mutex);
 
 
-    pthread_setspecific(sTlsKey, 0);
+    pthread_setspecific(sTlsKey, NULL);
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
     CloseHandle(tls.pthread_cond_helper_mutex);
     CloseHandle(tls.pthread_cond_helper_mutex);
 #endif
 #endif
@@ -6701,7 +6705,7 @@ static void master_thread_run(void *thread_func_param)
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
     CloseHandle(tls.pthread_cond_helper_mutex);
     CloseHandle(tls.pthread_cond_helper_mutex);
 #endif
 #endif
-    pthread_setspecific(sTlsKey, 0);
+    pthread_setspecific(sTlsKey, NULL);
 
 
     /* Signal mg_stop() that we're done.
     /* Signal mg_stop() that we're done.
        WARNING: This must be the very last thing this
        WARNING: This must be the very last thing this
@@ -6801,7 +6805,7 @@ void mg_stop(struct mg_context *ctx)
 #endif /* _WIN32 && !__SYMBIAN32__ */
 #endif /* _WIN32 && !__SYMBIAN32__ */
 }
 }
 
 
-void get_system_name(char **sysName)
+static void get_system_name(char **sysName)
 {
 {
 #if defined(_WIN32)
 #if defined(_WIN32)
 #if !defined(__SYMBIAN32__)
 #if !defined(__SYMBIAN32__)
@@ -6869,7 +6873,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
         ctx->callbacks = *callbacks;
         ctx->callbacks = *callbacks;
     }
     }
     ctx->user_data = user_data;
     ctx->user_data = user_data;
-    ctx->request_handlers = 0;
+    ctx->request_handlers = NULL;
 
 
 #if defined(USE_LUA) && defined(USE_WEBSOCKET)
 #if defined(USE_LUA) && defined(USE_WEBSOCKET)
     ctx->shared_lua_websockets = 0;
     ctx->shared_lua_websockets = 0;

+ 6 - 6
src/main.c

@@ -51,6 +51,7 @@
 
 
 #define getcwd(a,b) _getcwd(a,b)
 #define getcwd(a,b) _getcwd(a,b)
 extern char *_getcwd(char *buf, size_t size);
 extern char *_getcwd(char *buf, size_t size);
+static int guard = 0;                   /* test if any dialog is already open */
 
 
 #ifndef PATH_MAX
 #ifndef PATH_MAX
 #define PATH_MAX MAX_PATH
 #define PATH_MAX MAX_PATH
@@ -83,7 +84,6 @@ static char *server_name;               /* Set by init_server_name() */
 static char *icon_name;                 /* Set by init_server_name() */
 static char *icon_name;                 /* Set by init_server_name() */
 static char config_file[PATH_MAX] = ""; /* Set by process_command_line_arguments() */
 static char config_file[PATH_MAX] = ""; /* Set by process_command_line_arguments() */
 static struct mg_context *ctx;          /* Set by start_civetweb() */
 static struct mg_context *ctx;          /* Set by start_civetweb() */
-static int guard = 0;                   /* test if any dialog is already open */
 
 
 #if !defined(CONFIG_FILE)
 #if !defined(CONFIG_FILE)
 #define CONFIG_FILE "civetweb.conf"
 #define CONFIG_FILE "civetweb.conf"
@@ -235,7 +235,7 @@ static int set_option(char **options, const char *name, const char *value)
     int i, type;
     int i, type;
     const struct mg_option *default_options = mg_get_valid_options();
     const struct mg_option *default_options = mg_get_valid_options();
 
 
-    for (i = 0; main_config_options[i].name != 0; i++) {
+    for (i = 0; main_config_options[i].name != NULL; i++) {
         if (0==strcmp(name, main_config_options[i].name)) {
         if (0==strcmp(name, main_config_options[i].name)) {
             /* This option is evaluated by main.c, not civetweb.c - just skip it and return OK */
             /* This option is evaluated by main.c, not civetweb.c - just skip it and return OK */
             return 1;
             return 1;
@@ -243,7 +243,7 @@ static int set_option(char **options, const char *name, const char *value)
     }
     }
 
 
     type = CONFIG_TYPE_UNKNOWN;
     type = CONFIG_TYPE_UNKNOWN;
-    for (i = 0; default_options[i].name != 0; i++) {
+    for (i = 0; default_options[i].name != NULL; i++) {
         if (!strcmp(default_options[i].name, name)) {
         if (!strcmp(default_options[i].name, name)) {
             type = default_options[i].type;
             type = default_options[i].type;
         }
         }
@@ -415,7 +415,7 @@ static void init_server_name(int argc, const char *argv[])
             server_name = (char*)(argv[i+1]);
             server_name = (char*)(argv[i+1]);
         }
         }
     }
     }
-    icon_name = 0;
+    icon_name = NULL;
     for (i=0; i<argc-1; i++) {
     for (i=0; i<argc-1; i++) {
         if ((argv[i][0]=='-') && (0==strcmp(argv[i]+1, main_config_options[OPTION_ICON].name))) {
         if ((argv[i][0]=='-') && (0==strcmp(argv[i]+1, main_config_options[OPTION_ICON].name))) {
             icon_name = (char*)(argv[i+1]);
             icon_name = (char*)(argv[i+1]);
@@ -1516,7 +1516,7 @@ withApplication:@"TextEdit"];
 
 
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
-    init_server_name(argc, argv);
+    init_server_name(argc, (const char **)argv);
     start_civetweb(argc, argv);
     start_civetweb(argc, argv);
 
 
     [NSAutoreleasePool new];
     [NSAutoreleasePool new];
@@ -1576,7 +1576,7 @@ int main(int argc, char *argv[])
 #else
 #else
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
-    init_server_name(argc, argv);
+    init_server_name(argc, (const char **)argv);
     start_civetweb(argc, argv);
     start_civetweb(argc, argv);
     printf("%s started on port(s) %s with web root [%s]\n",
     printf("%s started on port(s) %s with web root [%s]\n",
            server_name, mg_get_option(ctx, "listening_ports"),
            server_name, mg_get_option(ctx, "listening_ports"),

+ 1 - 1
src/mod_lua.inl

@@ -901,7 +901,7 @@ struct file *filep, struct lua_State *ls)
         fileno(filep->fp), 0)) == MAP_FAILED) {
         fileno(filep->fp), 0)) == MAP_FAILED) {
             lsp_send_err(conn, ls, "mmap(%s, %zu, %d): %s", path, (size_t) filep->size,
             lsp_send_err(conn, ls, "mmap(%s, %zu, %d): %s", path, (size_t) filep->size,
                 fileno(filep->fp), strerror(errno));
                 fileno(filep->fp), strerror(errno));
-    } else if ((L = ls != NULL ? ls : lua_newstate(lua_allocator, NULL)) == NULL) {
+    } else if ((L = (ls != NULL ? ls : lua_newstate(lua_allocator, NULL))) == NULL) {
         send_http_error(conn, 500, http_500_error, "%s", "luaL_newstate failed");
         send_http_error(conn, 500, http_500_error, "%s", "luaL_newstate failed");
     } else {
     } else {
         /* We're not sending HTTP headers here, Lua page must do it. */
         /* We're not sending HTTP headers here, Lua page must do it. */

+ 2 - 2
test/unit_test.c

@@ -865,13 +865,13 @@ int __cdecl main(void) {
     if (f) {
     if (f) {
         fclose(f);
         fclose(f);
     } else {
     } else {
-        printf("Error: Test directory does not contain hello.txt\n", buffer);
+        printf("Error: Test directory does not contain hello.txt\n");
     }
     }
     f = fopen("unit_test.c", "r");
     f = fopen("unit_test.c", "r");
     if (f) {
     if (f) {
         fclose(f);
         fclose(f);
     } else {
     } else {
-        printf("Error: Test directory does not contain unit_test.c\n", buffer);
+        printf("Error: Test directory does not contain unit_test.c\n");
     }
     }
 
 
     /* test local functions */
     /* test local functions */