[Bug] [libc] gmtime_r 对负时间戳返回非法日历字段

Aperta
#11,923 4 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@Yunlk ci sta già lavorando.

Dal 24/10/2026.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
54/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
backend

Direzione di ricerca

Read components/libc/compilers/common/ctime.c, especially gmtime_r() around lines 166–215. Start by tracing how negative timestamps are split into days and seconds, then compare the results for t = -1 and t = -86400 with the expected UTC fields. Done means supported negative timestamps produce valid calendar fields; the report provides a minimal reproduction, but no target board validation.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

in progress
RT-Thread Version

master, commit a59e72fb5c2f272761a2813fc9bd9fe18eb29d7d.

Affected area

libc, components/libc/compilers/common/ctime.c, gmtime_r().

Hardware / Architecture / Toolchain

不限定具体硬件;要求 time_t 为有符号类型。本报告使用源码级隔离复现,无需 RTC 硬件。该问题与 time_t 是 32 位还是 64 位无关。

问题描述 / Describe the bug

gmtime_r() 对可表示的负时间戳返回非 NULL,但生成非法的日历字段。例如 time_t t = -1 得到 1970-01-01 00:00:-1,而不是 1969-12-31 23:59:59 UTC。

固定版本源码 L166–215

最小复现 / Reproduction

在使用 RT-Thread 此实现的环境中运行(不要用宿主 libc 的同名函数代替):

#include <time.h>
#include <stdio.h>

void reproduce(void)
{
    time_t t = (time_t)-1;
    struct tm r;
    if (gmtime_r(&t, &r) != NULL)
    {
        printf("%d-%02d-%02d %02d:%02d:%02d\n",
               r.tm_year + 1900, r.tm_mon + 1, r.tm_mday,
               r.tm_hour, r.tm_min, r.tm_sec);
    }
}

预期:1969-12-31 23:59:59。

实际:1970-01-01 00:00:-1。

另一个反例:t = -86400,应为 1969-12-31 00:00:00,却得到 1970-01-00 00:00:00,其中 tm_yday=-1。

验证方式:此前已提取 RT-Thread 原始 gmtime_r()、月份表和闰年辅助代码,在隔离的 signed time32/time64 模型中运行;仅替换 RT-Thread 的内存和 errno 接口,得到上述结果。未进行目标板验证。

Cause / Suggested direction
work = *timep % (24*60*60);
r->tm_sec = work % 60;
/* ... */
work = (int)(*timep / (24*60*60));

C 的负数除法向零截断、负余数没有规范化;后续年份循环又只从 1970 向未来遍历,无法处理负天数。

建议正确规范化负的秒内余数和天数,并支持对应的 Epoch 前日期,而不是成功返回包含负秒、零日期等非法字段的 struct tm。这不是要求直接拒绝所有负时间,也不依赖 Y2038 溢出才能触发。

最早可追溯实现和版本 / History
  • 现行组件实现的算法可追溯至 3bf68f2f42,2018-10-26,[Libc][dlib][armlibc] add gmtime_r api;首个包含该提交的正式 release 为 v3.1.1。后来搬至 common 层,不能将搬运提交误认为 bug 首次引入。
  • 更早的 libc/minilibc/time.c 中已有同类负时间算法,可追溯至 9a7ae65085,2009-10-20。历史 tag 中至少 v1.2.0 已包含该旧实现。这是“最早可追溯代码 / 最早已确认 tag”,不声称 v1.2.0 是整个项目历史上首次受影响的发布版;旧 minilibc 后来已删除。
其他实现参考 / Comparison

这些链接用于说明处理思路和预期行为,不是建议未经许可证及范围检查就直接复制实现。

Lingua principale
C
Stelle
12.4k
Fork
5.5k
Merge medio
3g 7h
PR unite (30g)
79

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di RT-Thread/rt-thread

Tutte le issue di RT-Thread/rt-thread

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.