Skip to content

Fix "time_t' different typedef on 32-bit /64-bit systems - #3787

Merged
enjoy-binbin merged 4 commits into
valkey-io:unstablefrom
chenshi5012:fix-32bit-crash
May 22, 2026
Merged

enjoy-binbin merged 4 commits into
valkey-io:unstablefrom
chenshi5012:fix-32bit-crash

Conversation

@chenshi5012

@chenshi5012 chenshi5012 commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Fix #3781

Description

This PR fixes a crash on 32-bit Alpine 3.23 / time64 environments when starting valkey-server and generating the INFO server output.

On 32-bit Alpine 3.23, time_t is 64-bit. In genValkeyInfoString(), the lru_clock field is formatted with %u, but the expression passed to sdscatfmt() was derived from server.unixtime, whose type is time_t. This can pass a 64-bit varargs argument to a formatter expecting an unsigned int-sized value. As a result, later varargs arguments may be read at the wrong offset, which can lead to a segmentation fault during startup.

The fix explicitly casts the computed lru_clock value to unsigned int before passing it to sdscatfmt(), matching the %u format specifier and preserving the intended 24-bit LRU clo

The Alpine 32-bit build fails with -Werror=format because a few time_t values are printed with %ld. This assumes that time_t is always compatible with long, which is not portable across all 32-bit and 64-bit targets. In particular, 32-bit Linux environments may use a time_t representation that does not match long, causing format checking failures when warnings are treated as errors.

Root Cause

The crash is caused by a varargs width mismatch on 32-bit time64 systems:

