[PATCH] Fix newlib/testsuite/newlib.search/hsearchtest.c compilation for 16-bit targets.

Jan Dubiec <[email protected]> Mon, 13 Jul 2026 03:10:28 +0200
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
When the test case is compiled for a 16-bit target, the compiler emits
the two warnings shown below, causing the test to fail. The code assumes
that pointers are 32 bits wide, which obviously is not true. This patch
fixes the issue.

h8300-elf-gcc /mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c  -mn      -I/mnt/Works/newlib/newlib/testsuite/include -lm  -o /mnt/Works/xcomp/build-newlib-h8300-linux/h8300-elf/newlib/testsuite/hsearchtest.x
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c: In function 'main':
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:80:26: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   80 |                 e.data = (void *)(long)i;
      |                          ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:84:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
   84 |                 TEST((long)ep->data == i);
      |                      ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:50:19: note: in definition of macro 'TEST'
   50 | #define TEST(e) ((e) ? (void)0 : testfail(__FILE__, __LINE__, #e))
      |                   ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:96:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
   96 |                 TEST((long)ep->data == i);
      |                      ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:50:19: note: in definition of macro 'TEST'
   50 | #define TEST(e) ((e) ? (void)0 : testfail(__FILE__, __LINE__, #e))
      |                   ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:105:14: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  105 |         TEST((long)ep->data == 0);
      |              ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:50:19: note: in definition of macro 'TEST'
   50 | #define TEST(e) ((e) ? (void)0 : testfail(__FILE__, __LINE__, #e))
      |                   ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:118:43: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  118 |         TEST(strcmp(ep->key, "a") == 0 && (long)ep->data == 0);
      |                                           ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:50:19: note: in definition of macro 'TEST'
   50 | #define TEST(e) ((e) ? (void)0 : testfail(__FILE__, __LINE__, #e))
      |                   ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:120:44: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  120 |         TEST(strcmp(ep2->key, "b") == 0 && (long)ep2->data == 1);
      |                                            ^
/mnt/Works/newlib/newlib/testsuite/newlib.search/hsearchtest.c:50:19: note: in definition of macro 'TEST'
   50 | #define TEST(e) ((e) ? (void)0 : testfail(__FILE__, __LINE__, #e))
      |                   ^

Signed-off-by: Jan Dubiec <[email protected]>
---
 newlib/testsuite/newlib.search/hsearchtest.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/newlib/testsuite/newlib.search/hsearchtest.c b/newlib/testsuite/newlib.search/hsearchtest.c
index 515440382..418b0c5d6 100644
--- a/newlib/testsuite/newlib.search/hsearchtest.c
+++ b/newlib/testsuite/newlib.search/hsearchtest.c
@@ -46,6 +46,14 @@ __COPYRIGHT(
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdint.h>
+
+#ifdef __INTPTR_TYPE__
+	#define INTPTRTYPE intptr_t
+#else
+	/* Just in case there is no intptr_t on a target... */
+	#define INTPTRTYPE long
+#endif
 
 #define	TEST(e)	((e) ? (void)0 : testfail(__FILE__, __LINE__, #e))
 
@@ -77,11 +85,11 @@ main(int argc, char *argv[])
 		ch[0] = 'a' + i;
 		e.key = strdup(ch);	/* ptr to provided key is kept! */
 		TEST(e.key != NULL);
-		e.data = (void *)(long)i;
+		e.data = (void *)(INTPTRTYPE)i;
 		ep = hsearch(e, ENTER);
 		TEST(ep != NULL);
 		TEST(strcmp(ep->key, ch) == 0);
-		TEST((long)ep->data == i);
+		TEST((INTPTRTYPE)ep->data == i);
 	}
 
 	/* e.key should be constant from here on down. */
@@ -93,16 +101,16 @@ main(int argc, char *argv[])
 		ep = hsearch(e, FIND);
 		TEST(ep != NULL);
 		TEST(strcmp(ep->key, ch) == 0);
-		TEST((long)ep->data == i);
+		TEST((INTPTRTYPE)ep->data == i);
 	}
 
 	/* Check duplicate entry.  Should _not_ overwrite existing data.  */
 	ch[0] = 'a';
-	e.data = (void *)(long)12345;
+	e.data = (void *)(INTPTRTYPE)12345;
 	ep = hsearch(e, FIND);
 	TEST(ep != NULL);
 	TEST(strcmp(ep->key, ch) == 0);
-	TEST((long)ep->data == 0);
+	TEST((INTPTRTYPE)ep->data == 0);
 
 	/* Check for something that's not there. */
 	ch[0] = 'A';
@@ -115,9 +123,9 @@ main(int argc, char *argv[])
 	ch[0] = 'b';
 	ep2 = hsearch(e, FIND);
 	TEST(ep != NULL);
-	TEST(strcmp(ep->key, "a") == 0 && (long)ep->data == 0);
+	TEST(strcmp(ep->key, "a") == 0 && (INTPTRTYPE)ep->data == 0);
 	TEST(ep2 != NULL);
-	TEST(strcmp(ep2->key, "b") == 0 && (long)ep2->data == 1);
+	TEST(strcmp(ep2->key, "b") == 0 && (INTPTRTYPE)ep2->data == 1);
 
 	hdestroy();
 
-- 
2.54.0