[SSSD] [PATCH] UTIL: Use standard maximum value of type size_t

Lukas Slebodnik lslebodn at redhat.com
Tue Sep 3 08:29:57 UTC 2013


On (02/09/13 22:55), Jakub Hrozek wrote:
>On Mon, Sep 02, 2013 at 03:20:12PM -0400, Simo Sorce wrote:
>> On Mon, 2013-09-02 at 19:18 +0200, Lukas Slebodnik wrote:
>> > ehlo,
>> > 
>> > Some platforms can have defined SIZE_T_MAX.
>> > It is better to use conditional build.
>> > 
>> > Two patches are attached. one for master(1.10) and second for 1.9
>> 
>> 
>> Uhmm defining SIZE_T_MAX as (size_t)-1 is not technically correct, it
>> may work with gcc, but could fail with an optimizer, as -1 is simply an
>> illegal value for an unsigned quantity.
>> 
>> We should use the actual maximum value here.
>> 
>> Simo.
>
>tl;dr - I think we should simply use SIZE_MAX instead.
>
>I actually think Lukas' patch *improves* the code. It seems that the
>non-patched code defined SIZE_T_MAX no matter what. Lukas simply wraps
>the macro #define in an #ifdef so if a platform defined SIZE_T_MAX in
>some kind of system wide header, the definition would be picked up.
>
>But the real question I see is why did we ever use SIZE_T_MAX where we
>should have used SIZE_MAX ? The standard says (7.18.3) that size_t
>maximum value is SIZE_MAX with the minimum value set to 65535 where
>implementations can choose larger values.
>
>Also, what problem do you see with (size_t)-1 ? As far as I see, this
>is the recommended approach. The only alternative I can think of is ~0
>but in several discussions (on comp.lang.c.moderated) this was considered
>unsafe on some machines.

I agree with SIZE_MAX.
Here is part of header file stdint.h
/* Limit of `size_t' type.  */
# if __WORDSIZE == 64
#  define SIZE_MAX              (18446744073709551615UL)
# else
#  define SIZE_MAX              (4294967295U)
# endif

New patches attached and subject is also changed.

LS
-------------- next part --------------
>From 73a0d9a23c3e73fb201065dea4d03e7be0f86329 Mon Sep 17 00:00:00 2001
From: Lukas Slebodnik <lslebodn at redhat.com>
Date: Tue, 3 Sep 2013 09:45:34 +0200
Subject: [PATCH] UTIL: Use standard maximum value of type size_t

It is better to use standard constant for maximum value of type size_t,
instead of reinventing wheel with own defined constant SIZE_T_MAX

This patch replace string "SIZE_T_MAX" -> "SIZE_MAX"
---
 src/tests/util-tests.c    | 12 ++++++------
 src/util/util_safealign.h |  5 ++---
 2 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c