"lru_clock:%u\r\n",
(server.unixtime / LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX,

On 32-bit Alpine 3.23, server.unixtime is backed by 64-bit time_t, so the expression may be passed as a 64-bit value. Since %u expects an unsigned int, sdscatfmt() consumes only part of the argument and subsequent arguments can become misaligned.

Fix

Cast the value passed for %u to unsigned int:
server.c

(unsigned int)((server.unixtime / LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX)

updates the affected time_t formatting sites to use %jd together with an explicit (intmax_t) cast. This keeps the output portable across architectures without changing the time_t ABI or making assumptions about its underlying typedef.

File Change
src/valkey-check-aof.c Print to_timestamp with %jd and (intmax_t)to_timestamp in all AOF truncate-to-timestamp messages.
src/fuzzer_command_generator.c Print generated ARG_TYPE_UNIX_TIME values with %jd and (intmax_t)currentTime.

Test

This PR also added a new daily CI "test-alpine-32bit"

@coderabbitai

coderabbitai Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Two changes: (1) cast the masked server.unixtime to unsigned int when printing lru_clock in INFO output; (2) add a test-alpine-32bit GitHub Actions job that builds a 32-bit binary in an i386 Alpine container, runs smoke tests, and is included in consolidation/notification jobs.

Changes

Integer formatting and timestamp diagnostics

Layer / File(s) Summary
LRU clock cast for unsigned formatting
src/server.c
The masked server.unixtime value is explicitly cast to unsigned int to match the %u printf format specifier in the INFO Server lru_clock output.
Fuzzer unix-time argument formatting
src/fuzzer_command_generator.c
Adds <stdint.h> and changes Unix-time argument printing to use %jd with an (intmax_t) cast.
AOF timestamp diagnostic formatting
src/valkey-check-aof.c
Adds <stdint.h> and updates AOF truncation and success messages to format to_timestamp with %jd and intmax_t casts, and adjusts related diagnostic text/offset formatting.

Alpine 32-bit CI

Layer / File(s) Summary
test-alpine-32bit job definition and behavior
.github/workflows/daily.yml
Adds test-alpine-32bit job that builds 32-bit binaries inside an i386 Alpine container, installs libbacktrace, runs the 32-bit build and test suites, and uploads failures.
Add job to consolidation & notification
.github/workflows/daily.yml
Adds test-alpine-32bit as a dependency to consolidate-test-failures and notify-about-job-results so its result is included in downstream consolidation and notifications.

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR addresses the primary coding requirement from issue #3781 by fixing the crash on 32-bit Alpine systems. It also adds CI coverage via the new test-alpine-32bit workflow job to prevent regressions.
Out of Scope Changes check ✅ Passed Beyond the primary lru_clock fix, the PR proactively addresses broader time_t formatting issues across multiple files (fuzzer_command_generator.c, valkey-check-aof.c) using intmax_t/printf(%jd) for portability, which aligns with the underlying concern about time_t compatibility.
Title check ✅ Passed The PR title accurately describes the main change: fixing time_t typedef differences between 32-bit and 64-bit systems, which is the core issue being addressed across the modified files.
Description check ✅ Passed The pull request description clearly explains the crash, root cause, fixes applied to multiple files, and the CI test coverage added.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented May 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.66%. Comparing base (8274099) to head (da43eee).
⚠️ Report is 5 commits behind head on unstable.

Files with missing lines Patch % Lines
src/valkey-check-aof.c 60.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #3787      +/-   ##
============================================
- Coverage     76.70%   76.66%   -0.05%     
============================================
  Files           162      162              
  Lines         80697    80710      +13     
============================================
- Hits          61900    61875      -25     
- Misses        18797    18835      +38     
Files with missing lines Coverage Δ
src/fuzzer_command_generator.c 76.82% <100.00%> (+0.10%) ⬆️
src/server.c 89.49% <ø> (-0.03%) ⬇️
src/valkey-check-aof.c 73.80% <60.00%> (ø)

... and 19 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@enjoy-binbin enjoy-binbin added the run-extra-tests Run extra tests on this PR (Runs all tests from daily except valgrind and RESP) label May 20, 2026
@enjoy-binbin enjoy-binbin changed the title [server] Fix INFO lru_clock formatting on 32-bit time64 systems Fix INFO lru_clock formatting on 32-bit time64 systems May 20, 2026

@roshkhatri roshkhatri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a daily test that runs on alpine 32 bit so we dont run into this issue again?

@chenshi5012 chenshi5012 changed the title Fix INFO lru_clock formatting on 32-bit time64 systems [wip]Fix INFO lru_clock formatting on 32-bit time64 systems May 21, 2026
@chenshi5012

Copy link
Copy Markdown
Contributor Author

@enjoy-binbin @roshkhatri

I found a relatively significant issue related to time_t.

The underlying definition of time_t is implementation-dependent and may vary across different libc implementations, ABIs, and architectures, mainly due to the Y2038 transition.

For example, existing code may assume:

time_t to_timestamp;
printf("%ld", to_timestamp);

This works when time_t is defined as:

typedef long time_t;

which is traditionally true on i386 glibc.

However, when enabling:

#define _TIME_BITS 64

time_t may become:

typedef long long time_t;

On musl libc, many 32-bit architectures already use a 64-bit time_t by default.

In such cases, using %ld becomes incorrect and may lead to undefined behavior.

Using %lld is still not fully portable either, since the actual underlying type of time_t is platform-dependent.

A safer portable approach would be something like:

printf("%jd", (intmax_t)to_timestamp);

This potentially has a fairly broad impact, especially on formatting, ABI compatibility, struct layouts, serialization, and cross-platform behavior, so I think we should discuss a proper compatibility/fix strategy.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.github/workflows/daily.yml:
- Line 1955: The skip-token used by the job test-alpine-32bit is mismatched: the
job checks for 'alpine32' in contains(github.event.inputs.skipjobs, 'alpine32')
but the workflow input defaults still expose 'alpine' and '32bit', causing
unexpected runs; fix by making the tokens consistent — either change the job
condition to check both 'alpine' and '32bit' (e.g., require both tokens or a
combined check) or update the workflow input default list to include the single
token 'alpine32' so contains(github.event.inputs.skipjobs, 'alpine32') behaves
as intended.
- Line 1987: The run lines dangerously interpolate user inputs unquoted
(test_args / cluster_test_args) into shell commands (runtest, runtest-moduleapi,
runtest-sentinel); change them to avoid shell injection by treating inputs as
data: set an env var using toJSON (e.g. TEST_ARGS: ${{
toJSON(github.event.inputs.test_args) }}) and in the run script parse that JSON
into a safe array (or read it as a single quoted argument) and then invoke the
commands with the array/quoted variable (e.g. ./runtest "${ARGS[@]}" or
./runtest "$TEST_ARGS") so inputs are properly escaped rather than interpolated
raw.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2cd0644c-784d-4855-8d77-c56c1a13f339

📥 Commits

Reviewing files that changed from the base of the PR and between a4b71c9 and 6dc7c52.

📒 Files selected for processing (3)
  • .github/workflows/daily.yml
  • src/fuzzer_command_generator.c
  • src/valkey-check-aof.c
✅ Files skipped from review due to trivial changes (1)
  • src/fuzzer_command_generator.c

Comment thread .github/workflows/daily.yml Resolved
run: apk add tcl procps tclx
- name: test
if: true && !contains(github.event.inputs.skiptests, 'valkey')
run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --failures-output test-failures/valkey.json --verbose --dump-logs ${{github.event.inputs.test_args}}

@coderabbitai coderabbitai Bot May 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
ls -la .github/workflows || true
sed -n '1950,2025p' .github/workflows/daily.yml

Repository: valkey-io/valkey

Length of output: 5743


🏁 Script executed:

#!/bin/bash
set -euo pipefail
# find where test_args and cluster_test_args are defined in daily.yml
rg -n "test_args|cluster_test_args" .github/workflows/daily.yml

Repository: valkey-io/valkey

Length of output: 17049


🏁 Script executed:

#!/bin/bash
set -euo pipefail
sed -n '1,220p' .github/workflows/daily.yml

Repository: valkey-io/valkey

Length of output: 10563


🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -n "^(on:|workflow_dispatch:|workflow_call:|inputs:)" .github/workflows/daily.yml

Repository: valkey-io/valkey

Length of output: 66


🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -n "workflow_dispatch:|workflow_call:" .github/workflows/daily.yml

Repository: valkey-io/valkey

Length of output: 104


🏁 Script executed:

#!/bin/bash
set -euo pipefail
# Show around workflow_dispatch/workflow_call blocks (guessed ranges)
# Extract first 400 lines around top for inputs
sed -n '220,520p' .github/workflows/daily.yml

Repository: valkey-io/valkey

Length of output: 16772


🏁 Script executed:

#!/bin/bash
set -euo pipefail
sed -n '1975,2010p' .github/workflows/daily.yml

Repository: valkey-io/valkey

Length of output: 2088


Harden run: commands against workflow input shell injection in .github/workflows/daily.yml

test_args and cluster_test_args are user-controllable workflow inputs (workflow_dispatch.inputs.* / workflow_call.inputs.*) but are interpolated unquoted into shell commands (./runtest, ./runtest-moduleapi, ./runtest-sentinel), allowing crafted values with shell metacharacters to alter command execution.

Suggested hardening pattern
       - name: test
         if: true && !contains(github.event.inputs.skiptests, 'valkey')
-        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --failures-output test-failures/valkey.json --verbose --dump-logs ${{github.event.inputs.test_args}}
+        env:
+          EXTRA_TEST_ARGS: ${{ inputs.test_args || github.event.inputs.test_args || '' }}
+        run: |
+          read -r -a extra_args <<< "$EXTRA_TEST_ARGS"
+          ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} \
+            --failures-output test-failures/valkey.json --verbose --dump-logs \
+            "${extra_args[@]}"

       - name: module api test
         if: true && !contains(github.event.inputs.skiptests, 'modules')
         run: |
           make -C tests/modules 32bit # the script below doesn't have an argument, we must build manually ahead of time
-          CFLAGS='-Werror' ./runtest-moduleapi --failures-output test-failures/moduleapi.json --verbose --dump-logs ${{github.event.inputs.test_args}}
+          read -r -a extra_args <<< "${{ inputs.test_args || github.event.inputs.test_args || '' }}"
+          CFLAGS='-Werror' ./runtest-moduleapi --failures-output test-failures/moduleapi.json --verbose --dump-logs "${extra_args[@]}"

       - name: sentinel tests
         if: true && !contains(github.event.inputs.skiptests, 'sentinel')
-        run: ./runtest-sentinel --failures-output test-failures/sentinel.json ${{github.event.inputs.cluster_test_args}}
+        env:
+          EXTRA_CLUSTER_TEST_ARGS: ${{ inputs.cluster_test_args || github.event.inputs.cluster_test_args || '' }}
+        run: |
+          read -r -a cluster_args <<< "$EXTRA_CLUSTER_TEST_ARGS"
+          ./runtest-sentinel --failures-output test-failures/sentinel.json \
+            "${cluster_args[@]}"
🧰 Tools
🪛 zizmor (1.25.2)

[error] 1987-1987: code injection via template expansion (template-injection): may expand into attacker-controllable code

(template-injection)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/daily.yml at line 1987, The run lines dangerously
interpolate user inputs unquoted (test_args / cluster_test_args) into shell
commands (runtest, runtest-moduleapi, runtest-sentinel); change them to avoid
shell injection by treating inputs as data: set an env var using toJSON (e.g.
TEST_ARGS: ${{ toJSON(github.event.inputs.test_args) }}) and in the run script
parse that JSON into a safe array (or read it as a single quoted argument) and
then invoke the commands with the array/quoted variable (e.g. ./runtest
"${ARGS[@]}" or ./runtest "$TEST_ARGS") so inputs are properly escaped rather
than interpolated raw.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roshkhatri, understood — the shell injection pattern exists across all jobs in the workflow file and predates this PR, so it's indeed out of scope here. Would you like me to open a follow-up issue to track hardening the test_args / cluster_test_args interpolation across daily.yml?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

The fix explicitly casts the computed lru_clock value to unsigned int before passing it to sdscatfmt(), matching the %u format specifier and preserving the intended 24-bit LRU clock value.

The Alpine 32-bit build fails with -Werror=format because a few time_t values are printed with %ld. This assumes that time_t is always compatible with long, which is not portable across all 32-bit and 64-bit targets. In particular, 32-bit Linux environments may use a time_t representation that does not match long, causing format checking failures when warnings are treated as errors.

Signed-off-by: chenshi <[email protected]>
@chenshi5012 chenshi5012 changed the title [wip]Fix INFO lru_clock formatting on 32-bit time64 systems [wip]Fix "time_t' different typedef on 32-bit /64-bit systems May 21, 2026
@chenshi5012

Copy link
Copy Markdown
Contributor Author

@roshkhatri
The daily test cases have wasted a lot of my time, not good at it

  1. The test script uses docker to pull an image of version 3.23; is it necessary to modify it to the latest version
  2. Currently, executing a use case takes approximately 30 to 40 minutes. Is there something wrong with the way I wrote it? Is there a more efficient way?

Assistance from official personnel is required !!!

@roshkhatri roshkhatri left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me.

The test script uses docker to pull an image of version 3.23; is it necessary to modify it to the latest version

Yes we should change to latest for alpine.

Currently, executing a use case takes approximately 30 to 40 minutes. Is there something wrong with the way I wrote it? Is there a more efficient way?

They way it is written is correct, github runners container: dont have a field of -platform so this is correct, However, I think we should keep this test just as a smoke test for the crash we saw in try-valkey.
But it is also file to keep it as is, cuz it took 31 mins, which is quite similar to other jobs in the WF
https://github.com/valkey-io/valkey/actions/runs/26227227813/job/77177403146?pr=3787

run: apk add tcl procps tclx
- name: test
if: true && !contains(github.event.inputs.skiptests, 'valkey')
run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --failures-output test-failures/valkey.json --verbose --dump-logs ${{github.event.inputs.test_args}}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR

Comment thread .github/workflows/daily.yml Outdated
-e SKIPTESTS \
-e TEST_ARGS \
-e CLUSTER_TEST_ARGS \
i386/alpine:3.23 \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change this to latest as, in this file we are also testing against alpine:latest for the other test.

CLUSTER_TEST_ARGS: ${{ github.event.inputs.cluster_test_args }}
run: |
docker run --rm \
--platform linux/386 \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It takes times to build under emulation, so 30-40 mins should be okay

@roshkhatri roshkhatri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can change the alpine to latest and merge it.

@chenshi5012 chenshi5012 changed the title [wip]Fix "time_t' different typedef on 32-bit /64-bit systems Fix "time_t' different typedef on 32-bit /64-bit systems May 22, 2026
@chenshi5012

Copy link
Copy Markdown
Contributor Author

I think we can change the alpine to latest and merge it.

done.

@chenshi5012
chenshi5012 requested a review from enjoy-binbin May 22, 2026 02:59

@enjoy-binbin enjoy-binbin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, i see the CI is also green now, it is good to go

@enjoy-binbin
enjoy-binbin merged commit 8d3e6c3 into valkey-io:unstable May 22, 2026
71 of 72 checks passed
@chenshi5012

Copy link
Copy Markdown
Contributor Author

@enjoy-binbin @roshkhatri

I found a relatively significant issue related to time_t.

The underlying definition of time_t is implementation-dependent and may vary across different libc implementations, ABIs, and architectures, mainly due to the Y2038 transition.

For example, existing code may assume:

time_t to_timestamp; printf("%ld", to_timestamp);

This works when time_t is defined as:

typedef long time_t;

which is traditionally true on i386 glibc.

However, when enabling:

#define _TIME_BITS 64

time_t may become:

typedef long long time_t;

On musl libc, many 32-bit architectures already use a 64-bit time_t by default.

In such cases, using %ld becomes incorrect and may lead to undefined behavior.

Using %lld is still not fully portable either, since the actual underlying type of time_t is platform-dependent.

A safer portable approach would be something like:

printf("%jd", (intmax_t)to_timestamp);

This potentially has a fairly broad impact, especially on formatting, ABI compatibility, struct layouts, serialization, and cross-platform behavior, so I think we should discuss a proper compatibility/fix strategy.

@enjoy-binbin Please consider the issue of Y2038 from a global perspective. Thank you, In the future, our product will switch from Redis to Valkey, and the requirements for stability are quite high. thanks.

@enjoy-binbin

Copy link
Copy Markdown
Member

Using intmax_t is SGTM, we are indeed already using it in some places. Since this PR is merged, could you file a issue and make a proposal?

@chenshi5012
chenshi5012 deleted the fix-32bit-crash branch May 23, 2026 00:40
alon-arenberg pushed a commit to alon-arenberg/valkey that referenced this pull request Jul 15, 2026
)

