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

Đang mở
#11,923 4 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@Yunlk đang làm issue này rồi.

Từ ngày 24/10/2026.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
54/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c
Lĩnh vực
backend

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

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

Ngôn ngữ chính
C
Star
12.4k
Fork
5.5k
Merge trung bình
3 ngày 7 giờ
Pull request đã merge (30 ngày)
79

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của RT-Thread/rt-thread

Tất cả issue của RT-Thread/rt-thread

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.