index 4b0d5009c7c65f259f66aa66eb68281098164b74..9af12081dd64d0b94cae7a190bf7915181e3d57b 100644
--- a/src/tests/util-tests.c
+++ b/src/tests/util-tests.c
@@ -388,14 +388,14 @@ END_TEST
 START_TEST(test_size_t_overflow)
 {
     fail_unless(!SIZE_T_OVERFLOW(1, 1), "unexpected overflow");
-    fail_unless(!SIZE_T_OVERFLOW(SIZE_T_MAX, 0), "unexpected overflow");
-    fail_unless(!SIZE_T_OVERFLOW(SIZE_T_MAX-10, 10), "unexpected overflow");
-    fail_unless(SIZE_T_OVERFLOW(SIZE_T_MAX, 1), "overflow not detected");
-    fail_unless(SIZE_T_OVERFLOW(SIZE_T_MAX, SIZE_T_MAX),
+    fail_unless(!SIZE_T_OVERFLOW(SIZE_MAX, 0), "unexpected overflow");
+    fail_unless(!SIZE_T_OVERFLOW(SIZE_MAX-10, 10), "unexpected overflow");
+    fail_unless(SIZE_T_OVERFLOW(SIZE_MAX, 1), "overflow not detected");
+    fail_unless(SIZE_T_OVERFLOW(SIZE_MAX, SIZE_MAX),
                 "overflow not detected");
-    fail_unless(SIZE_T_OVERFLOW(SIZE_T_MAX, ULLONG_MAX),
+    fail_unless(SIZE_T_OVERFLOW(SIZE_MAX, ULLONG_MAX),
                 "overflow not detected");
-    fail_unless(SIZE_T_OVERFLOW(SIZE_T_MAX, -10), "overflow not detected");
+    fail_unless(SIZE_T_OVERFLOW(SIZE_MAX, -10), "overflow not detected");
 }
 END_TEST
 
diff --git a/src/util/util_safealign.h b/src/util/util_safealign.h
index d1d7a4861415ee6b0a755f17247985857b23ed57..e7f34e147fa96171dd6a74a30dbc13581d20746c 100644
--- a/src/util/util_safealign.h
+++ b/src/util/util_safealign.h
@@ -30,11 +30,10 @@
 #define _UTIL_SAFEALIGN_H
 
 #include <string.h>
-
-#define SIZE_T_MAX ((size_t) -1)
+#include <stdint.h>
 
 #define SIZE_T_OVERFLOW(current, add) \
-                        (((size_t)(add)) > (SIZE_T_MAX - ((size_t)(current))))
+                        (((size_t)(add)) > (SIZE_MAX - ((size_t)(current))))
 
 static inline void
 safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter)
-- 
1.8.3.1

-------------- next part --------------
>From f0472b4b371746bd140839202f3acd3a747cccf9 Mon Sep 17 00:00:00 2001
From: Lukas Slebodnik <lslebodn at redhat.com>
Date: Tue, 3 Sep 2013 10:03:22 +0200
Subject: [PATCH] UTIL: Use standard maximum value of type size_t

It is better to use standard constant for maximum value of type size_t,
instead of reinventing wheel with own defined constant SIZE_T_MAX

This patch replace string "SIZE_T_MAX" -> "SIZE_MAX"
---
 src/tests/util-tests.c | 12 ++++++------
 src/util/util.h        |  4 +---
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c
index 3628b0a6b9637c88ca8e90456897cea81daa6fe0..69fdf156ec80e86f937924f97250067d427d7eb2 100644
--- a/src/tests/util-tests.c
+++ b/src/tests/util-tests.c
@@ -388,14 +388,14 @@ END_TEST
 START_TEST(test_size_t_overflow)
 {
     fail_unless(!SIZE_T_OVERFLOW(1, 1), "unexpected overflow");
-    fail_unless(!SIZE_T_OVERFLOW(SIZE_T_MAX, 0), "unexpected overflow");
-    fail_unless(!SIZE_T_OVERFLOW(SIZE_T_MAX-10, 10), "unexpected overflow");
-    fail_unless(SIZE_T_OVERFLOW(SIZE_T_MAX, 1), "overflow not detected");
-    fail_unless(SIZE_T_OVERFLOW(SIZE_T_MAX, SIZE_T_MAX),
+    fail_unless(!SIZE_T_OVERFLOW(SIZE_MAX, 0), "unexpected overflow");
+    fail_unless(!SIZE_T_OVERFLOW(SIZE_MAX-10, 10), "unexpected overflow");
+    fail_unless(SIZE_T_OVERFLOW(SIZE_MAX, 1), "overflow not detected");
+    fail_unless(SIZE_T_OVERFLOW(SIZE_MAX, SIZE_MAX),
                 "overflow not detected");
-    fail_unless(SIZE_T_OVERFLOW(SIZE_T_MAX, ULLONG_MAX),
+    fail_unless(SIZE_T_OVERFLOW(SIZE_MAX, ULLONG_MAX),
                 "overflow not detected");
-    fail_unless(SIZE_T_OVERFLOW(SIZE_T_MAX, -10), "overflow not detected");
+    fail_unless(SIZE_T_OVERFLOW(SIZE_MAX, -10), "overflow not detected");
 }
 END_TEST
 
diff --git a/src/util/util.h b/src/util/util.h
index 606eab03fc6e404f8b838a120b93ac63627eab01..eab1f7880c95bc057e1b6c1aa6d9a80ee4da9a91 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -276,10 +276,8 @@ errno_t set_debug_file_from_fd(const int fd);
 #define OUT_OF_ID_RANGE(id, min, max) \
     (id == 0 || (min && (id < min)) || (max && (id > max)))
 
-#define SIZE_T_MAX ((size_t) -1)
-
 #define SIZE_T_OVERFLOW(current, add) \
-                        (((size_t)(add)) > (SIZE_T_MAX - ((size_t)(current))))
+                        (((size_t)(add)) > (SIZE_MAX - ((size_t)(current))))
 
 static inline void
 safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter)
-- 
1.8.3.1



More information about the sssd-devel mailing list