Skip to content

Capture strchr to a const char - #160

Merged
blairsteven merged 1 commit into
masterfrom
const_char_strchr
Aug 31, 2026
Merged

blairsteven merged 1 commit into
masterfrom
const_char_strchr

Conversation

@sparlane

Copy link
Copy Markdown
Contributor

strchr returns const char *, and we only need to read the returned pointer, so don't try to remove the const.

@blairsteven

Copy link
Copy Markdown
Contributor

You'll need to fix the broken build / test.

gcc 16 is stricter about strchr() on a const string, and these all
failed -Werror=discarded-qualifiers building against it:

  schema.c:1244:9: error: assignment discards 'const' qualifier from pointer target type
   1244 |     key = strchr (path, '/');

lookup_node(), sch_node_find_name(), _sch_path_to_gnode(),
sch_path_to_query() and _sch_json_to_gnode() all scan a "const char
*path"/"name" for a separator and then only ever read the result - to
measure a length, to compare against another position, or to step past
a prefix. Declare those scan pointers const to match.

lookup_node() needed a little more care because "key" was doing two
jobs: first holding the strchr() result, then owning the g_strndup()
copy that is written through and freed. Give the scan its own const
pointer and leave "key" as the owned string.

_sch_query_to_gnode() takes a "char *query" but g_strdup()s it before
doing anything, so it never writes through the caller's pointer. Make
the parameter const and keep the mutable copy in its own local, which
also lets sch_query_to_gnode() drop the (char *) cast it was using to
get around this.

No functional change.

Assisted-by: Claude: claude-opus-5
@blairsteven
blairsteven merged commit 474bfe1 into master Aug 31, 2026
1 check passed
@atlnz-scottp
atlnz-scottp deleted the const_char_strchr branch September 1, 2026 00:36
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