Browse Source

Avoid some warnings from PVS Studio (#597)

bel2125 7 years ago
parent
commit
bc6eaaeef5
5 changed files with 96 additions and 56 deletions
  1. 1 1
      src/civetweb.c
  2. 8 4
      src/main.c
  3. 51 42
      unittest/private.c
  4. 24 2
      unittest/public_func.c
  5. 12 7
      unittest/public_server.c

+ 1 - 1
src/civetweb.c

@@ -2941,7 +2941,7 @@ mg_set_thread_name(const char *name)
 #elif defined(__MINGW32__)
 /* No option known to set thread name for MinGW */
 #endif
-#elif defined(_GNU_SOURCE) && defined(__GLIBC__)                                                       \
+#elif defined(_GNU_SOURCE) && defined(__GLIBC__)                               \
     && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))
 	/* pthread_setname_np first appeared in glibc in version 2.12*/
 	(void)pthread_setname_np(pthread_self(), threadName);

+ 8 - 4
src/main.c

@@ -1874,7 +1874,8 @@ get_password(const char *user,
 
 	/* Create the dialog */
 	(void)memset(mem, 0, sizeof(mem));
-	(void)memcpy(mem, &dialog_header, sizeof(dialog_header));
+	p = mem;
+	(void)memcpy(p, &dialog_header, sizeof(dialog_header));
 	p = mem + sizeof(dialog_header);
 
 	y = HEIGHT;
@@ -2150,7 +2151,8 @@ show_settings_dialog()
 	}
 
 	(void)memset(mem, 0, sizeof(mem));
-	(void)memcpy(mem, &dialog_header, sizeof(dialog_header));
+	p = mem;
+	(void)memcpy(p, &dialog_header, sizeof(dialog_header));
 	p = mem + sizeof(dialog_header);
 
 	options = mg_get_valid_options();
