Skip to content

Fix teststr segfault when built with -ftrivial-auto-var-init#71

Open
dimakuv wants to merge 1 commit intoapache:trunkfrom
dimakuv:dimakuv/fix-teststr-broken-case
Open

Fix teststr segfault when built with -ftrivial-auto-var-init#71
dimakuv wants to merge 1 commit intoapache:trunkfrom
dimakuv:dimakuv/fix-teststr-broken-case

Conversation

@dimakuv
Copy link
Copy Markdown

@dimakuv dimakuv commented Mar 27, 2026

The apr test suite has a test error (segfault) when built with -ftrivial-auto-var-init=zero:

$ ./testall -v -q
testatomic : SUCCESS
testdir : SUCCESS
...
teststr             :  Segmentation fault

Debug stack trace:

(gdb) bt
#0 apr_strtok (str=str@entry=0x0, sep=sep@entry=0x43a429 " \t", last=last@entry=0x7fffffffe2d8) at strings/apr_strtok.c:35
#1 test_strtok (tc=0x7fffffffe380, data=<optimized out>) at test/teststr.c:81

Root cause analysis:

  • NULL, /* but who cares if apr_strtok() segfaults? */
  • retval1 = apr_strtok(str1, cases[curtc].sep, &state);
  • The apr_strtok(str, sep, internal_state) function must not be called with str == NULL in the first invocation. However the test does exactly this, and this leads to an access to *internal_state, which is technically undefined (uninitialized pointer on the stack).
  • Without -ftrivial-auto-var-init=zero, the *internal_state is benign by accident: the previous test case left the pointer-on-stack with some reasonable address. However, with -ftrivial-auto-var-init=zero, the *internal_state access fails because internal_state = NULL (auto-initialized to zero). So the whole test segfaults.

Reproducer

docker run -it amazonlinux:2023 /bin/bash

dnf install -y git vim make gcc14 autoconf libtool expat-devel
export CC=gcc14-gcc
export CFLAGS="$CFLAGS -ftrivial-auto-var-init=zero"

./buildconf
./configure
make -j
make test  # fails

cd test && ./testall -v -q  # to see the failure more clearly

Note that -ftrivial-auto-var-init=zero flag was introduced in GCC v12.

Testing the fix

$ cd test && ./testall -v -q
...
testsockopt         :  SUCCESS
teststr             :  SUCCESS  # <-- works now!
teststrnatcmp       :  SUCCESS
...

* test/teststr.c -- one test case was broken and worked by accident: the
apr_strtok() function was intentionally called with `str == NULL` on
first invocation. This leads to an access to `*internal_state`, which is
technically undefined (uninitialized pointer on the stack).

Without `-ftrivial-auto-var-init`, the `*internal_state` is benign by
accident: the previous test case left the pointer-on-stack with some
reasonable address. However, with `-ftrivial-auto-var-init=zero`, the
`*internal_state` access fails because `internal_state = NULL`
(auto-initialized to zero). So the whole test segfaults.

This commit comments out this broken test case.
@notroj
Copy link
Copy Markdown
Contributor

notroj commented Mar 27, 2026

@dimakuv could you try adding another CI workflow to cover this compilation mode?

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