[gnutls-dev] Re: Compiler warnings on 64bit archs

Andreas Metzler ametzler at downhill.at.eu.org
Sat Jun 24 14:09:11 CEST 2006


On 2006-06-16 Andreas Metzler <ametzler at downhill.at.eu.org> wrote:
> On 2006-06-16 Simon Josefsson <jas at extundo.com> wrote:
>> Andreas Metzler <ametzler at downhill.at.eu.org> writes:
[...]
>>> ------------------------
>>> gnutls_buffers.c:268: warning: cast from pointer to integer of different size
>>> gnutls_buffers.c:704: warning: cast from pointer to integer of different size
>> ...

>> Storing integers within pointers is safe, isn't it?  Compare:
>> http://developer.gnome.org/doc/API/2.0/glib/glib-Type-Conversion-Macros.html

> I did not know this page. ;-)
> <quote>
> The problem is that on some systems you need to do this:
> p = (void*) (long) 42;
> i = (int) (long) p;
> </quote>
[...]

Attached a small prrof of concept patch showing how this could be
done, eliminating the warnings for gnutls_buffers.c. I have put the
macros in gnutls.h, it might be better to not make them part of the
GnuTLS API, though.
cu andreas
-- 
The 'Galactic Cleaning' policy undertaken by Emperor Zhark is a personal
vision of the emperor's, and its inclusion in this work does not constitute
tacit approval by the author or the publisher for any such projects,
howsoever undertaken.                                (c) Jasper Ffforde
-------------- next part --------------
diff -Nur gnutls13-1.4.0-2/configure.in gnutls13-1.4.0/configure.in
--- gnutls13-1.4.0-2/configure.in	2006-05-11 13:20:09.000000000 +0200
+++ gnutls13-1.4.0/configure.in	2006-06-24 14:08:42.914407736 +0200
@@ -182,6 +182,21 @@
 AC_CHECK_SIZEOF(unsigned long, 4)
 AC_CHECK_SIZEOF(unsigned int, 4)
 
+# for strong ints in pointers without warnings
+AC_CHECK_SIZEOF(long)
+AC_CHECK_SIZEOF(int)
+AC_CHECK_SIZEOF(void *)
+
+AC_SUBST(CAST_INT_AND_POINTER)
+case $ac_cv_sizeof_void_p in
+    $ac_cv_sizeof_int)
+        CAST_INT_AND_POINTER=''
+    ;;
+    $ac_cv_sizeof_long)
+        CAST_INT_AND_POINTER='(long)'
+    ;;
+esac
+
 # For some systems we know that we have ld_version scripts.
 # Use it then as default.
 have_ld_version_script=no
diff -Nur gnutls13-1.4.0-2/includes/gnutls/gnutls.h.in gnutls13-1.4.0/includes/gnutls/gnutls.h.in
--- gnutls13-1.4.0-2/includes/gnutls/gnutls.h.in	2006-05-05 11:07:42.000000000 +0200
+++ gnutls13-1.4.0/includes/gnutls/gnutls.h.in	2006-06-24 14:08:10.124392576 +0200
@@ -1161,6 +1161,10 @@
 
 #define GNUTLS_E_UNIMPLEMENTED_FEATURE -1250
 
+/* helper macros */
+#define GNUTLS_POINTER_TO_INT(p)  ((int)  @CAST_INT_AND_POINTER@ (p))
+#define GNUTLS_INT_TO_POINTER(i)  ((void*) @CAST_INT_AND_POINTER@ (i))
+
 #ifdef __cplusplus
 }
 #endif
diff -Nur gnutls13-1.4.0-2/lib/gnutls_buffers.c gnutls13-1.4.0/lib/gnutls_buffers.c
--- gnutls13-1.4.0-2/lib/gnutls_buffers.c	2006-03-08 11:44:59.000000000 +0100
+++ gnutls13-1.4.0/lib/gnutls_buffers.c	2006-06-24 14:08:10.131391512 +0200
@@ -265,7 +265,7 @@
     {
 
       if (session->internals._gnutls_pull_func == NULL)
-	i = recv ((int) fd, &ptr[sizeOfPtr - left], left, flags);
+	i = recv (GNUTLS_POINTER_TO_INT(fd), &ptr[sizeOfPtr - left], left, flags);
       else
 	i = session->internals._gnutls_pull_func (fd,
 						  &ptr[sizeOfPtr -
@@ -701,7 +701,7 @@
     {
 
       if (session->internals._gnutls_push_func == NULL)
-	i = send ((int) fd, &ptr[n - left], left, 0);
+	i = send (GNUTLS_POINTER_TO_INT(fd), &ptr[n - left], left, 0);
       else
 	i = session->internals._gnutls_push_func (fd, &ptr[n - left], left);
 


More information about the Gnutls-devel mailing list