Skip to content

Commit e07d4dd

Browse files
committed
Suppress compiler warning in relptr_store().
clang 13 with -Wextra warns that "performing pointer subtraction with a null pointer has undefined behavior" in the places where freepage.c tries to set a relptr variable to constant NULL. This appears to be a compiler bug, but it's unlikely to get fixed instantly. Fortunately, we can work around it by introducing an inline support function, which seems like a good change anyway because it removes the macro's existing double-evaluation hazard. Backpatch to v10 where this code was introduced. Patch by me, based on an idea of Andres Freund's. Discussion: https://postgr.es/m/48826.1648310694@sss.pgh.pa.us
1 parent 41b00f8 commit e07d4dd

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/include/utils/relptr.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,32 @@
5656
#define relptr_is_null(rp) \
5757
((rp).relptr_off == 0)
5858

59+
/* We use this inline to avoid double eval of "val" in relptr_store */
60+
static inline Size
61+
relptr_store_eval(char *base, char *val)
62+
{
63+
if (val == NULL)
64+
return 0;
65+
else
66+
{
67+
Assert(val > base);
68+
return val - base;
69+
}
70+
}
71+
5972
#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
6073
#define relptr_store(base, rp, val) \
6174
(AssertVariableIsOfTypeMacro(base, char *), \
6275
AssertVariableIsOfTypeMacro(val, __typeof__((rp).relptr_type)), \
63-
(rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base)))
76+
(rp).relptr_off = relptr_store_eval(base, (char *) (val)))
6477
#else
6578
/*
6679
* If we don't have __builtin_types_compatible_p, assume we might not have
6780
* __typeof__ either.
6881
*/
6982
#define relptr_store(base, rp, val) \
7083
(AssertVariableIsOfTypeMacro(base, char *), \
71-
(rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base)))
84+
(rp).relptr_off = relptr_store_eval(base, (char *) (val)))
7285
#endif
7386

7487
#define relptr_copy(rp1, rp2) \

0 commit comments

Comments
 (0)