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

Abierto
#11,923 4 comentarios 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 2 días

@Yunlk ya está trabajando en esto.

Desde el 24/10/2026.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
54/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c
Área
backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

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

Lenguaje dominante
C
Estrellas
12.4k
Forks
5.5k
Merge medio
3 d 7 h
PR fusionados (30 d)
79

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de RT-Thread/rt-thread

Todos los issues de RT-Thread/rt-thread

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.