Skip to content

fix(cli): const-qualify local pointers in codex_hook_strip for gcc 15#588

Merged
DeusData merged 1 commit into
DeusData:mainfrom
GothUncc:fix/gcc15-strstr-const-qualifier
Jun 23, 2026
Merged

fix(cli): const-qualify local pointers in codex_hook_strip for gcc 15#588
DeusData merged 1 commit into
DeusData:mainfrom
GothUncc:fix/gcc15-strstr-const-qualifier

Conversation

@GothUncc

Copy link
Copy Markdown
Contributor

Problem

Building from source with gcc 15 (Ubuntu 25.x ships 15.2) fails:

src/cli/cli.c:1503:19: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
 1503 |     char *begin = strstr(content, CODEX_HOOK_BEGIN);
      |                   ^~~~~~
cc1: all warnings being treated as errors

This blocks scripts/build.sh on modern toolchains. Every other source file (including all 158 vendored grammars) compiles cleanly — this is the only blocker.

Cause

Makefile.cbm builds with -D_GNU_SOURCE -O2, which enables glibc's optimized string.h macros. Under those, strstr() called on a const char * (with a constant needle) returns a const char *. Assigning that to a char * trips -Werror=discarded-qualifiers on gcc 15.

Minimal reproducer (gcc 15.2):

#include <string.h>
int main(void) {
    const char *c = "hello";
    char *p = strstr(c, "ell");   /* errors under: gcc -D_GNU_SOURCE -O2 -Werror=discarded-qualifiers */
    return p ? 0 : 1;
}

(Compiles clean without -D_GNU_SOURCE -O2, which is why it only surfaces in the real build.)

Fix

codex_hook_strip() only ever reads through begin, end, and cut (offset arithmetic and byte comparisons — never writes through them), so const-qualifying the three locals is the correct fix. It also matches the function's existing const contract: content is already declared const char *. The malloc'd output buffer (out) stays char *.

No behavior change.

Testing

  • Reproduced the failure on gcc 15.2; confirmed the patch resolves it.
  • Full scripts/build.sh completes cleanly with the patch.
  • Did not run scripts/lint.sh locally (no clang in my environment); the change conforms to surrounding style and builds under -Wall -Wextra -Werror.

Under -D_GNU_SOURCE with optimization, glibc's string.h macros make
strstr() on a `const char *` (with a constant needle) yield a
`const char *`. gcc 15 then rejects assigning that to `char *` with
-Werror=discarded-qualifiers, breaking the build:

    src/cli/cli.c:1503: error: initialization discards 'const'
    qualifier from pointer target type [-Werror=discarded-qualifiers]

Minimal repro (gcc 15.2):
    const char *c = "hello";
    char *p = strstr(c, "ell");   // gcc -D_GNU_SOURCE -O2 -Werror=discarded-qualifiers

codex_hook_strip only reads through begin/end/cut (offset math and
byte comparisons, never writes), so making them `const char *` is the
correct fix and preserves the function's existing const contract
(`content` is already const). The malloc'd output buffer stays char *.
No behavior change; build succeeds on gcc 15.2.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Jeremy Maserang <gothuncc@gothaunty.com>
@DeusData
DeusData merged commit ca5c0d2 into DeusData:main Jun 23, 2026
13 checks passed
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