Skip to content

Fix -Wdiscarded-qualifiers warning in kms_request.c#2354

Closed
jeroen wants to merge 1 commit into
mongodb:masterfrom
jeroen:fix-strchr-const-warning
Closed

Fix -Wdiscarded-qualifiers warning in kms_request.c#2354
jeroen wants to merge 1 commit into
mongodb:masterfrom
jeroen:fix-strchr-const-warning

Conversation

@jeroen

@jeroen jeroen commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

This fixes a (problematic) -Wdiscarded-qualifiers warning that we get in the R bindings on gcc-16:

kms/kms_request.c: In function 'parse_query_params':
kms/kms_request.c:35:14: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
   35 |       equals = strchr ((const char *) p, '=');
      |              ^
kms/kms_request.c:40:11: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
   40 |       amp = strchr ((const char *) equals, '&');
      |           ^

AI summary: In C23 (the default language standard for GCC 15), strchr is const-generic: when passed a const char * it returns a const char *. The explicit (const char *) casts in parse_query_params therefore make the result unassignable to the non-const equals and amp pointers.

Since p and equals are already plain char *, the casts serve no purpose; removing them is warning-free on both old and new compilers.

Remove unnecessary (const char *) casts on the strchr arguments in
parse_query_params. In C23 (the default for GCC 15) strchr is
const-generic: passing a const char * makes it return const char *,
which cannot be assigned to the non-const 'equals' and 'amp' pointers:

kms/kms_request.c: In function 'parse_query_params':
kms/kms_request.c:35:14: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
   35 |       equals = strchr ((const char *) p, '=');
      |              ^
kms/kms_request.c:40:11: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
   40 |       amp = strchr ((const char *) equals, '&');
      |           ^

Since p and equals are already plain char *, the casts serve no
purpose and dropping them is warning-free on both old and new
compilers.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@jeroen
jeroen requested a review from a team as a code owner July 20, 2026 14:12
@jeroen
jeroen requested a review from vector-of-bool July 20, 2026 14:12
jeroen added a commit to jeroen/mongolite that referenced this pull request Jul 20, 2026
In C23 (default for GCC 15) strchr is const-generic, so the explicit
(const char *) casts made it return const char *, which cannot be
assigned to the non-const 'equals' and 'amp' pointers. Upstreamed as
mongodb/mongo-c-driver#2354

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@kevinAlbs
kevinAlbs self-requested a review July 20, 2026 16:39
@kevinAlbs

Copy link
Copy Markdown
Collaborator

Thanks for the PR @jeroen. This file is copied from libmongocrypt. Applied this fix in mongodb/libmongocrypt#1206 and can update the copy to the latest libmongocrypt sources after merging.

@kevinAlbs

Copy link
Copy Markdown
Collaborator

Closing in favor of #2355 which updates the kms-message sources to include mongodb/libmongocrypt#1206. Thanks again @jeroen for bringing this to our attention.

@kevinAlbs kevinAlbs closed this Jul 21, 2026
@jeroen

jeroen commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants