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

Offen
#11,923 4 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

@Yunlk arbeitet bereits daran.

Seit 24.10.2026.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
54/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
c
Bereich
backend

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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

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

Vorherrschende Sprache
C
Sterne
12.4k
Forks
5.5k
Ø Merge
3 T. 7 Std.
Gemergte PRs (30 T.)
79

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus RT-Thread/rt-thread

Alle Issues in RT-Thread/rt-thread

Ähnliche Issues

Weitere Issues zu C

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.