浏览代码

Replace assert by own debug implementation

bel2125 7 年之前
父节点
当前提交
dc64c224b4
共有 3 个文件被更改,包括 89 次插入64 次删除
  1. 53 43
      src/civetweb.c
  2. 29 14
      src/main.c
  3. 7 7
      src/mod_lua.inl

+ 53 - 43
src/civetweb.c

@@ -215,7 +215,6 @@ mg_static_assert(sizeof(void *) >= sizeof(int), "data type size check");
 #include <mach/clock.h>
 #include <mach/mach.h>
 #include <mach/mach_time.h>
-#include <assert.h>
 
 /* clock_gettime is not implemented on OSX prior to 10.12 */
 static int
@@ -240,12 +239,11 @@ _civet_clock_gettime(int clk_id, struct timespec *t)
 
 		if (clock_start_time == 0) {
 			kern_return_t mach_status = mach_timebase_info(&timebase_ifo);
-#if defined(DEBUG)
-			assert(mach_status == KERN_SUCCESS);
-#else
+			DEBUG_ASSERT(mach_status == KERN_SUCCESS);
+
 			/* appease "unused variable" warning for release builds */
 			(void)mach_status;
-#endif
+
 			clock_start_time = now;
 		}
 
@@ -284,7 +282,6 @@ _civet_safe_clock_gettime(int clk_id, struct timespec *t)
 #include <time.h>
 #include <stdlib.h>
 #include <stdarg.h>
-#include <assert.h>
 #include <string.h>
 #include <ctype.h>
 #include <limits.h>
@@ -1528,6 +1525,22 @@ DEBUG_TRACE_FUNC(const char *func, unsigned line, const char *fmt, ...)
 #endif /* DEBUG_TRACE */
 
 
