<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54570>54570</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Spurious -Wnull-pointer-subtraction inside macros / unreachable code
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
anarazel
</td>
</tr>
</table>
<pre>
Hi,
Compiling postgres with clang 13 or newer triggers a host of -Wnull-pointer-subtraction warnings (discovered because the warning is part of -Wextra). To us it looks like the warnings are spurious.
Simplified reproducer (also at https://godbolt.org/z/ErfEc6Ezz):
compiling
```C
#include <stddef.h>
#define relptr_store(base, rp, val) \
((rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base)))
typedef union { struct foo *relptr_type; size_t relptr_off; } relptr;
void
problem_not_present(relptr *rp, char *base)
{
struct foo *val = NULL;
relptr_store(base, *rp, val);
}
void
problem_present(relptr *rp, char *base)
{
relptr_store(base, *rp, NULL);
}
```
with just clang -Wnull-pointer-subtraction yields:
```
<source>:19:5: warning: performing pointer subtraction with a null pointer has undefined behavior [-Wnull-pointer-subtraction]
relptr_store(base, *rp, NULL);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:4:60: note: expanded from macro 'relptr_store'
((rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base)))
~~~~~~~~~~~~~~~~ ^
1 warning generated.
```
The way I read the standard legalese it's the *evaluation* that'd produce undefined behavior, but it never is evaluated here.
@jamieschmeiser, looks like this came in with your 9cb00b9ecbe74d19389a5818d61ddee328afe031 / https://reviews.llvm.org/D98798?
Regards,
Andres
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFVktv4zYQ_jXyZWBDD8uyDjp4YxstsOihu0WPAUWOLGYpUSApZ51f3yEtO3aQTbEtigq0xOfMNy9-rrU4Vb_IKH2I4m0Ub87vB90NUsn-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