Skip to contentSkip to conversation

osdep: Deal with TIME_MAX and OpenBSD 64-bit time_t

PATCH In review Brad Smith 5 participants 6 messages Nov 6, 2017 – Nov 23, 2017

lore
Share & tools
lore

Brad Smith

author
#0 lore raw

PATCH 1/1 osdep: Deal with TIME_MAX and OpenBSD 64-bit time_t

Define TIME_MAX to LLONG_MAX for OpenBSD since OpenBSD uses 64-bit time_t.

include/qemu/osdep.h +4 -0
index 6855b94bbf..824714049b 100644
@@ -132,8 +132,12 @@ extern int daemon(int, int);
 #define ESHUTDOWN 4099
 #endif
 #ifndef TIME_MAX
+#ifdef __OpenBSD__
+#define TIME_MAX LLONG_MAX
+#else
 #define TIME_MAX LONG_MAX
 #endif
+#endif
 
 /* HOST_LONG_BITS is the size of a native pointer in bits. */
 #if UINTPTR_MAX == UINT32_MAX
#1 lore raw
Quoted 10 lines · Brad Smith

Define TIME_MAX to LLONG_MAX for OpenBSD since OpenBSD uses 64-bit time_t.

include/qemu/osdep.h +0 -0
index 6855b94bbf..824714049b 100644

include/qemu/osdep.h extern int daemon(int, int); · line 140

Quoted 9 lines
include/qemu/osdep.h +4 -0
@@ -132,8 +132,12 @@ extern int daemon(int, int);
 #define ESHUTDOWN 4099
 #endif
 #ifndef TIME_MAX
+#ifdef __OpenBSD__
+#define TIME_MAX LLONG_MAX
+#else
 #define TIME_MAX LONG_MAX
 #endif
+#endif

I'm not really a fan of adding new OS-specific #ifdefs -- what if one of the other BSDs uses or switches to 64-bit time_t for 32-bit platforms? Is there some way we can detect this generically at compile time (possibly in configure) ?

thanks
-- PMM

#2 lore raw
Quoted 17 lines · Peter Maydell
Quoted 16 lines · Brad Smith

Define TIME_MAX to LLONG_MAX for OpenBSD since OpenBSD uses 64-bit time_t.

include/qemu/osdep.h +3 -0
index 6855b94bbf..824714049b 100644
@@ -132,8 +132,12 @@ extern int daemon(int, int);
 #define ESHUTDOWN 4099
 #endif
 #ifndef TIME_MAX
+#ifdef __OpenBSD__
+#define TIME_MAX LLONG_MAX
+#else
Quoted 8 lines

include/qemu/osdep.h extern int daemon(int, int); · line 140

Quoted 3 lines
include/qemu/osdep.h +1 -0
 #define TIME_MAX LONG_MAX
 #endif
+#endif

I'm not really a fan of adding new OS-specific #ifdefs -- what if one of the other BSDs uses or switches to 64-bit time_t for 32-bit platforms? Is there some way we can detect this generically at compile time (possibly in configure) ?

You could use a pair of compile time asserts to figure it out.

Would need one compile test to check 32 vs 64 bit:

  #include <time.h>
  char time_t_64bit[sizeof(time_t) == 8 ? 1 : -1];

and a second to check signed vs unsigned:

  #include <time.h>
  char time_t_signed[(time_t) -1 < 0 ? 1 : -1];

Save each of these programs to the file $TMPC, and then run 'compile_object' from configure. You then have a decision matrix for 4 different TIME_MAX values to write into config-host.h.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 36 lines · Daniel P. Berrange
Quoted 17 lines · Peter Maydell
Quoted 16 lines · Brad Smith

Define TIME_MAX to LLONG_MAX for OpenBSD since OpenBSD uses 64-bit time_t.

include/qemu/osdep.h +3 -0
index 6855b94bbf..824714049b 100644
@@ -132,8 +132,12 @@ extern int daemon(int, int);
 #define ESHUTDOWN 4099
 #endif
 #ifndef TIME_MAX
+#ifdef __OpenBSD__
+#define TIME_MAX LLONG_MAX
+#else
Quoted 8 lines

include/qemu/osdep.h extern int daemon(int, int); · line 140

Quoted 3 lines
include/qemu/osdep.h +1 -0
 #define TIME_MAX LONG_MAX
 #endif
+#endif

I'm not really a fan of adding new OS-specific #ifdefs -- what if one of the other BSDs uses or switches to 64-bit time_t for 32-bit platforms? Is there some way we can detect this generically at compile time (possibly in configure) ?

You could use a pair of compile time asserts to figure it out.

Would need one compile test to check 32 vs 64 bit:

  #include <time.h>
  char time_t_64bit[sizeof(time_t) == 8 ? 1 : -1];

and a second to check signed vs unsigned:

Quoted 7 lines
  #include <time.h>
  char time_t_signed[(time_t) -1 < 0 ? 1 : -1];

Save each of these programs to the file $TMPC, and then run 'compile_object' from configure. You then have a decision matrix for 4 different TIME_MAX values to write into config-host.h.

What about

#define type_max(t)                   \
    ((t) -1 > 0                       \
     ? (t)~0                          \
     : (((t)1) <<                     \
         (sizeof(t) * 8 - 1)) - 1)    \

#define TIME_MAX type_max(time_t)

? We don't need it to be a cpp constant, do we? Or if we did, we could assume it to be signed, it's enough for the three users.

Thanks,

Paolo

#4 lore raw
Quoted 8 lines · Paolo Bonzini

What about

#define type_max(t)                   \
    ((t) -1 > 0                       \
     ? (t)~0                          \
     : (((t)1) <<                     \
         (sizeof(t) * 8 - 1)) - 1)    \
Quoted 4 lines

#define TIME_MAX type_max(time_t)

? We don't need it to be a cpp constant, do we? Or if we did, we could assume it to be signed, it's enough for the three users.

GCC doesn't like this phrasing, it complains
qmp.c: In function ‘qmp_expire_password’:
qmp.c:334:89: error: integer overflow in expression [-Werror=overflow]

because of the shift into the sign bit. Instead we can borrow the TYPE_MAXIMUM macro from gnulib, which uses basically the same approach but avoids the UB and the compiler warning. I'll send a patch in a bit.

thanks
-- PMM

#5 lore raw
In reply to Peter Maydell
Quoted 17 lines · Peter Maydell
Quoted 16 lines · Brad Smith

Define TIME_MAX to LLONG_MAX for OpenBSD since OpenBSD uses 64-bit time_t.

include/qemu/osdep.h +3 -0
index 6855b94bbf..824714049b 100644
@@ -132,8 +132,12 @@ extern int daemon(int, int);
 #define ESHUTDOWN 4099
 #endif
 #ifndef TIME_MAX
+#ifdef __OpenBSD__
+#define TIME_MAX LLONG_MAX
+#else
Quoted 9 lines

include/qemu/osdep.h extern int daemon(int, int); · line 140

Quoted 3 lines
include/qemu/osdep.h +1 -0
 #define TIME_MAX LONG_MAX
 #endif
+#endif

I'm not really a fan of adding new OS-specific #ifdefs -- what if one of the other BSDs uses or switches to 64-bit time_t for 32-bit platforms? Is there some way we can detect this generically at compile time (possibly in configure) ?

NetBSD 32-bit ports switched to 64-bit in 2009. There are no longer supported releases with 32-bit time_t.

Quoted 3 lines

thanks
-- PMM