More const-marking cleanup

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-hackers@lists.postgresql.org
Date: 2025-12-04T22:09:30Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Fix some cases of indirectly casting away const.

  2. Fix another case of indirectly casting away const.

  3. ecpg: refactor to eliminate cast-away-const in find_variable().

Attachments

I noticed that our two buildfarm animals that are running Fedora
rawhide (caiman, midge) are emitting warnings like

compression.c: In function "parse_compress_options":
compression.c:458:13: warning: assignment discards "const" qualifier from pointer target type [-Wdiscarded-qualifiers]
  458 |         sep = strchr(option, ':');
      |             ^

Apparently, latest gcc is able to notice that constructions like

	const char *str = ...;
	char       *ptr = strchr(str, ':');

are effectively casting away const.  This is a good thing and long
overdue, but we have some work to do to clean up the places where
we are doing that.

Attached is a patch that cleans up all the cases I saw on a local
rawhide installation.  Most of it is unremarkable, but the changes
in ecpg are less so.  In particular, variable.c is a mess, because
somebody const-ified some of the char* arguments to find_struct()
and find_struct_member() without regard for the fact that all their
char* arguments point at the same string.  (Not that you could
discover that from their nonexistent documentation.)  I considered
just reverting that change, but was able to fix things up reasonably
well at the price of a few extra strdup's.  It turned out that
find_struct_member()'s modification of its input string is actually
entirely useless, because it undoes that before consulting the
string again.  find_struct() and find_variable() need to make
copies, though.

			regards, tom lane