@@ -2368,7 +2370,8 @@ change_password_file()
 	do {
 		s_dlg_proc_param.hWnd = NULL;
 		(void)memset(mem, 0, sizeof(mem));
-		(void)memcpy(mem, &dialog_header, sizeof(dialog_header));
+		p = mem;
+		(void)memcpy(p, &dialog_header, sizeof(dialog_header));
 		p = mem + sizeof(dialog_header);
 
 		f = fopen(path, "r+");
@@ -2586,7 +2589,8 @@ show_system_info()
 
 	/* Create the dialog */
 	(void)memset(mem, 0, sizeof(mem));
-	(void)memcpy(mem, &dialog_header, sizeof(dialog_header));
+	p = mem;
+	(void)memcpy(p, &dialog_header, sizeof(dialog_header));
 	p = mem + sizeof(dialog_header);
 
 	y = HEIGHT;

+ 51 - 42
unittest/private.c

@@ -562,52 +562,61 @@ END_TEST
 START_TEST(test_parse_port_string)
 {
 	/* Adapted from unit_test.c */
-	/* Copyright (c) 2013-2015 the Civetweb developers */
+	/* Copyright (c) 2013-2018 the Civetweb developers */
 	/* Copyright (c) 2004-2013 Sergey Lyubka */
-	static const char *valid[] =
-	{ "0",
-	  "1",
-	  "1s",
-	  "1r",
-	  "1.2.3.4:1",
-	  "1.2.3.4:1s",
-	  "1.2.3.4:1r",
+	struct t_test_parse_port_string {
+		const char *port_string;
+		int valid;
+		int ip_family;
+	};
+
+	static struct t_test_parse_port_string testdata[] =
+	{ {"0", 1, 4},
+	  {"1", 1, 4},
+	  {"1s", 1, 4},
+	  {"1r", 1, 4},
+	  {"1.2.3.4:1", 1, 4},
+	  {"1.2.3.4:1s", 1, 4},
+	  {"1.2.3.4:1r", 1, 4},
 #if defined(USE_IPV6)
-	  "[::1]:123",
-	  "[::]:80",
-	  "[3ffe:2a00:100:7031::1]:900",
-	  "+80",
+	  {"[::1]:123", 1, 6},
+	  {"[::]:80", 1, 6},
+	  {"[3ffe:2a00:100:7031::1]:900", 1, 6},
+	  {"+80", 1, 4 + 6},
+#else
+	  {"[::1]:123", 0, 0},
+	  {"[::]:80", 0, 0},
+	  {"[3ffe:2a00:100:7031::1]:900", 0, 0},
+	  {"+80", 0, 0},
 #endif
-	  NULL };
-	static const char *invalid[] = {
-	    "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p", NULL};
+	  {"99999", 0, 0},
+	  {"1k", 0, 0},
+	  {"1.2.3", 0, 0},
+	  {"1.2.3.4:", 0, 0},
+	  {"1.2.3.4:2p", 0, 0},
+	  {NULL, 0, 0} };
+
 	struct socket so;
 	struct vec vec;
 	int ip_family;
-	int i;
+	int i, ret;
 
 	mark_point();
 
-	for (i = 0; valid[i] != NULL; i++) {
-		vec.ptr = valid[i];
+	for (i = 0; testdata[i].port_string != NULL; i++) {
+		vec.ptr = testdata[i].port_string;
 		vec.len = strlen(vec.ptr);
+
 		ip_family = 123;
-		ck_assert_int_ne(parse_port_string(&vec, &so, &ip_family), 0);
-		if (i < 7) {
-			ck_assert_int_eq(ip_family, 4);
-		} else if (i < 10) {
-			ck_assert_int_eq(ip_family, 6);
+		ret = parse_port_string(&vec, &so, &ip_family);
+
+		if (testdata[i].valid) {
+			ck_assert_int_ne(ret, 0);
 		} else {
-			ck_assert_int_eq(ip_family, 4 + 6);
+			ck_assert_int_eq(ret, 0);
 		}
-	}
 
-	for (i = 0; invalid[i] != NULL; i++) {
-		vec.ptr = invalid[i];
-		vec.len = strlen(vec.ptr);
-		ip_family = 123;
-		ck_assert_int_eq(parse_port_string(&vec, &so, &ip_family), 0);
-		ck_assert_int_eq(ip_family, 0);
+		ck_assert_int_eq(ip_family, testdata[i].ip_family);
 	}
 }
 END_TEST
@@ -794,12 +803,12 @@ START_TEST(test_parse_date_string)
 
 	sprintf(date,
 	        "%02u %s %04u %02u:%02u:%02u",
-	        tm->tm_mday,
+	        (unsigned int)tm->tm_mday,
 	        month_names[tm->tm_mon],
-	        tm->tm_year + 1900,
-	        tm->tm_hour,
-	        tm->tm_min,
-	        tm->tm_sec);
+	        (unsigned int)(tm->tm_year + 1900),
+	        (unsigned int)tm->tm_hour,
+	        (unsigned int)tm->tm_min,
+	        (unsigned int)tm->tm_sec);
 	ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
 
 	gmt_time_string(date, 1, NULL);
@@ -819,12 +828,12 @@ START_TEST(test_parse_date_string)
 		tm = gmtime(&now);
 		sprintf(date,
 		        "%02u-%s-%04u %02u:%02u:%02u",
-		        tm->tm_mday,
+		        (unsigned int)tm->tm_mday,
 		        month_names[tm->tm_mon],
-		        tm->tm_year + 1900,
-		        tm->tm_hour,
-		        tm->tm_min,
-		        tm->tm_sec);
+		        (unsigned int)(tm->tm_year + 1900),
+		        (unsigned int)tm->tm_hour,
+		        (unsigned int)tm->tm_min,
+		        (unsigned int)tm->tm_sec);
 		ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
 	}
 #endif

+ 24 - 2
unittest/public_func.c

