Subject: Limits in .h files should avoid casts? Minor SEM_VALUE_MAX problem

Dan Kegel dank@kegel.com
Thu Apr 1 06:23:00 GMT 2004


I ran into some code on the net that contained the line

#if 4294967295 == SEM_VALUE_MAX

This didn't compile, apparantly because SEM_VALUE_MAX's definition
contains a cast:

#define SEM_VALUE_MAX  ((int) ((~0u) >> 1))

A simple test case showing the problem is

#if 1 == ((int) 1)
#endif

Compiling that with gcc-2.95.3 or gcc-3.2.2 gives the error "missing binary operator".
I guess it's reasonable for the preprocessor to not handle casts, come to think of it.

Leaving aside whether it's a good idea to compare SEM_VALUE_MAX to a constant
in the preprocessor, I can't find any other system limit symbols that
have casts in them, so perhaps this is a style violation in glibc, and
SEM_VALUE_MAX should be rewritten in terms of the symbols in limits.h, e.g.

--- linuxthreads-2.3.2/linuxthreads/semaphore.h.old     2004-03-31 12:38:28.000000000 -0800
+++ linuxthreads-2.3.2/linuxthreads/semaphore.h 2004-03-31 12:38:45.000000000 -0800
@@ -16,6 +16,7 @@
  #define _SEMAPHORE_H    1

  #include <features.h>
+#include <limits.h>
  #include <sys/types.h>
  #ifdef __USE_XOPEN2K
  # define __need_timespec
@@ -42,7 +43,7 @@
  #define SEM_FAILED     ((sem_t *) 0)

  /* Maximum value the semaphore can have.  */
-#define SEM_VALUE_MAX  ((int) ((~0u) >> 1))
+#define SEM_VALUE_MAX  MAX_INT


  __BEGIN_DECLS

What say?

- Dan

-- 
My technical stuff: http://kegel.com
My politics: see http://www.misleader.org for examples of why I'm for regime change



More information about the Libc-alpha mailing list