Bladeren bron

Allocate memory for request buffer (required, since mg_connection is now pre-allocated)

bel 8 jaren geleden
bovenliggende
commit
9f19afabbb
3 gewijzigde bestanden met toevoegingen van 34 en 6 verwijderingen
  1. 32 4
      src/civetweb.c
  2. 1 1
      src/main.c
  3. 1 1
      test/timertest.c

+ 32 - 4
src/civetweb.c

@@ -13580,14 +13580,37 @@ worker_thread_run(struct worker_thread_args *thread_args)
 	tls.pthread_cond_helper_mutex = CreateEvent(NULL, FALSE, FALSE, NULL);
 #endif
 
+	/* Initialize thread local storage before calling any callback */
+	pthread_setspecific(sTlsKey, &tls);
+
 	if (ctx->callbacks.init_thread) {
 		/* call init_thread for a worker thread (type 1) */
 		ctx->callbacks.init_thread(ctx, 1);
 	}
-	conn = &ctx->worker_connections[thread_args->index];
-	pthread_setspecific(sTlsKey, &tls);
+
+	/* Connection structure has been pre-allocated */
+	if (((int)thread_args->index < 0)
+	    || ((unsigned)thread_args->index
+	        >= (unsigned)ctx->cfg_worker_threads)) {
+		mg_cry(fc(ctx),
+		       "Internal error: Invalid worker index %i",
+		       (int)thread_args->index);
+		return NULL;
+	}
+	conn = ctx->worker_connections + thread_args->index;
+
+	/* Request buffers are not pre-allocated. They are private to the
+	 * request and do not contain any state information that might be
+	 * of interest to anyone observing a server status.  */
+	conn->buf = (char *)mg_malloc(MAX_REQUEST_SIZE);
+	if (conn->buf == NULL) {
+		mg_cry(fc(ctx),
+		       "Out of memory: Cannot allocate buffer for worker %i",
+		       (int)thread_args->index);
+		return NULL;
+	}
 	conn->buf_size = MAX_REQUEST_SIZE;
-	conn->buf = (char *)(conn + 1);
+
 	conn->ctx = ctx;
 	conn->thread_index = thread_args->index;
 	conn->request_info.user_data = ctx->user_data;
@@ -13681,6 +13704,11 @@ worker_thread_run(struct worker_thread_args *thread_args)
 #endif
 	pthread_mutex_destroy(&conn->mutex);
 
+	/* Free the request buffer. */
+	conn->buf_size = 0;
+	mg_free(conn->buf);
+	conn->buf = NULL;
+
 	DEBUG_TRACE("%s", "exiting");
 	return NULL;
 }
@@ -14311,7 +14339,7 @@ mg_start(const struct mg_callbacks *callbacks,
 	    (struct mg_connection *)mg_calloc(ctx->cfg_worker_threads,
 	                                      sizeof(struct mg_connection));
 	if (ctx->worker_connections == NULL) {
-		mg_cry(fc(ctx), "Not enough memory for worker thread ID array");
+		mg_cry(fc(ctx), "Not enough memory for worker thread connection array");
 		free_context(ctx);
 		pthread_setspecific(sTlsKey, NULL);
 		return NULL;

+ 1 - 1
src/main.c

@@ -652,7 +652,7 @@ init_system_info(void)
 {
 	int len = mg_get_system_info(NULL, 0);
 	if (len > 0) {
-		g_system_info = (char*)malloc((unsigned)len + 1);
+		g_system_info = (char *)malloc((unsigned)len + 1);
 		(void)mg_get_system_info(g_system_info, len + 1);
 	} else {
 		g_system_info = sdup("Not available");

+ 1 - 1
test/timertest.c

@@ -54,7 +54,7 @@ action_dec(void *arg)
 
 	if (*p < -1) {
 		ck_abort_msg("Periodic timer called too often");
-        /* return 0 here would be unreachable code */
+		/* return 0 here would be unreachable code */
 	}
 
 	return (*p >= -3) ? action_dec_ret : 0;