Re: safer node casting

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: safer node casting
Date: 2017-01-26 21:55:33
Message-ID: 8401.1485467733@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Andres Freund <andres(at)anarazel(dot)de> writes:
> How about something like the attached? The first patch just contains
> castNode(), the second one a rebased version of Peter's changes (with
> some long lines broken up).

Looks generally good. A couple quibbles from a quick read-through:

* All but the first change in ProcessCopyOptions seem rather pointless:

else if (defel->arg && IsA(defel->arg, List))
- cstate->force_quote = (List *) defel->arg;
+ cstate->force_quote = castNode(List, defel->arg);

In these places, castNode() isn't checking anything the if-condition
didn't. Maybe it's good style anyway, but I'm not really convinced.

* In ExecInitAgg:

aggnode = list_nth(node->chain, phase - 1);
- sortnode = (Sort *) aggnode->plan.lefttree;
- Assert(IsA(sortnode, Sort));
+ sortnode = castNode(Sort, aggnode->plan.lefttree);

it seems like the assignment to aggnode ought to have a castNode on it too
(the fact that it lacks any cast now is sloppy and not per project style,
IMO).

There were a bunch of places in ab1f0c822 where I wished I had this,
but I can go back and back-fill that later; doesn't need to be in the
first commit.

BTW, maybe it's just the first flush of enthusiasm, but I can see us
using this so much that the lack of it in back branches will become
a serious PITA for back-patching. So I'm strongly tempted to propose
that your 0001 should be back-patched. However, before 9.6 we didn't
have the compiler requirement that "static inline" in headers must be
handled sanely. Maybe a useful compromise would be to put 0001 in 9.6,
and before that just add

#define castNode(_type_,nodeptr) ((_type_ *)(nodeptr))

which would allow the notation to be used safely, albeit without
any assertion backup. Alternatively, we could enable the assertion
code only for gcc, which would probably be plenty good enough for
finding bugs in stable branches.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2017-01-26 21:55:37 Re: [PATCH] Rename pg_switch_xlog to pg_switch_wal
Previous Message Alvaro Herrera 2017-01-26 21:46:39 Re: Failure in commit_ts tap tests