Fix valkey-io#3781

## Description
This PR fixes a crash on 32-bit Alpine 3.23 / time64 environments when
starting `valkey-server` and generating the `INFO server` output.

On 32-bit Alpine 3.23, `time_t` is 64-bit. In `genValkeyInfoString()`,
the `lru_clock` field is formatted with `%u`, but the expression passed
to `sdscatfmt()` was derived from `server.unixtime`, whose type is
`time_t`. This can pass a 64-bit varargs argument to a formatter
expecting an unsigned int-sized value. As a result, later varargs
arguments may be read at the wrong offset, which can lead to a
segmentation fault during startup.

The fix explicitly casts the computed `lru_clock` value to `unsigned
int` before passing it to `sdscatfmt()`, matching the `%u` format
specifier and preserving the intended 24-bit LRU clo

The Alpine 32-bit build fails with `-Werror=format` because a few
`time_t` values are printed with `%ld`. This assumes that `time_t` is
always compatible with `long`, which is not portable across all 32-bit
and 64-bit targets. In particular, 32-bit Linux environments may use a
`time_t` representation that does not match `long`, causing format
checking failures when warnings are treated as errors.

## Root Cause

The crash is caused by a varargs width mismatch on 32-bit time64
systems:

```c
"lru_clock:%u\r\n",
(server.unixtime / LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX,
```