+#if !defined(DEBUG_ASSERT)
+#if defined(DEBUG)
+#define DEBUG_ASSERT(cond)                                                     \
+	do {                                                                       \
+		if (!(cond)) {                                                         \
+			DEBUG_TRACE("ASSERTION FAILED: %s", #cond);                        \
+			exit(2); /* Exit with error */                                     \
+		}                                                                      \
+	} while (0)
+#else
+#define DEBUG_ASSERT(cond)                                                     \
+	do {                                                                       \
+	} while (0)
+#endif /* DEBUG */
+#endif
+
 #define MD5_STATIC static
 #include "md5.inl"
 
@@ -3821,10 +3834,8 @@ header_has_option(const char *header, const char *option)
 	struct vec opt_vec;
 	struct vec eq_vec;
 
-	/*
-	assert(option != NULL);
-	assert(option[0] != '\0');
-	*/
+	DEBUG_ASSERT(option != NULL);
+	DEBUG_ASSERT(option[0] != '\0');
 
 	while ((header = next_option(header, &opt_vec, &eq_vec)) != NULL) {
 		if (mg_strncasecmp(option, opt_vec.ptr, opt_vec.len) == 0)
@@ -4503,7 +4514,7 @@ pthread_cond_signal(pthread_cond_t *cv)
 		cv->waiting_thread = cv->waiting_thread->next_waiting_thread;
 
 		ok = SetEvent(wkup);
-		assert(ok);
+		DEBUG_ASSERT(ok);
 	}
 	LeaveCriticalSection(&cv->threadIdSec);
 
@@ -4530,7 +4541,7 @@ static int
 pthread_cond_destroy(pthread_cond_t *cv)
 {
 	EnterCriticalSection(&cv->threadIdSec);
-	assert(cv->waiting_thread == NULL);
+	DEBUG_ASSERT(cv->waiting_thread == NULL);
 	LeaveCriticalSection(&cv->threadIdSec);
 	DeleteCriticalSection(&cv->threadIdSec);
 
@@ -6528,7 +6539,7 @@ mg_get_var2(const char *data,
 				if (s == NULL) {
 					s = e;
 				}
-				/* assert(s >= p); */
+				DEBUG_ASSERT(s >= p);
 				if (s < p) {
 					return -3;
 				}
@@ -9828,7 +9839,7 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
 	}
 
 	expect = mg_get_header(conn, "Expect");
-	/* assert(fp != NULL); */
+	DEBUG_ASSERT(fp != NULL);
 	if (!fp) {
 		mg_send_http_error(conn, 500, "%s", "Error: NULL File");
 		return 0;
@@ -9859,8 +9870,8 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
 		buffered_len = (int64_t)(conn->data_len) - (int64_t)conn->request_len
 		               - conn->consumed_content;
 
-		/* assert(buffered_len >= 0); */
-		/* assert(conn->consumed_content == 0); */
+		DEBUG_ASSERT(buffered_len >= 0);
+		DEBUG_ASSERT(conn->consumed_content == 0);
 
 		if ((buffered_len < 0) || (conn->consumed_content != 0)) {
 			mg_send_http_error(conn, 500, "%s", "Error: Size mismatch");
@@ -11380,7 +11391,7 @@ read_websocket(struct mg_connection *conn,
 	 * callback, and waiting repeatedly until an error occurs. */
 	while (!conn->phys_ctx->stop_flag && !conn->must_close) {
 		header_len = 0;
-		assert(conn->data_len >= conn->request_len);
+		DEBUG_ASSERT(conn->data_len >= conn->request_len);
 		if ((body_len = (size_t)(conn->data_len - conn->request_len)) >= 2) {
 			len = buf[1] & 127;
 			mask_len = (buf[1] & 128) ? 4 : 0;
@@ -11432,7 +11443,7 @@ read_websocket(struct mg_connection *conn,
 
 			/* Read frame payload from the first message in the queue into
 			 * data and advance the queue by moving the memory in place. */
-			assert(body_len >= header_len);
+			DEBUG_ASSERT(body_len >= header_len);
 			if (data_len + (uint64_t)header_len > (uint64_t)body_len) {
 				mop = buf[0]; /* current mask and opcode */
 				/* Overflow case */
@@ -12193,12 +12204,13 @@ mg_set_handler_type(struct mg_context *phys_ctx,
 	size_t urilen = strlen(uri);
 
 	if (handler_type == WEBSOCKET_HANDLER) {
-		/* assert(handler == NULL); */
-		/* assert(is_delete_request || connect_handler!=NULL ||
-		 *        ready_handler!=NULL || data_handler!=NULL ||
-		 *        close_handler!=NULL);
-		 */
-		/* assert(auth_handler == NULL); */
+		DEBUG_ASSERT(handler == NULL);
+		DEBUG_ASSERT(is_delete_request || connect_handler != NULL
+		             || ready_handler != NULL
+		             || data_handler != NULL
+		             || close_handler != NULL);
+
+		DEBUG_ASSERT(auth_handler == NULL);
 		if (handler != NULL) {
 			return;
 		}
@@ -12211,11 +12223,12 @@ mg_set_handler_type(struct mg_context *phys_ctx,
 			return;
 		}
 	} else if (handler_type == REQUEST_HANDLER) {
-		/* assert(connect_handler==NULL && ready_handler==NULL &&
-		 *        data_handler==NULL && close_handler==NULL); */
-		/* assert(is_delete_request || (handler!=NULL));
-		 */
-		/* assert(auth_handler == NULL); */
+		DEBUG_ASSERT(connect_handler == NULL && ready_handler == NULL
+		             && data_handler == NULL
+		             && close_handler == NULL);
+		DEBUG_ASSERT(is_delete_request || (handler != NULL));
+		DEBUG_ASSERT(auth_handler == NULL);
+
 		if ((connect_handler != NULL) || (ready_handler != NULL)
 		    || (data_handler != NULL) || (close_handler != NULL)) {
 			return;
@@ -12227,10 +12240,11 @@ mg_set_handler_type(struct mg_context *phys_ctx,
 			return;
 		}
 	} else { /* AUTH_HANDLER */
-		     /* assert(handler == NULL); */
-		     /* assert(connect_handler==NULL && ready_handler==NULL &&
-		      *        data_handler==NULL && close_handler==NULL); */
-		/* assert(auth_handler != NULL); */
+		DEBUG_ASSERT(handler == NULL);
+		DEBUG_ASSERT(connect_handler == NULL && ready_handler == NULL
+		             && data_handler == NULL
+		             && close_handler == NULL);
+		DEBUG_ASSERT(auth_handler != NULL);
 		if (handler != NULL) {
 			return;
 		}
@@ -14456,10 +14470,7 @@ ssl_servername_callback(SSL *ssl, int *ad, void *arg)
 
 	(void)ad;
 
-	if (conn->phys_ctx != ctx) {
-		/* Cannot happen - TODO(low): ASSERT ? */
-		DEBUG_TRACE("Internal error: %p != %p", conn->phys_ctx, ctx);
-	}
+	DEBUG_ASSERT(conn->phys_ctx == ctx);
 
 	/* Old clients (Win XP) will not support SNI. Then, there
 	 * is no server name available in the request - we can
@@ -15643,8 +15654,7 @@ get_message(struct mg_connection *conn, char *ebuf, size_t ebuf_len, int *err)
 
 	conn->request_len =
 	    read_message(NULL, conn, conn->buf, conn->buf_size, &conn->data_len);
-	/* assert(conn->request_len < 0 || conn->data_len >= conn->request_len);
-	 */
+	DEBUG_ASSERT(conn->request_len < 0 || conn->data_len >= conn->request_len);
 	if ((conn->request_len >= 0) && (conn->data_len < conn->request_len)) {
 		mg_snprintf(conn,
 		            NULL, /* No truncation check for ebuf */
@@ -16199,7 +16209,7 @@ process_new_connection(struct mg_connection *conn)
 			 * the server, or it was incomplete or a timeout. Send an
 			 * error message and close the connection. */
 			if (reqerr > 0) {
-				/*assert(ebuf[0] != '\0');*/
+				DEBUG_ASSERT(ebuf[0] != '\0');
 				mg_send_http_error(conn, reqerr, "%s", ebuf);
 			}
 		} else if (strcmp(ri->http_version, "1.0")
@@ -16312,7 +16322,7 @@ process_new_connection(struct mg_connection *conn)
 		                   < (int64_t)conn->data_len))
 		                  ? (int)(conn->request_len + conn->content_len)
 		                  : conn->data_len;
-		/*assert(discard_len >= 0);*/
+		DEBUG_ASSERT(discard_len >= 0);
 		if (discard_len < 0) {
 			DEBUG_TRACE("internal error: discard_len = %li",
 			            (long int)discard_len);
@@ -16324,8 +16334,8 @@ process_new_connection(struct mg_connection *conn)
 			memmove(conn->buf, conn->buf + discard_len, (size_t)conn->data_len);
 		}
 
-		/* assert(conn->data_len >= 0); */
-		/* assert(conn->data_len <= conn->buf_size); */
+		DEBUG_ASSERT(conn->data_len >= 0);
+		DEBUG_ASSERT(conn->data_len <= conn->buf_size);
 
 		if ((conn->data_len < 0) || (conn->data_len > conn->buf_size)) {
 			DEBUG_TRACE("internal error: data_len = %li, buf_size = %li",

+ 29 - 14
src/main.c

@@ -83,7 +83,6 @@
 #include <stddef.h>
 #include <stdarg.h>
 #include <ctype.h>
-#include <assert.h>
 
 #include "civetweb.h"
 
@@ -135,6 +134,22 @@ extern char *_getcwd(char *buf, size_t size);
 #endif /* defined(_WIN32) && !defined(__SYMBIAN32__) - WINDOWS / UNIX include  \
           block */
 
+#if !defined(DEBUG_ASSERT)
+#if defined(DEBUG)
+#define DEBUG_ASSERT(cond)                                                     \
+	do {                                                                       \
+		if (!(cond)) {                                                         \
+			fprintf(stderr, "ASSERTION FAILED: %s", #cond);                    \
+			exit(2); /* Exit with error */                                     \
+		}                                                                      \
+	} while (0)
+#else
+#define DEBUG_ASSERT(cond)                                                     \
+	do {                                                                       \
+	} while (0)
+#endif /* DEBUG */
+#endif
+
 #ifndef PATH_MAX
 #define PATH_MAX (1024)
 #endif
@@ -857,9 +872,9 @@ init_system_info(void)
 static void
 init_server_name(void)
 {
-	assert(sizeof(main_config_options) / sizeof(main_config_options[0])
-	       == NUM_MAIN_OPTIONS + 1);
-	assert((strlen(mg_version()) + 12) < sizeof(g_server_base_name));
+	DEBUG_ASSERT(sizeof(main_config_options) / sizeof(main_config_options[0])
+	             == NUM_MAIN_OPTIONS + 1);
+	DEBUG_ASSERT((strlen(mg_version()) + 12) < sizeof(g_server_base_name));
 	snprintf(g_server_base_name,
 	         sizeof(g_server_base_name),
 	         "CivetWeb V%s",
@@ -1740,9 +1755,9 @@ InputDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
 
 		if (hIn) {
 			/* This is an input dialog */
-			assert(inBuf != NULL);
-			assert((inBuf->buffer != NULL) && (inBuf->buflen != 0));
-			assert(strlen(inBuf->buffer) < inBuf->buflen);
+			DEBUG_ASSERT(inBuf != NULL);
+			DEBUG_ASSERT((inBuf->buffer != NULL) && (inBuf->buflen != 0));
+			DEBUG_ASSERT(strlen(inBuf->buffer) < inBuf->buflen);
 			SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon);
 			SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon);
 			SendMessage(hIn, EM_LIMITTEXT, inBuf->buflen - 1, 0);
@@ -1832,7 +1847,7 @@ get_password(const char *user,
 	                   8,
 	                   L"Tahoma"};
 
-	assert((user != NULL) && (realm != NULL) && (passwd != NULL));
+	DEBUG_ASSERT((user != NULL) && (realm != NULL) && (passwd != NULL));
 
 	/* Only allow one instance of this dialog to be open. */
 	if (s_dlg_proc_param.guard == 0) {
@@ -1952,7 +1967,7 @@ get_password(const char *user,
 	            12,
 	            "Cancel");
 
-	assert((intptr_t)p - (intptr_t)mem < (intptr_t)sizeof(mem));
+	DEBUG_ASSERT((intptr_t)p - (intptr_t)mem < (intptr_t)sizeof(mem));
 
 	dia->cy = y + (WORD)(HEIGHT * 1.5);
 
@@ -2197,7 +2212,7 @@ show_settings_dialog()
 		            "");
 		nelems++;
 
-		assert(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
+		DEBUG_ASSERT(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
 	}
 
 	y = (((nelems + 1) / 2 + 1) * HEIGHT + 5);
@@ -2263,7 +2278,7 @@ show_settings_dialog()
 	            12,
 	            g_server_base_name);
 
-	assert(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
+	DEBUG_ASSERT(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
 
 	dia->cy = ((nelems + 1) / 2 + 1) * HEIGHT + 30;
 
@@ -2414,7 +2429,7 @@ change_password_file()
 			            u);
 
 			nelems++;
-			assert(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
+			DEBUG_ASSERT(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
 		}
 		fclose(f);
 
@@ -2476,7 +2491,7 @@ change_password_file()
 		            12,
 		            g_server_base_name);
 
-		assert(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
+		DEBUG_ASSERT(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
 
 		dia->cy = y + 20;
 
@@ -2620,7 +2635,7 @@ show_system_info()
 	            12,
 	            "Close");
 
-	assert((intptr_t)p - (intptr_t)mem < (intptr_t)sizeof(mem));
+	DEBUG_ASSERT((intptr_t)p - (intptr_t)mem < (intptr_t)sizeof(mem));
 
 	dia->cy = y + (WORD)(HEIGHT * 1.5);
 

+ 7 - 7
src/mod_lua.inl

@@ -2072,7 +2072,7 @@ lua_websocket_new(const char *script, struct mg_connection *conn)
 	struct lua_websock_data *ws;
 	int err, ok = 0;
 
-	assert(conn->lua_websocket_state == NULL);
+	DEBUG_ASSERT(conn->lua_websocket_state == NULL);
 
 	/* lock list (mg_context global) */
 	mg_lock_context(conn->phys_ctx);
@@ -2164,8 +2164,8 @@ lua_websocket_data(struct mg_connection *conn,
 	struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
 	int err, ok = 0;
 
-	assert(ws != NULL);
-	assert(ws->state != NULL);
+	DEBUG_ASSERT(ws != NULL);
+	DEBUG_ASSERT(ws->state != NULL);
 
 	(void)pthread_mutex_lock(&(ws->ws_mutex));
 
@@ -2205,8 +2205,8 @@ lua_websocket_ready(struct mg_connection *conn, void *ws_arg)
 	struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
 	int err, ok = 0;
 
-	assert(ws != NULL);
-	assert(ws->state != NULL);
+	DEBUG_ASSERT(ws != NULL);
+	DEBUG_ASSERT(ws->state != NULL);
 
 	(void)pthread_mutex_lock(&(ws->ws_mutex));
 
@@ -2240,8 +2240,8 @@ lua_websocket_close(struct mg_connection *conn, void *ws_arg)
 	int err = 0;
 	unsigned i;
 
-	assert(ws != NULL);
-	assert(ws->state != NULL);
+	DEBUG_ASSERT(ws != NULL);
+	DEBUG_ASSERT(ws->state != NULL);
 
 	(void)pthread_mutex_lock(&(ws->ws_mutex));