661d72
From cc6bf2f8a3a1986ae0ef74ad8baadc08145a6702 Mon Sep 17 00:00:00 2001
661d72
From: Mark Andrews <marka@isc.org>
661d72
Date: Wed, 27 Sep 2017 16:02:02 +1000
661d72
Subject: [PATCH] 4740.   [cleanup]       Avoid triggering format-truncated
661d72
 warnings. [RT #46107]
661d72
661d72
(cherry picked from commit 73074e954a1f751c561728d08176b6849e843a2e)
661d72
661d72
use %u and cast to unsigned int
661d72
661d72
(cherry picked from commit 2495de04a50dc44968490168226199b9c80a2ab4)
661d72
661d72
4775.   [bug]           Address Coverity warnings in ht_test.c and mem_test.c
661d72
                        [RT #46281]
661d72
661d72
(cherry picked from commit 583e355951c73f5cdae3a161be9753701abaf553)
661d72
661d72
4776.   [bug]           Improve portability of ht_test. [RT #46333]
661d72
661d72
(cherry picked from commit 66258ca349b7398062e684337b19c5ce76701037)
661d72
661d72
(cherry picked from commit 5ff48dca189244295a0baded4970605156ca8529)
661d72
---
661d72
 lib/isc/tests/ht_test.c | 99 +++++++++++++++++++++++++++++++++----------------
661d72
 1 file changed, 67 insertions(+), 32 deletions(-)
661d72
661d72
diff --git a/lib/isc/tests/ht_test.c b/lib/isc/tests/ht_test.c
661d72
index 78dbc2d6a1..4a56ab86b5 100644
661d72
--- a/lib/isc/tests/ht_test.c
661d72
+++ b/lib/isc/tests/ht_test.c
661d72
@@ -20,8 +20,11 @@
661d72
 #include <isc/ht.h>
661d72
 #include <isc/mem.h>
661d72
 #include <isc/print.h>
661d72
+#include <isc/string.h>
661d72
 #include <isc/util.h>
661d72
 
661d72
+#include <inttypes.h>
661d72
+
661d72
 static void *
661d72
 default_memalloc(void *arg, size_t size) {
661d72
 	UNUSED(arg);
661d72
@@ -36,25 +39,28 @@ default_memfree(void *arg, void *ptr) {
661d72
 	free(ptr);
661d72
 }
661d72
 
661d72
-
661d72
-static void test_ht_full(int bits, int count) {
661d72
+static void test_ht_full(int bits, uintptr_t count) {
661d72
 	isc_ht_t *ht = NULL;
661d72
 	isc_result_t result;
661d72
 	isc_mem_t *mctx = NULL;
661d72
-	isc_int64_t i;
661d72
+	uintptr_t i;
661d72
 
661d72
 	result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
661d72
 				  NULL, &mctx, 0);
661d72
 	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
 
661d72
-	isc_ht_init(&ht, mctx, bits);
661d72
+	result = isc_ht_init(&ht, mctx, bits);
661d72
+	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
+	ATF_REQUIRE(ht != NULL);
661d72
+
661d72
 	for (i = 1; i < count; i++) {
661d72
 		/*
661d72
-		 * Note that the string we're snprintfing is always > 16 bytes
661d72
-		 * so we are always filling the key.
661d72
+		 * Note: snprintf() is followed with strlcat()
661d72
+		 * to ensure we are always filling the 16 byte key.
661d72
 		 */
661d72
 		unsigned char key[16];
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", i);
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_add(ht, key, 16, (void *) i);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
 	}
661d72
@@ -62,22 +68,29 @@ static void test_ht_full(int bits, int count) {
661d72
 	for (i = 1; i < count; i++) {
661d72
 		unsigned char key[16];
661d72
 		void *f = NULL;
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", i);
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_find(ht, key, 16, &f);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
-		ATF_REQUIRE_EQ(i, (isc_int64_t) f);
661d72
+		ATF_REQUIRE_EQ(i, (uintptr_t) f);
661d72
 	}
661d72
 
661d72
 	for (i = 1; i < count; i++) {
661d72
 		unsigned char key[16];
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", i);
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_add(ht, key, 16, (void *) i);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_EXISTS);
661d72
 	}
661d72
 
661d72
 	for (i = 1; i < count; i++) {
661d72
 		char key[64];
661d72
-		snprintf((char *)key, 64, "%lld key of a str hashtable!!", i);
661d72
+		/*
661d72
+		 * Note: the key size is now strlen(key) which is bigger
661d72
+		 * then the keys added above.
661d72
+		 */
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_add(ht, (const unsigned char *) key,
661d72
 				    strlen(key), (void *) i);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
@@ -86,7 +99,11 @@ static void test_ht_full(int bits, int count) {
661d72
 	for (i = 1; i < count; i++) {
661d72
 		unsigned char key[16];
661d72
 		void *f = NULL;
661d72
-		snprintf((char *)key, 16, "%lld KEY of a raw hashtable!!", i);
661d72
+		/*
661d72
+		 * Note: case of KEY is now in capitals,
661d72
+		 */
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_find(ht, key, 16, &f);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
661d72
 		ATF_REQUIRE_EQ(f, NULL);
661d72
@@ -95,7 +112,8 @@ static void test_ht_full(int bits, int count) {
661d72
 	for (i = 1; i < count; i++) {
661d72
 		char key[64];
661d72
 		void *f = NULL;
661d72
-		snprintf((char *)key, 64, "%lld key of a str hashtable!!", i);
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_find(ht, (const unsigned char *) key,
661d72
 				     strlen(key), &f);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
@@ -105,7 +123,8 @@ static void test_ht_full(int bits, int count) {
661d72
 	for (i = 1; i < count; i++) {
661d72
 		unsigned char key[16];
661d72
 		void *f = NULL;
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", i);
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_delete(ht, key, 16);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
 		result = isc_ht_find(ht, key, 16, &f);
661d72
@@ -115,7 +134,11 @@ static void test_ht_full(int bits, int count) {
661d72
 
661d72
 	for (i = 1; i < count; i++) {
661d72
 		unsigned char key[16];
661d72
-		snprintf((char *)key, 16, "%lld KEY of a raw hashtable!!", i);
661d72
+		/*
661d72
+		 * Note: upper case KEY.
661d72
+		 */
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_add(ht, key, 16, (void *) i);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
 	}
661d72
@@ -123,7 +146,8 @@ static void test_ht_full(int bits, int count) {
661d72
 	for (i = 1; i < count; i++) {
661d72
 		char key[64];
661d72
 		void *f = NULL;
661d72
-		snprintf((char *)key, 64, "%lld key of a str hashtable!!", i);
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_delete(ht, (const unsigned char *) key,
661d72
 				       strlen(key));
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
@@ -137,16 +161,21 @@ static void test_ht_full(int bits, int count) {
661d72
 	for (i = 1; i < count; i++) {
661d72
 		unsigned char key[16];
661d72
 		void *f = NULL;
661d72
-		snprintf((char *)key, 16, "%lld KEY of a raw hashtable!!", i);
661d72
+		/*
661d72
+		 * Note: case of KEY is now in capitals,
661d72
+		 */
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_find(ht, key, 16, &f);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
-		ATF_REQUIRE_EQ(i, (isc_int64_t) f);
661d72
+		ATF_REQUIRE_EQ(i, (uintptr_t) f);
661d72
 	}
661d72
 
661d72
 	for (i = 1; i < count; i++) {
661d72
 		unsigned char key[16];
661d72
 		void *f = NULL;
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", i);
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_find(ht, key, 16, &f);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
661d72
 		ATF_REQUIRE_EQ(f, NULL);
661d72
@@ -161,9 +190,9 @@ static void test_ht_iterator() {
661d72
 	isc_result_t result;
661d72
 	isc_mem_t *mctx = NULL;
661d72
 	isc_ht_iter_t * iter = NULL;
661d72
-	isc_int64_t i;
661d72
-	isc_int64_t v;
661d72
-	isc_uint32_t count = 10000;
661d72
+	uintptr_t i;
661d72
+	void *v;
661d72
+	uintptr_t count = 10000;
661d72
 	isc_uint32_t walked;
661d72
 	unsigned char key[16];
661d72
 	unsigned char *tkey;
661d72
@@ -181,7 +210,8 @@ static void test_ht_iterator() {
661d72
 		 * Note that the string we're snprintfing is always > 16 bytes
661d72
 		 * so we are always filling the key.
661d72
 		 */
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", i);
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
661d72
 		result = isc_ht_add(ht, key, 16, (void *) i);
661d72
 		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
661d72
 	}
661d72
@@ -194,10 +224,12 @@ static void test_ht_iterator() {
661d72
 	     result == ISC_R_SUCCESS;
661d72
 	     result = isc_ht_iter_next(iter))
661d72
 	{
661d72
-		isc_ht_iter_current(iter, (void**) &v);
661d72
+		isc_ht_iter_current(iter, &v);
661d72
 		isc_ht_iter_currentkey(iter, &tkey, &tksize);
661d72
 		ATF_REQUIRE_EQ(tksize, 16);
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", v);
661d72
+		i = (uintptr_t)v;
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
661d72
 		ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
661d72
 		walked++;
661d72
 	}
661d72
@@ -208,12 +240,14 @@ static void test_ht_iterator() {
661d72
 	walked = 0;
661d72
 	result = isc_ht_iter_first(iter);
661d72
 	while (result == ISC_R_SUCCESS) {
661d72
-		isc_ht_iter_current(iter, (void**) &v);
661d72
+		isc_ht_iter_current(iter, &v);
661d72
 		isc_ht_iter_currentkey(iter, &tkey, &tksize);
661d72
 		ATF_REQUIRE_EQ(tksize, 16);
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", v);
661d72
+		i = (uintptr_t)v;
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
661d72
 		ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
661d72
-		if (v % 2 == 0) {
661d72
+		if ((uintptr_t)v % 2 == 0) {
661d72
 			result = isc_ht_iter_delcurrent_next(iter);
661d72
 		} else {
661d72
 			result = isc_ht_iter_next(iter);
661d72
@@ -227,12 +261,14 @@ static void test_ht_iterator() {
661d72
 	walked = 0;
661d72
 	result = isc_ht_iter_first(iter);
661d72
 	while (result == ISC_R_SUCCESS) {
661d72
-		isc_ht_iter_current(iter, (void**) &v);
661d72
+		isc_ht_iter_current(iter, &v);
661d72
 		isc_ht_iter_currentkey(iter, &tkey, &tksize);
661d72
 		ATF_REQUIRE_EQ(tksize, 16);
661d72
-		snprintf((char *)key, 16, "%lld key of a raw hashtable!!", v);
661d72
+		i = (uintptr_t)v;
661d72
+		snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
661d72
+		strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
661d72
 		ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
661d72
-		if (v % 2 == 1) {
661d72
+		if ((uintptr_t)v % 2 == 1) {
661d72
 			result = isc_ht_iter_delcurrent_next(iter);
661d72
 		} else {
661d72
 			result = isc_ht_iter_next(iter);
661d72
@@ -322,4 +358,3 @@ ATF_TP_ADD_TCS(tp) {
661d72
 	ATF_TP_ADD_TC(tp, isc_ht_iterator);
661d72
 	return (atf_no_error());
661d72
 }
661d72
-
661d72
-- 
661d72
2.14.3
661d72