On 32-bit Alpine 3.23, server.unixtime is backed by 64-bit time_t, so
the expression may be passed as a 64-bit value. Since %u expects an
unsigned int, sdscatfmt() consumes only part of the argument and
subsequent arguments can become misaligned.

# Fix
Cast the value passed for %u to unsigned int:
server.c
```c
(unsigned int)((server.unixtime / LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX)
```

updates the affected `time_t` formatting sites to use `%jd` together
with an explicit `(intmax_t)` cast. This keeps the output portable
across architectures without changing the `time_t` ABI or making
assumptions about its underlying typedef.

# Test

This PR also added a new daily CI "test-alpine-32bit"

Signed-off-by: chenshi <[email protected]>
Signed-off-by: Alon Arenberg <[email protected]>
sarthakaggarwal97 pushed a commit that referenced this pull request Jul 16, 2026
Fix #3781 

## Description
This PR fixes a crash on 32-bit Alpine 3.23 / time64 environments when
starting `valkey-server` and generating the `INFO server` output.

On 32-bit Alpine 3.23, `time_t` is 64-bit. In `genValkeyInfoString()`,
the `lru_clock` field is formatted with `%u`, but the expression passed
to `sdscatfmt()` was derived from `server.unixtime`, whose type is
`time_t`. This can pass a 64-bit varargs argument to a formatter
expecting an unsigned int-sized value. As a result, later varargs
arguments may be read at the wrong offset, which can lead to a
segmentation fault during startup.

