ソースを参照

Fix or mark some issues from static code analysis

bel 10 年 前
コミット
125049597b
3 ファイル変更36 行追加24 行削除
  1. 2 5
      src/civetweb.c
  2. 2 2
      src/main.c
  3. 32 17
      src/mod_lua.inl

+ 2 - 5
src/civetweb.c

@@ -3758,7 +3758,7 @@ base64_decode(const unsigned char *src, int src_len, char *dst, size_t *dst_len)
 		}
 
 		d = b64reverse(i + 3 >= src_len ? 0 : src[i + 3]);
-		if (c == 254) {
+		if (d == 254) {
 			return i + 3;
 		}
 
@@ -7657,7 +7657,7 @@ int mg_upload(struct mg_connection *conn, const char *destination_dir)
 	for (;;) {
 		/* Pull in headers */
 		/* assert(len >= 0 && len <= (int) sizeof(buf)); */
-		if (len < 0 || len > (int)sizeof(buf)) {
+		if ((len < 0) || (len > (int)sizeof(buf))) {
 			break;
 		}
 		while ((n = mg_read(conn, buf + len, sizeof(buf) - (size_t)len)) > 0) {
@@ -9969,9 +9969,6 @@ static void *worker_thread_run(void *thread_func_param)
 	ctx->num_threads--;
 	(void)pthread_cond_signal(&ctx->thread_cond);
 	/* assert(ctx->num_threads >= 0); */
-	if (ctx->num_threads < 0) {
-		return NULL;
-	}
 	(void)pthread_mutex_unlock(&ctx->thread_mutex);
 
 	pthread_setspecific(sTlsKey, NULL);

+ 2 - 2
src/main.c

@@ -396,12 +396,12 @@ static void read_config_file(const char *config_file, char **options)
 {
 	char line[MAX_CONF_FILE_LINE_SIZE], *p;
 	FILE *fp = NULL;
-	size_t i, j, cmd_line_opts_start = 1, line_no = 0;
+	size_t i, j, line_no = 0;
 
 	fp = fopen(config_file, "r");
 
 	/* If config file was set in command line and open failed, die */
-	if (cmd_line_opts_start == 2 && fp == NULL) {
+	if (fp == NULL) {
 		die("Cannot open config file %s: %s", config_file, strerror(errno));
 	}
 

+ 32 - 17
src/mod_lua.inl

@@ -231,6 +231,8 @@ static int lsp_connect(lua_State *L)
 			reg_string(L, "host", lua_tostring(L, -4));
 			luaL_getmetatable(L, LUASOCKET);
 			lua_setmetatable(L, -2);
+			/* TODO (high): The metatable misses a _gc method to free the
+			 * sock object -> currently lsp_connect is a resource leak. */
 		}
 	} else {
 		return luaL_error(
@@ -811,6 +813,8 @@ static int lwebsock_write(lua_State *L)
 	lua_gettable(L, LUA_REGISTRYINDEX);
 	ws = (struct lua_websock_data *)lua_touserdata(L, -1);
 
+	(void)pthread_mutex_lock(&(ws->ws_mutex));
+
 	if (num_args == 1) {
 		/* just one text: send it to all client */
 		if (lua_isstring(L, 1)) {
@@ -879,8 +883,12 @@ static int lwebsock_write(lua_State *L)
 			}
 		}
 	} else {
+		(void)pthread_mutex_unlock(&ws->ws_mutex);
 		return luaL_error(L, "invalid websocket write() call");
 	}
+
+	(void)pthread_mutex_unlock(&ws->ws_mutex);
+
 #else
 	(void)(L);           /* unused */
 #endif
@@ -1127,7 +1135,7 @@ void lua_civet_open_all_libs(lua_State *L)
 
 static void prepare_lua_environment(struct mg_context *ctx,
                                     struct mg_connection *conn,
-                                    struct lua_websock_data *conn_list,
+                                    struct lua_websock_data *ws_conn_list,
                                     lua_State *L,
                                     const char *script_name,
                                     int lua_env_type)
@@ -1142,14 +1150,14 @@ static void prepare_lua_environment(struct mg_context *ctx,
 	lua_register(L, "connect", lsp_connect);
 
 	/* Store context in the registry */
-	if (ctx) {
+	if (ctx != NULL) {
 		lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
 		lua_pushlightuserdata(L, (void *)ctx);
 		lua_settable(L, LUA_REGISTRYINDEX);
 	}
-	if (conn_list) {
+	if (ws_conn_list != NULL) {
 		lua_pushlightuserdata(L, (void *)&lua_regkey_connlist);
-		lua_pushlightuserdata(L, (void *)conn_list);
+		lua_pushlightuserdata(L, (void *)ws_conn_list);
 		lua_settable(L, LUA_REGISTRYINDEX);
 	}
 
@@ -1203,15 +1211,20 @@ static void prepare_lua_environment(struct mg_context *ctx,
 	reg_function(L, "get_response_code_text", lsp_get_response_code_text);
 
 	reg_string(L, "version", CIVETWEB_VERSION);
-	reg_string(L, "document_root", ctx->config[DOCUMENT_ROOT]);
-	reg_string(L, "auth_domain", ctx->config[AUTHENTICATION_DOMAIN]);
+
+	reg_string(L, "script_name", script_name);
+
+	if (ctx != NULL) {
+		reg_string(L, "document_root", ctx->config[DOCUMENT_ROOT]);
+		reg_string(L, "auth_domain", ctx->config[AUTHENTICATION_DOMAIN]);
 #if defined(USE_WEBSOCKET)
-	reg_string(L, "websocket_root", ctx->config[WEBSOCKET_ROOT]);
+		reg_string(L, "websocket_root", ctx->config[WEBSOCKET_ROOT]);
 #endif
-	reg_string(L, "script_name", script_name);
 
-	if (ctx->systemName != NULL) {
-		reg_string(L, "system", ctx->systemName);
+
+		if (ctx->systemName != NULL) {
+			reg_string(L, "system", ctx->systemName);
+		}
 	}
 
 	/* Export connection specific info */
@@ -1227,13 +1240,15 @@ static void prepare_lua_environment(struct mg_context *ctx,
 	                  "mg.onerror = function(e) mg.write('\\nLua error:\\n', "
 	                  "debug.traceback(e, 1)) end"));
 
-	/* Preload */
-	if (ctx->config[LUA_PRELOAD_FILE] != NULL) {
-		IGNORE_UNUSED_RESULT(luaL_dofile(L, ctx->config[LUA_PRELOAD_FILE]));
-	}
+	if (ctx != NULL) {
+		/* Preload */
+		if (ctx->config[LUA_PRELOAD_FILE] != NULL) {
+			IGNORE_UNUSED_RESULT(luaL_dofile(L, ctx->config[LUA_PRELOAD_FILE]));
+		}
 
-	if (ctx->callbacks.init_lua != NULL) {
-		ctx->callbacks.init_lua(conn, L);
+		if (ctx->callbacks.init_lua != NULL) {
+			ctx->callbacks.init_lua(conn, L);
+		}
 	}
 }
 
@@ -1420,10 +1435,10 @@ static void *lua_websocket_new(const char *script, struct mg_connection *conn)
 		ws = &(*shared_websock_list)->ws;
 		ws->script = mg_strdup(script); /* TODO (low): handle OOM */
 		pthread_mutex_init(&(ws->ws_mutex), NULL);
+		(void)pthread_mutex_lock(&(ws->ws_mutex));
 		ws->state = lua_newstate(lua_allocator, NULL);
 		ws->conn[0] = conn;
 		ws->references = 1;
-		(void)pthread_mutex_lock(&(ws->ws_mutex));
 		prepare_lua_environment(
 		    conn->ctx, NULL, ws, ws->state, script, LUA_ENV_TYPE_LUA_WEBSOCKET);
 		err = luaL_loadfile(ws->state, script);