Commit a2ad4447c8 for ffmpeg
commit a2ad4447c8253f308dafdfa812b3cd519dcdbbfd
Author: Michael Niedermayer <michael@niedermayer.cc>
Date: Wed Nov 11 04:32:57 2026 +0100
avutil/parseutils: saturate av_timegm() to the range of time_t
With a 32 bit time_t a date after 2038 wrapped around, so a cookie
expiring in 2100 was treated as expired.
Fixes: av_timegm() overflow with a 32 bit time_t
diff --git a/libavutil/parseutils.c b/libavutil/parseutils.c
index 9f57b16ec2..acdc148c58 100644
--- a/libavutil/parseutils.c
+++ b/libavutil/parseutils.c
@@ -572,7 +572,7 @@ char *av_small_strptime(const char *p, const char *fmt, struct tm *dt)
time_t av_timegm(struct tm *tm)
{
- time_t t;
+ int64_t t;
int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
@@ -586,6 +586,12 @@ time_t av_timegm(struct tm *tm)
t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
+ // saturate if time_t cannot hold the result
+ if ((time_t)t != t) {
+ const time_t max = (time_t)-1 < 0 ? (time_t)(INT64_MAX >> (64 - 8 * sizeof(time_t))) : (time_t)-1;
+ return t > 0 ? max : (time_t)-1 < 0 ? -max - 1 : 0;
+ }
+
return t;
}
diff --git a/libavutil/parseutils.h b/libavutil/parseutils.h
index dad5c2775b..9d5ea2638a 100644
--- a/libavutil/parseutils.h
+++ b/libavutil/parseutils.h
@@ -190,7 +190,8 @@ int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info
char *av_small_strptime(const char *p, const char *fmt, struct tm *dt);
/**
- * Convert the decomposed UTC time in tm to a time_t value.
+ * Convert the decomposed UTC time in tm to a time_t value, saturated to the
+ * range of time_t.
*/
time_t av_timegm(struct tm *tm);