The fix explicitly casts the computed `lru_clock` value to `unsigned
int` before passing it to `sdscatfmt()`, matching the `%u` format
specifier and preserving the intended 24-bit LRU clo

The Alpine 32-bit build fails with `-Werror=format` because a few
`time_t` values are printed with `%ld`. This assumes that `time_t` is
always compatible with `long`, which is not portable across all 32-bit
and 64-bit targets. In particular, 32-bit Linux environments may use a
`time_t` representation that does not match `long`, causing format
checking failures when warnings are treated as errors.

## Root Cause

The crash is caused by a varargs width mismatch on 32-bit time64
systems:

```c
"lru_clock:%u\r\n",
(server.unixtime / LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX,
```

On 32-bit Alpine 3.23, server.unixtime is backed by 64-bit time_t, so
the expression may be passed as a 64-bit value. Since %u expects an
unsigned int, sdscatfmt() consumes only part of the argument and
subsequent arguments can become misaligned.

# Fix
Cast the value passed for %u to unsigned int:
server.c
```c
(unsigned int)((server.unixtime / LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX)
```

updates the affected `time_t` formatting sites to use `%jd` together
with an explicit `(intmax_t)` cast. This keeps the output portable
across architectures without changing the `time_t` ABI or making
assumptions about its underlying typedef.

