misc/176628: [stdint.h] use safer way of definint __WORDSIZE

Dmitry Marakasov amdmi3 at FreeBSD.org
Mon Mar 4 01:50:01 UTC 2013


>Number:         176628
>Category:       misc
>Synopsis:       [stdint.h] use safer way of definint __WORDSIZE
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Mar 04 01:50:00 UTC 2013
>Closed-Date:
>Last-Modified:
>Originator:     Dmitry Marakasov
>Release:        FreeBSD 9.0-RELEASE-p3 amd64
>Organization:
>Environment:
System: FreeBSD hades.panopticon 9.0-RELEASE-p3 FreeBSD 9.0-RELEASE-p3 #0: Wed Jun 13 17:39:20 MSK 2012 root at hades.panopticon:/usr/obj/usr/src/sys/HADES amd64


>Description:
r228529 introduced __WORDSIZE macro:

--- sys/sys/stdint.h
+#if defined(UINTPTR_MAX) && defined(UINT64_MAX) && (UINTPTR_MAX == UINT64_MAX)
+#define	__WORDSIZE		64
+#else
+#define	__WORDSIZE		32
+#endif
---

However the way it's defined is utterly unsafe: when UINTPTR_MAX or UINT64_MAX are not defined (which is the case for C++, as their definitions in e.g. x86/_stint.h are wrapped in

#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS)

__WORDSIZE is always defined as 32, which is wrong on 64bit systems.

I have two solutions for the problem.
First one uses the same way of testing for 64bit pointers, but doesn't define __WORDSIZE if it can't be detected reliably.
Second one uses different way of testing for 64bit pointers with checking for __LP64__.

The second one looks much more useful, but I'm not sure if __LP64__ has the right semantics and will work in all platforms.

Also, can't it just be unconditionally defined to (sizeof(int*)*8)?

>How-To-Repeat:
>Fix:

--- wordsize.patch begins here ---
diff --git sys/sys/stdint.h sys/sys/stdint.h
index 762e879..de10869 100644
--- sys/sys/stdint.h
+++ sys/sys/stdint.h
@@ -65,10 +65,12 @@ typedef	__uintmax_t		uintmax_t;
 #endif
 
 /* GNU and Darwin define this and people seem to think it's portable */
-#if defined(UINTPTR_MAX) && defined(UINT64_MAX) && (UINTPTR_MAX == UINT64_MAX)
-#define	__WORDSIZE		64
-#else
-#define	__WORDSIZE		32
+#if defined(UINTPTR_MAX) && defined(UINT64_MAX)
+# if UINTPTR_MAX == UINT64_MAX
+#  define	__WORDSIZE		64
+# else
+#  define	__WORDSIZE		32
+# endif
 #endif
 
 /* Limits of wchar_t. */
--- wordsize.patch ends here ---

--- wordsize.1.patch begins here ---
diff --git sys/sys/stdint.h sys/sys/stdint.h
index 762e879..b921b99 100644
--- sys/sys/stdint.h
+++ sys/sys/stdint.h
@@ -65,7 +65,7 @@ typedef	__uintmax_t		uintmax_t;
 #endif
 
 /* GNU and Darwin define this and people seem to think it's portable */
-#if defined(UINTPTR_MAX) && defined(UINT64_MAX) && (UINTPTR_MAX == UINT64_MAX)
+#if defined(__LP64__)
 #define	__WORDSIZE		64
 #else
 #define	__WORDSIZE		32
--- wordsize.1.patch ends here ---
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list