const qualifier for list APIs

Started by Ashutosh Bapatover 3 years ago2 messageshackers
Jump to latest
#1Ashutosh Bapat
ashutosh.bapat@enterprisedb.com

Hi All,
Functions like lappend_*() in list.c do not modify the second
argument. So it can be qualified as const. Any reason why we don't do
that? Is it because the target pointer ptr_value is not const
qualified?

In my code, I am using lappend() and passing it the output of
pq_getmsgstring() which returns const char *. The list is used to just
collect these pointers to be scanned later a few times within the same
function. So there is no possibility of freeing or changing area
within the StringInfo. So the coding practice though questionable, is
safe and avoids unnecessary pallocs. But SonarQube does complain about
it.

--
Best Wishes,
Ashutosh Bapat

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ashutosh Bapat (#1)
Re: const qualifier for list APIs

Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> writes:

Functions like lappend_*() in list.c do not modify the second
argument. So it can be qualified as const. Any reason why we don't do
that? Is it because the target pointer ptr_value is not const
qualified?

It would be a lie in many (most?) cases, wherever somebody later pulls the
pointer out of the list without applying "const" to it. So I can't see
that adding "const" there would be an improvement.

So the coding practice though questionable, is
safe and avoids unnecessary pallocs. But SonarQube does complain about
it.

Maybe an explicit cast to (void *) would shut it up.

regards, tom lane