This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

More intl-directory warnings patrol


This deals with the "comparison of signed and unsigned" warning upon
attempting to check that a file size fits in a size_t, the same way
it's done in cpplib.  intl/ files don't include system.h, so I have to
clone some of its macros.

bootstrapped i686-linux with --with-included-gettext, ok to apply?

zw

	* loadmsgcat.c (INTTYPE_SIGNED, INTTYPE_MINIMUM,
	INTTYPE_MAXIMUM): Clone from system.h.
	(_nl_load_domain): Use them when testing for overflow of size_t.
	Cast result of sizeof to off_t to compare to st_size value.
	Move side effects out of conditional for comprehensibility.

===================================================================
Index: intl/loadmsgcat.c
--- intl/loadmsgcat.c	2001/10/21 16:29:13	1.6
+++ intl/loadmsgcat.c	2001/10/23 04:19:18
@@ -78,6 +78,15 @@ char *alloca ();
 # include "../locale/localeinfo.h"
 #endif
 
+/* GCC LOCAL: These macros are used below.  */
+/* The extra casts work around common compiler bugs.  */
+#define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
+/* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
+   It is necessary at least when t == time_t.  */
+#define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
+                             ? ~ (t) 0 << (sizeof(t) * CHAR_BIT - 1) : (t) 0))
+#define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
+
 /* @@ end of prolog @@ */
 
 #ifdef _LIBC
@@ -373,19 +382,23 @@ _nl_load_domain (domain_file, domainbind
     return;
 
   /* We must know about the size of the file.  */
+  /* GCC_LOCAL: Use INTTYPE_MAXIMUM for overflow check, cast sizeof to
+     off_t, move set of size below if.  */
   if (
 #ifdef _LIBC
       __builtin_expect (fstat64 (fd, &st) != 0, 0)
 #else
       __builtin_expect (fstat (fd, &st) != 0, 0)
 #endif
-      || __builtin_expect ((size = (size_t) st.st_size) != st.st_size, 0)
-      || __builtin_expect (size < sizeof (struct mo_file_header), 0))
+      || __builtin_expect (st.st_size > INTTYPE_MAXIMUM (ssize_t), 0)
+      || __builtin_expect (st.st_size < (off_t) sizeof (struct mo_file_header),
+			   0))
     {
       /* Something went wrong.  */
       close (fd);
       return;
     }
+  size = (size_t) st.st_size;
 
 #ifdef HAVE_MMAP
   /* Now we are ready to load the file.  If mmap() is available we try


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]