-Wcast-qual home stretch
We have been progressively cleaning up -Wcast-qual warnings this year.
This patch set is the final part of this, containing a few tricky,
complicated, and/or ugly cases that I have been putting off. And then
we can turn -Wcast-qual on by default. Each patch is individually
described in its commit message. Alternative approaches are possible;
feedback welcome.
Attachments:
t254539_10001-Fix-accidentally-casting-away-const.patchtext/plain; charset=UTF-8; name=0001-Fix-accidentally-casting-away-const.patchDownload+1-2
0002-Fix-Wcast-qual-warning-in-lnext.patchtext/plain; charset=UTF-8; name=0002-Fix-Wcast-qual-warning-in-lnext.patchDownload+4-5
0003-Fix-Wcast-qual-warning-in-foreach_delete_current.patchtext/plain; charset=UTF-8; name=0003-Fix-Wcast-qual-warning-in-foreach_delete_current.patchDownload+11-2
0004-Fix-Wcast-qual-warning-in-XLogInsertRecord.patchtext/plain; charset=UTF-8; name=0004-Fix-Wcast-qual-warning-in-XLogInsertRecord.patchDownload+13-8
0005-Fix-Wcast-qual-warnings-in-regex-module.patchtext/plain; charset=UTF-8; name=0005-Fix-Wcast-qual-warnings-in-regex-module.patchDownload+7-10
0006-Fix-Wcast-qual-warning-in-guc_funcs.c.patchtext/plain; charset=UTF-8; name=0006-Fix-Wcast-qual-warning-in-guc_funcs.c.patchDownload+9-6
0007-Add-unconstify_pointee.patchtext/plain; charset=UTF-8; name=0007-Add-unconstify_pointee.patchDownload+20-8
0008-Fix-Wcast-qual-warnings-in-read-write-accessor-macro.patchtext/plain; charset=UTF-8; name=0008-Fix-Wcast-qual-warnings-in-read-write-accessor-macro.patchDownload+15-17
0009-Adjust-unconstify-unvolatize-to-avoid-Wcast-qual-war.patchtext/plain; charset=UTF-8; name=0009-Adjust-unconstify-unvolatize-to-avoid-Wcast-qual-war.patchDownload+5-6
0010-Add-warning-option-Wcast-qual.patchtext/plain; charset=UTF-8; name=0010-Add-warning-option-Wcast-qual.patchDownload+5-5
On Nov 3, 2026, at 23:34, Peter Eisentraut <peter@eisentraut.org> wrote:
We have been progressively cleaning up -Wcast-qual warnings this year. This patch set is the final part of this, containing a few tricky, complicated, and/or ugly cases that I have been putting off. And then we can turn -Wcast-qual on by default. Each patch is individually described in its commit message. Alternative approaches are possible; feedback welcome.<0001-Fix-accidentally-casting-away-const.patch><0002-Fix-Wcast-qual-warning-in-lnext.patch><0003-Fix-Wcast-qual-warning-in-foreach_delete_current.patch><0004-Fix-Wcast-qual-warning-in-XLogInsertRecord.patch><0005-Fix-Wcast-qual-warnings-in-regex-module.patch><0006-Fix-Wcast-qual-warning-in-guc_funcs.c.patch><0007-Add-unconstify_pointee.patch><0008-Fix-Wcast-qual-warnings-in-read-write-accessor-macro.patch><0009-Adjust-unconstify-unvolatize-to-avoid-Wcast-qual-war.patch><0010-Add-warning-option-Wcast-qual.patch>
The patch set overall looks good to me. I have a few comments as below:
1 - 0001
```
--- a/src/include/access/detoast.h
+++ b/src/include/access/detoast.h
@@ -23,7 +23,7 @@
*/
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr) \
do { \
- varattrib_1b_e *attre = (varattrib_1b_e *) (attr); \
+ const varattrib_1b_e *attre = (const varattrib_1b_e *) (attr); \
```
Looks like the header comment needs to be updated accordingly:
```
* intermediate "varattrib_1b_e *" variable seems to fix it.
```
2 - 0002
```
static inline ListCell *
lnext(const List *l, const ListCell *c)
{
- Assert(c >= &l->elements[0] && c < &l->elements[l->length]);
- c++;
- if (c < &l->elements[l->length])
- return (ListCell *) c;
+ int n = list_cell_number(l, c) + 1;
+
+ if (n < l->length)
+ return &l->elements[n];
else
return NULL;
}
```
Here, I think the problem is that c is const while the return value is not const. Can't we just use unconstify(), like:
```
return unconstify(ListCell *, c);
```
Feels like VARDATA_EXTERNAL is similar use case that is used in 0001. It calls VARDATA_EXTERNAL(attre), where attre is const, and VARDATA_EXTERNAL() uses unconstify() to make it non-const.
3 - 0006
Since BuildTupleFromCStrings doesn’t update values, making values as a const feels more reasonable, like:
```
extern HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, const char *const*values);
```
I tried that, but that required more callers’ changes. So, I’d reserve this comment, maybe we can do that with a separate refactoring patch.
4 - After applying the patch set, my local build still gets a couple of warnings:
```
input.c:306:27: warning: cast from 'const char *' to 'char *' drops const qualifier [-Wcast-qual]
306 | for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
| ^
input.c:326:27: warning: cast from 'const char *' to 'char *' drops const qualifier [-Wcast-qual]
326 | for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
| ^
2 warnings generated.
```
We can fix them by using unconstify:
```
diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c
index 6d37359fc96..177434ed975 100644
--- a/src/bin/psql/input.c
+++ b/src/bin/psql/input.c
@@ -303,7 +303,7 @@ encode_history(void)
char *cur_ptr;
/* some platforms declare HIST_ENTRY.line as const char * */
- for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
+ for (cur_ptr = unconstify(char *, cur_hist->line); *cur_ptr; cur_ptr++)
{
if (*cur_ptr == '\n')
*cur_ptr = NL_IN_HISTORY;
@@ -323,7 +323,7 @@ decode_history(void)
char *cur_ptr;
/* some platforms declare HIST_ENTRY.line as const char * */
- for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
+ for (cur_ptr = unconstify(char *, cur_hist->line); *cur_ptr; cur_ptr++)
{
if (*cur_ptr == NL_IN_HISTORY)
*cur_ptr = '\n';
```
But that doesn’t feel good, because we are writing to the pointers, forcing to unconstify those points might be unsafe.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
On 04.11.26 03:46, Chao Li wrote:
The patch set overall looks good to me. I have a few comments as below:
1 - 0001 ``` --- a/src/include/access/detoast.h +++ b/src/include/access/detoast.h @@ -23,7 +23,7 @@ */ #define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr) \ do { \ - varattrib_1b_e *attre = (varattrib_1b_e *) (attr); \ + const varattrib_1b_e *attre = (const varattrib_1b_e *) (attr); \ ```Looks like the header comment needs to be updated accordingly:
```
* intermediate "varattrib_1b_e *" variable seems to fix it.
```
Hmm, I see what you mean, but I think this would distract from the point
of the comment, which is about the pointer type, not about whether it's
qualified.
2 - 0002 ``` static inline ListCell * lnext(const List *l, const ListCell *c) { - Assert(c >= &l->elements[0] && c < &l->elements[l->length]); - c++; - if (c < &l->elements[l->length]) - return (ListCell *) c; + int n = list_cell_number(l, c) + 1; + + if (n < l->length) + return &l->elements[n]; else return NULL; } ```Here, I think the problem is that c is const while the return value is not const. Can't we just use unconstify(), like:
```
return unconstify(ListCell *, c);
```
Well, we could, but then we should justify why this is safe. The way I
have written it, we don't have to do that but we can instead let the
code speak for itself.
Feels like VARDATA_EXTERNAL is similar use case that is used in 0001. It calls VARDATA_EXTERNAL(attre), where attre is const, and VARDATA_EXTERNAL() uses unconstify() to make it non-const.
No, VARDATA_EXTERNAL is different, because it really should be
const-preserving, like in C++ you would overload it like
static inline char *VARDATA_EXTERNAL(void *PTR);
static inline const char *VARDATA_EXTERNAL(const void *PTR);
lnext() is not const-preserving. The input cell is always const and the
output cell is always non-const. The old code was just using the "c"
variable for both, which was slightly wrong.
3 - 0006
Since BuildTupleFromCStrings doesn’t update values, making values as a const feels more reasonable, like:
```
extern HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, const char *const*values);
```I tried that, but that required more callers’ changes. So, I’d reserve this comment, maybe we can do that with a separate refactoring patch.
You can't change BuildTupleFromCStrings(), because then you would break
callers who want to pass in a char **. You could make a separate new
function that takes a const char **. But it doesn't seem worth it for
just one caller.
4 - After applying the patch set, my local build still gets a couple of warnings:
We can fix them by using unconstify: ``` diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 6d37359fc96..177434ed975 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -303,7 +303,7 @@ encode_history(void) char *cur_ptr;/* some platforms declare HIST_ENTRY.line as const char * */ - for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++) + for (cur_ptr = unconstify(char *, cur_hist->line); *cur_ptr; cur_ptr++) { if (*cur_ptr == '\n') *cur_ptr = NL_IN_HISTORY; @@ -323,7 +323,7 @@ decode_history(void) char *cur_ptr;/* some platforms declare HIST_ENTRY.line as const char * */ - for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++) + for (cur_ptr = unconstify(char *, cur_hist->line); *cur_ptr; cur_ptr++) { if (*cur_ptr == NL_IN_HISTORY) *cur_ptr = '\n'; ```
Ok, what platform and libreadline/libedit version is this? (It's
apparently not covered on CI.)
But that doesn’t feel good, because we are writing to the pointers, forcing to unconstify those points might be unsafe.
Right, but we are already doing it. Probably the underlying storage of
what those pointers point to is not const, so it would be ok.
On Nov 5, 2026, at 05:55, Peter Eisentraut <peter@eisentraut.org> wrote:
On 04.11.26 03:46, Chao Li wrote:
4 - After applying the patch set, my local build still gets a couple of warnings:
We can fix them by using unconstify: ``` diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 6d37359fc96..177434ed975 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -303,7 +303,7 @@ encode_history(void) char *cur_ptr; /* some platforms declare HIST_ENTRY.line as const char * */ - for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++) + for (cur_ptr = unconstify(char *, cur_hist->line); *cur_ptr; cur_ptr++) { if (*cur_ptr == '\n') *cur_ptr = NL_IN_HISTORY; @@ -323,7 +323,7 @@ decode_history(void) char *cur_ptr; /* some platforms declare HIST_ENTRY.line as const char * */ - for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++) + for (cur_ptr = unconstify(char *, cur_hist->line); *cur_ptr; cur_ptr++) { if (*cur_ptr == NL_IN_HISTORY) *cur_ptr = '\n'; ```Ok, what platform and libreadline/libedit version is this? (It's apparently not covered on CI.)
It's MacOS 26.6.2 on arm64, using Apple’s system libedit. otool -L reports current version 3.0.0 and compatibility version 2.0.0:
```
% otool -L src/bin/psql/psql
src/bin/psql/psql:
/usr/local/pgsql/lib/libpq.5.dylib (compatibility version 5.0.0, current version 5.20.0)
/usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1356.0.0)
```
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
On 05.11.26 06:39, Chao Li wrote:
Ok, what platform and libreadline/libedit version is this? (It's apparently not covered on CI.)
It's MacOS 26.6.2 on arm64, using Apple’s system libedit. otool -L reports current version 3.0.0 and compatibility version 2.0.0:
Here is an updated patch set. It needed a bit of rebasing over this
week's security fixes. And I included a fix for the macOS libedit issue.
Attachments:
t254539_5v2-0001-Fix-accidentally-casting-away-const.patchtext/plain; charset=UTF-8; name=v2-0001-Fix-accidentally-casting-away-const.patchDownload+1-2
v2-0002-Fix-Wcast-qual-warning-in-lnext.patchtext/plain; charset=UTF-8; name=v2-0002-Fix-Wcast-qual-warning-in-lnext.patchDownload+4-5
v2-0003-Fix-Wcast-qual-warning-in-foreach_delete_current.patchtext/plain; charset=UTF-8; name=v2-0003-Fix-Wcast-qual-warning-in-foreach_delete_current.patchDownload+11-2
v2-0004-Fix-Wcast-qual-warning-in-XLogInsertRecord.patchtext/plain; charset=UTF-8; name=v2-0004-Fix-Wcast-qual-warning-in-XLogInsertRecord.patchDownload+13-8
v2-0005-Fix-Wcast-qual-warnings-in-regex-module.patchtext/plain; charset=UTF-8; name=v2-0005-Fix-Wcast-qual-warnings-in-regex-module.patchDownload+7-10
v2-0006-Fix-Wcast-qual-warning-in-guc_funcs.c.patchtext/plain; charset=UTF-8; name=v2-0006-Fix-Wcast-qual-warning-in-guc_funcs.c.patchDownload+11-6
v2-0007-Fix-Wcast-qual-warnings-in-psql.patchtext/plain; charset=UTF-8; name=v2-0007-Fix-Wcast-qual-warnings-in-psql.patchDownload+2-3
v2-0008-Add-unconstify_pointee.patchtext/plain; charset=UTF-8; name=v2-0008-Add-unconstify_pointee.patchDownload+20-8
v2-0009-Fix-Wcast-qual-warnings-in-read-write-accessor-ma.patchtext/plain; charset=UTF-8; name=v2-0009-Fix-Wcast-qual-warnings-in-read-write-accessor-ma.patchDownload+15-17
v2-0010-Adjust-unconstify-unvolatize-to-avoid-Wcast-qual-.patchtext/plain; charset=UTF-8; name=v2-0010-Adjust-unconstify-unvolatize-to-avoid-Wcast-qual-.patchDownload+5-6
v2-0011-Add-warning-option-Wcast-qual.patchtext/plain; charset=UTF-8; name=v2-0011-Add-warning-option-Wcast-qual.patchDownload+5-5
On Nov 13, 2026, at 17:36, Peter Eisentraut <peter@eisentraut.org> wrote:
On 05.11.26 06:39, Chao Li wrote:
Ok, what platform and libreadline/libedit version is this? (It's apparently not covered on CI.)
It's MacOS 26.6.2 on arm64, using Apple’s system libedit. otool -L reports current version 3.0.0 and compatibility version 2.0.0:
Here is an updated patch set. It needed a bit of rebasing over this week's security fixes. And I included a fix for the macOS libedit issue.
<v2-0001-Fix-accidentally-casting-away-const.patch><v2-0002-Fix-Wcast-qual-warning-in-lnext.patch><v2-0003-Fix-Wcast-qual-warning-in-foreach_delete_current.patch><v2-0004-Fix-Wcast-qual-warning-in-XLogInsertRecord.patch><v2-0005-Fix-Wcast-qual-warnings-in-regex-module.patch><v2-0006-Fix-Wcast-qual-warning-in-guc_funcs.c.patch><v2-0007-Fix-Wcast-qual-warnings-in-psql.patch><v2-0008-Add-unconstify_pointee.patch><v2-0009-Fix-Wcast-qual-warnings-in-read-write-accessor-ma.patch><v2-0010-Adjust-unconstify-unvolatize-to-avoid-Wcast-qual-.patch><v2-0011-Add-warning-option-Wcast-qual.patch>
Thanks for updating the patch. With v2, my local build passed without any warning.
I also went through the patch set again, no more finding. So, v2 overall looks good to me.
A nitpick on 0009:
```
#define ARR_DIMS(a) \
- ((int *) (((char *) (a)) + sizeof(ArrayType)))
+ ((int *) ((unconstify(char *, (const char *) (a))) + sizeof(ArrayType)))
#define ARR_LBOUND(a) \
- ((int *) (((char *) (a)) + sizeof(ArrayType) + \
+ ((int *) ((unconstify(char *, (const char *) (a))) + sizeof(ArrayType) + \
sizeof(int) * ARR_NDIM(a)))
#define ARR_NULLBITMAP(a) \
(ARR_HASNULL(a) ? \
- (uint8 *) (((char *) (a)) + sizeof(ArrayType) + \
+ (uint8 *) ((unconstify(char *, (const char *) (a))) + sizeof(ArrayType) + \
2 * sizeof(int) * ARR_NDIM(a)) \
: (uint8 *) NULL)
```
For these three macros, unconstify() already wraps its result in parentheses, so the additional parentheses around the unconstify() calls are unnecessary. For example, ARR_DIMS() could be simplified to:
```
#define ARR_DIMS(a) \
((int *) (unconstify(char *, (const char *) (a)) + sizeof(ArrayType)))
```
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/