pgsql: Improve pg_list.h's linitial(), lsecond() and co macros

From: David Rowley <drowley(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Improve pg_list.h's linitial(), lsecond() and co macros
Date: 2020-09-28 01:47:53
Message-ID: E1kMiGf-0007I4-8s@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Improve pg_list.h's linitial(), lsecond() and co macros

Prior to this commit, the linitial(), lsecond(), lthird(), lfourth()
macros and their int and Oid list cousins would call their corresponding
inlined function to fetch the cell of interest. Those inline functions
were kind enough to return NULL if the particular cell did not exist.
Unfortunately, the care that these functions took was of no relevance to
the calling macros as they proceeded to directly dereference the returned
value without any regard to whether that value was NULL or not. If it had
been, we'd have segfaulted.

Of course, the fact that we would have segfaulted on misuse of these
macros just goes to prove that nobody is relying on the empty or list too
small checks. So here we just get rid of those checks completely.

The existing inline functions have been left alone as someone may be using
those directly. We just replace the call within each macro to use
list_nth_cell().

For the llast*() case we require a new list_last_cell() inline function to
get away from the multiple evaluation hazard that we'd get if we fetched
->length on the macro's parameter.

Author: David Rowley
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/CAApHDvpo1zj9KhEpU2cCRZfSM3Q6XGdhzuAS2v79PH7WJBkYVA@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/cc99baa43e0ed83ac18fcbde31f2ab7274eb26cf

Modified Files
--------------
src/include/nodes/pg_list.h | 41 +++++++++++++++++++++++++----------------
1 file changed, 25 insertions(+), 16 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2020-09-28 02:24:19 pgsql: Improve tab-completion for DEALLOCATE.
Previous Message Michael Paquier 2020-09-28 01:15:13 pgsql: Improve range checks of options for pg_test_fsync and pg_test_ti