openresty / openresty/echo-nginx-module

$echo_timer_elapsed is corrupted on 32-bit platforms with 64-bit time_t

Open Beginner friendly
#127 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
1.2k
Forks
251
Avg merge
1h 24m
Merged PRs (30d)
2

Description

Hi,

$echo_timer_elapsed produces corrupted values on 32-bit platforms where time_t is 64-bit, such as armhf systems.

Example output:

elapsed 17592186044416.611 sec.
elapsed 93427976913289250.458 sec.

In some cases the result can also be negative:

-8549631993283870719.457

The problem is reproducible with echo-nginx-module 0.65 and nginx 1.30.4 on armhf. The relevant test failures can be seen in this CI log:

https://ci.debian.net/data/autopkgtest/testing/armhf/libn/libnginx-mod-http-echo/74055259/log.gz

The issue appears to be a varargs type mismatch in src/ngx_http_echo_timer.c:

v->len = ngx_snprintf(p, size, "%T.%03M",
ms / 1000, ms % 1000) - p;

The nginx-specific %T format specifier expects a time_t, while ms / 1000 has type ngx_msec_int_t. On a 32-bit platform with a 64-bit time_t, ngx_snprintf() therefore reads an argument with the wrong width,
resulting in undefined behaviour and corrupted output.

Explicitly converting both arguments to the types required by the format specifiers fixes the issue:

--- a/src/ngx_http_echo_timer.c
+++ b/src/ngx_http_echo_timer.c
@@ -76,7 +76,8 @@ ngx_http_echo_timer_elapsed_variable(ngx_http_request_t *r,
         return NGX_ERROR;
     }

-    v->len = ngx_snprintf(p, size, "%T.%03M", ms / 1000, ms % 1000) - p;
+    v->len = ngx_snprintf(p, size, "%T.%03M", (time_t) (ms / 1000),
+                          (ngx_msec_t) (ms % 1000)) - p;
     v->data = p;

     v->valid = 1;

Jan

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/ngx_http_echo_timer.c at ngx_http_echo_timer_elapsed_variable and review the ngx_snprintf call that formats $echo_timer_elapsed. Reproduce or inspect the armhf CI failure linked in the issue, then verify that the elapsed value is formatted correctly on 32-bit platforms with 64-bit time_t.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, nginx
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.