Re: safer node casting
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>, Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2017-02-24T12:31:27Z
Lists: pgsql-hackers
Hi, I was about to add a few more uses of castNode, which made me think. You proposed replacing: On 2016-12-31 12:08:22 -0500, Peter Eisentraut wrote: > There is a common coding pattern that goes like this: > > RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc); > Assert(IsA(rinfo, RestrictInfo)); with > +#define castNode(_type_,nodeptr) (AssertMacro(!nodeptr || IsA(nodeptr,_type_)), (_type_ *)(nodeptr)) (now an inline function, but that's besides my point) Those aren't actually equivalent, because of the !nodeptr. IsA() crashes for NULL pointers, but the new code won't. Which means 9ba8a9ce4548b et al actually weakened some asserts. Should we perhaps have one NULL accepting version (castNodeNull?) and one that separately asserts that ptr != NULL? Greetings, Andres Freund
Commits
-
Suppress unused-variable warning.
- c56ac2913a1f 10.0 landed
-
Make more use of castNode()
- 38d103763d14 10.0 cited
-
Add castNode(type, ptr) for safe casting between NodeTag based types.
- 1ad163ef0130 9.3.16 landed
- 14d0e290cbe7 9.2.20 landed
- cf8c86af950b 9.4.11 landed
- 8fef0c34143b 9.5.6 landed
- 5bcab111426e 10.0 landed
- 574b091e5835 9.6.2 landed
-
Use the new castNode() macro in a number of places.
- 9ba8a9ce4548 10.0 landed