# Test 

This PR also added a new daily CI "test-alpine-32bit"

Signed-off-by: chenshi <[email protected]>
@valkeyrie-ops valkeyrie-ops Bot moved this from To be backported to Done in Valkey 9.1 Jul 16, 2026
sarthakaggarwal97 pushed a commit to sarthakaggarwal97/valkey that referenced this pull request Jul 16, 2026
Use intmax_t and %jd for time_t values so 32-bit time64 builds do not fail under -Werror=format.

(cherry picked from commit 8d3e6c3)

Signed-off-by: chenshi <[email protected]>

Signed-off-by: Sarthak Aggarwal <[email protected]>
sarthakaggarwal97 pushed a commit to sarthakaggarwal97/valkey that referenced this pull request Jul 16, 2026
Use intmax_t and %jd for time_t values so 32-bit time64 builds do not fail under -Werror=format.

(cherry picked from commit 8d3e6c3)

Signed-off-by: chenshi <[email protected]>

Signed-off-by: Sarthak Aggarwal <[email protected]>
sarthakaggarwal97 pushed a commit to sarthakaggarwal97/valkey that referenced this pull request Jul 16, 2026
Use intmax_t and %jd for time_t values so 32-bit time64 builds do not fail under -Werror=format.

(cherry picked from commit 8d3e6c3)

Signed-off-by: chenshi <[email protected]>

Signed-off-by: Sarthak Aggarwal <[email protected]>
sarthakaggarwal97 pushed a commit to sarthakaggarwal97/valkey that referenced this pull request Jul 16, 2026
Backport to 9.0: valkey-check-aof.c %jd fix and an adapted test-alpine-32bit CI job. server.c/fuzzer changes are not applicable to 9.0.