@@ -149,15 +149,37 @@ END_TEST
 
 START_TEST(test_mg_get_valid_options)
 {
-	int i;
+	int i, j, len;
+	char c;
 	const struct mg_option *default_options = mg_get_valid_options();
 
 	ck_assert(default_options != NULL);
 
 	for (i = 0; default_options[i].name != NULL; i++) {
+
+		/* every option has a name */
 		ck_assert(default_options[i].name != NULL);
-		ck_assert(strlen(default_options[i].name) > 0);
+
+		/* every option has a valie type >0 and <= the highest currently known
+		 * option type (currently 9 = MG_CONFIG_TYPE_YES_NO_OPTIONAL) */
 		ck_assert(((int)default_options[i].type) > 0);
+		ck_assert(((int)default_options[i].type) < 10);
+
+		/* options start with a lowercase letter (a-z) */
+		c = default_options[i].name[0];
+		ck_assert((c >= 'a') && (c <= 'z'));
+
+		/* check some reasonable length (this is not a permanent spec
+		 * for min/max option name lengths) */
+		len = (int)strlen(default_options[i].name);
+		ck_assert_int_ge(len, 8);
+		ck_assert_int_lt(len, 40);
+
+		/* check valid characters (lower case or underscore) */
+		for (j = 0; j < len; j++) {
+			c = default_options[i].name[j];
+			ck_assert(((c >= 'a') && (c <= 'z')) || (c == '_'));
+		}
 	}
 
 	ck_assert(i > 0);

+ 12 - 7
unittest/public_server.c

@@ -1134,7 +1134,7 @@ websocket_client_close_handler(const struct mg_connection *conn,
 
 START_TEST(test_request_handlers)
 {
-	char ebuf[100];
+	char ebuf[1024];
 	struct mg_context *ctx;
 	struct mg_connection *client_conn;
 	const struct mg_response_info *client_ri;
@@ -1191,7 +1191,7 @@ START_TEST(test_request_handlers)
 	struct mg_connection *ws_client4_conn = NULL;
 #endif
 
-	char cmd_buf[256];
+	char cmd_buf[1024];
 	char *cgi_env_opt;
 
 	mark_point();
@@ -1212,7 +1212,7 @@ START_TEST(test_request_handlers)
 #endif
 	OPTIONS[opt_idx++] = "cgi_environment";
 	cgi_env_opt = (char *)calloc(1, 4096 /* CGI_ENVIRONMENT_SIZE */);
-	ck_assert_ptr_ne(cgi_env_opt, NULL);
+	ck_assert(cgi_env_opt != NULL);
 	cgi_env_opt[0] = 'x';
 	cgi_env_opt[1] = '=';
 	memset(cgi_env_opt + 2, 'y', 4090); /* Add large env field, so the server
@@ -2470,7 +2470,7 @@ START_TEST(test_handle_form)
 	const char *OPTIONS[8];
 	const char *opt;
 	int opt_idx = 0;
-	char ebuf[100];
+	char ebuf[1024];
 	const char *multipart_body;
 	const char *boundary;
 	size_t body_len, body_sent, chunk_len;
@@ -2954,10 +2954,11 @@ START_TEST(test_handle_form)
 	 * environments (depending on the network stack) */
 	body_sent = 0;
 	do {
+		size_t bound_len = strlen(boundary);
 		send_chunk_string(client_conn, "ignore\r\n");
 		body_sent += 8;
 		/* send some strings that are almost boundaries */
-		for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) {
+		for (chunk_len = 1; chunk_len < bound_len; chunk_len++) {
 			/* chunks from 1 byte to strlen(boundary)-1 */
 			send_chunk_stringl(client_conn, boundary, (unsigned int)chunk_len);
 			body_sent += chunk_len;
@@ -3046,7 +3047,7 @@ START_TEST(test_http_auth)
 	const char *str;
 	size_t len;
 	int i;
-	char HA1[256], HA2[256], HA[256];
+	char HA1[256], HA2[256];
 	char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
 	char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
 	const char *nc = "00000001";
@@ -3167,7 +3168,6 @@ START_TEST(test_http_auth)
 	       (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
 	memset(HA1, 0, sizeof(HA1));
 	memset(HA2, 0, sizeof(HA2));
-	memset(HA, 0, sizeof(HA));
 	memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
 	memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
 	memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
@@ -3772,9 +3772,14 @@ START_TEST(test_error_log_file)
 
 	client_ri = mg_get_response_info(client);
 
+	/* Check status - should be 404 Not Found */
 	ck_assert(client_ri != NULL);
 	ck_assert_int_eq(client_ri->status_code, 404);
 
+	/* Get body data (could exist, but does not have to) */
+	len = mg_read(client, client_data_buf, sizeof(client_data_buf));
+	ck_assert_int_ge(len, 0);
+
 	/* Close the client connection */
 	mg_close_connection(client);