Переглянути джерело

Fix some PVS Studio warnings (#597)

bel2125 7 роки тому
батько
коміт
3235a2ecae
3 змінених файлів з 43 додано та 22 видалено
  1. 28 20
      src/civetweb.c
  2. 2 1
      src/handle_form.inl
  3. 13 1
      src/timer.inl

+ 28 - 20
src/civetweb.c

@@ -15185,34 +15185,41 @@ initialize_ssl(char *ebuf, size_t ebuf_len)
 
 	/* allocate mutex array, if required */
 	if (num_locks == 0) {
+		/* No mutex array required */
 		ssl_mutexes = NULL;
-	} else if ((ssl_mutexes = (pthread_mutex_t *)mg_malloc(size)) == NULL) {
-		mg_snprintf(NULL,
-		            NULL, /* No truncation check for ebuf */
-		            ebuf,
-		            ebuf_len,
-		            "%s: cannot allocate mutexes: %s",
-		            __func__,
-		            ssl_error());
-		DEBUG_TRACE("%s", ebuf);
-		return 0;
-	}
+	} else {
+		/* Mutex array required - allocate it */
+		ssl_mutexes = (pthread_mutex_t *)mg_malloc(size);
 
-	/* initialize required mutex array */
-	for (i = 0; i < num_locks; i++) {
-		if (0 != pthread_mutex_init(&ssl_mutexes[i], &pthread_mutex_attr)) {
+		/* Check OOM */
+		if (ssl_mutexes == NULL) {
 			mg_snprintf(NULL,
 			            NULL, /* No truncation check for ebuf */
 			            ebuf,
 			            ebuf_len,
-			            "%s: error initializing mutex %i of %i",
+			            "%s: cannot allocate mutexes: %s",
 			            __func__,
-			            i,
-			            num_locks);
+			            ssl_error());
 			DEBUG_TRACE("%s", ebuf);
-			mg_free(ssl_mutexes);
 			return 0;
 		}
+
+		/* initialize mutex array */
+		for (i = 0; i < num_locks; i++) {
+			if (0 != pthread_mutex_init(&ssl_mutexes[i], &pthread_mutex_attr)) {
+				mg_snprintf(NULL,
+				            NULL, /* No truncation check for ebuf */
+				            ebuf,
+				            ebuf_len,
+				            "%s: error initializing mutex %i of %i",
+				            __func__,
+				            i,
+				            num_locks);
+				DEBUG_TRACE("%s", ebuf);
+				mg_free(ssl_mutexes);
+				return 0;
+			}
+		}
 	}
 
 	CRYPTO_set_locking_callback(&ssl_locking_callback);
@@ -18343,11 +18350,11 @@ mg_start(const struct mg_callbacks *callbacks,
 	    (struct socket *)mg_calloc_ctx(sizeof(ctx->client_socks[0]),
 	                                   ctx->cfg_worker_threads,
 	                                   ctx);
-	if (ctx->client_wait_events == NULL) {
+	if (ctx->client_socks == NULL) {
 		mg_cry_internal(fc(ctx),
 		                "%s",
 		                "Not enough memory for worker socket array");
-		mg_free(ctx->client_socks);
+		mg_free(ctx->client_wait_events);
 		mg_free(ctx->worker_threadids);
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
@@ -18363,6 +18370,7 @@ mg_start(const struct mg_callbacks *callbacks,
 				event_destroy(ctx->client_wait_events[i]);
 			}
 			mg_free(ctx->client_socks);
+			mg_free(ctx->client_wait_events);
 			mg_free(ctx->worker_threadids);
 			free_context(ctx);
 			pthread_setspecific(sTlsKey, NULL);

+ 2 - 1
src/handle_form.inl

@@ -799,7 +799,8 @@ mg_handle_form_request(struct mg_connection *conn,
 				}
 			}
 
-			if (!fbeg) {
+			if (!fbeg || !fend) {
+				fbeg = NULL;
 				fend = NULL;
 				fnlen = 0;
 			} else {

+ 13 - 1
src/timer.inl

@@ -199,10 +199,22 @@ timer_thread(void *thread_func_param)
 TIMER_API int
 timers_init(struct mg_context *ctx)
 {
+	/* Initialize timers data structure */
 	ctx->timers =
 	    (struct ttimers *)mg_calloc_ctx(sizeof(struct ttimers), 1, ctx);
-	(void)pthread_mutex_init(&ctx->timers->mutex, NULL);
 
+	if (!ctx->timers) {
+		return -1;
+	}
+
+	/* Initialize mutex */
+	if (0 != pthread_mutex_init(&ctx->timers->mutex, NULL)) {
+		mg_free((void *)(ctx->timers));
+		return -1;
+	}
+
+	/* For some systems timer_getcurrenttime does some initialization
+	 * during the first call. Call it once now, ignore the result. */
 	(void)timer_getcurrenttime();
 
 	/* Start timer thread */