(cherry picked from commit 8d3e6c3)
Signed-off-by: chenshi <[email protected]>
Signed-off-by: Roshan Khatri <[email protected]>
Signed-off-by: Sarthak Aggarwal <[email protected]>
sarthakaggarwal97 pushed a commit that referenced this pull request Jul 17, 2026
Use intmax_t and %jd for time_t values so 32-bit time64 builds do not fail under -Werror=format.

(cherry picked from commit 8d3e6c3)

Signed-off-by: chenshi <[email protected]>

Signed-off-by: Sarthak Aggarwal <[email protected]>
sarthakaggarwal97 pushed a commit that referenced this pull request Jul 17, 2026
Use intmax_t and %jd for time_t values so 32-bit time64 builds do not fail under -Werror=format.

(cherry picked from commit 8d3e6c3)

Signed-off-by: chenshi <[email protected]>

Signed-off-by: Sarthak Aggarwal <[email protected]>
sarthakaggarwal97 pushed a commit that referenced this pull request Jul 17, 2026
Use intmax_t and %jd for time_t values so 32-bit time64 builds do not fail under -Werror=format.

(cherry picked from commit 8d3e6c3)

Signed-off-by: chenshi <[email protected]>

Signed-off-by: Sarthak Aggarwal <[email protected]>
sarthakaggarwal97 pushed a commit that referenced this pull request Jul 17, 2026
Backport to 9.0: valkey-check-aof.c %jd fix and an adapted test-alpine-32bit CI job. server.c/fuzzer changes are not applicable to 9.0.

(cherry picked from commit 8d3e6c3)
Signed-off-by: chenshi <[email protected]>
Signed-off-by: Roshan Khatri <[email protected]>
Signed-off-by: Sarthak Aggarwal <[email protected]>
alon-arenberg pushed a commit to alon-arenberg/valkey that referenced this pull request Jul 30, 2026
)

Fix valkey-io#3781

## Description
This PR fixes a crash on 32-bit Alpine 3.23 / time64 environments when
starting `valkey-server` and generating the `INFO server` output.

On 32-bit Alpine 3.23, `time_t` is 64-bit. In `genValkeyInfoString()`,
the `lru_clock` field is formatted with `%u`, but the expression passed
to `sdscatfmt()` was derived from `server.unixtime`, whose type is
`time_t`. This can pass a 64-bit varargs argument to a formatter
expecting an unsigned int-sized value. As a result, later varargs
arguments may be read at the wrong offset, which can lead to a
segmentation fault during startup.

The fix explicitly casts the computed `lru_clock` value to `unsigned
int` before passing it to `sdscatfmt()`, matching the `%u` format
specifier and preserving the intended 24-bit LRU clo

The Alpine 32-bit build fails with `-Werror=format` because a few
`time_t` values are printed with `%ld`. This assumes that `time_t` is
always compatible with `long`, which is not portable across all 32-bit
and 64-bit targets. In particular, 32-bit Linux environments may use a
`time_t` representation that does not match `long`, causing format
checking failures when warnings are treated as errors.

## Root Cause

The crash is caused by a varargs width mismatch on 32-bit time64
systems:

```c
"lru_clock:%u\r\n",
(server.unixtime / LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX,
```

On 32-bit Alpine 3.23, server.unixtime is backed by 64-bit time_t, so
the expression may be passed as a 64-bit value. Since %u expects an
unsigned int, sdscatfmt() consumes only part of the argument and
subsequent arguments can become misaligned.

# Fix
Cast the value passed for %u to unsigned int:
server.c
```c
(unsigned int)((server.unixtime / LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX)
```

updates the affected `time_t` formatting sites to use `%jd` together
with an explicit `(intmax_t)` cast. This keeps the output portable
across architectures without changing the `time_t` ABI or making
assumptions about its underlying typedef.

# Test

This PR also added a new daily CI "test-alpine-32bit"

Signed-off-by: chenshi <[email protected]>
Signed-off-by: Alon Arenberg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-extra-tests Run extra tests on this PR (Runs all tests from daily except valgrind and RESP)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG]Valkey server crashing on the 32 bit alpine emulator

3 participants