Module Name: src
Committed By: riastradh
Date: Sat Mar 16 00:06:46 UTC 2024
Modified Files:
src/lib/libc/time: strptime.c
Log Message:
strptime(3): Avoid arithmetic overflow.
PR lib/58041
To generate a diff of this commit:
cvs rdiff -u -r1.63 -r1.64 src/lib/libc/time/strptime.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libc/time/strptime.c
diff -u src/lib/libc/time/strptime.c:1.63 src/lib/libc/time/strptime.c:1.64
--- src/lib/libc/time/strptime.c:1.63 Mon Sep 21 15:31:54 2020
+++ src/lib/libc/time/strptime.c Sat Mar 16 00:06:45 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: strptime.c,v 1.63 2020/09/21 15:31:54 ginsbach Exp $ */
+/* $NetBSD: strptime.c,v 1.64 2024/03/16 00:06:45 riastradh Exp $ */
/*-
* Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: strptime.c,v 1.63 2020/09/21 15:31:54 ginsbach Exp $");
+__RCSID("$NetBSD: strptime.c,v 1.64 2024/03/16 00:06:45 riastradh Exp $");
#endif
#include "namespace.h"
@@ -346,30 +346,33 @@ literal:
LEGAL_ALT(ALT_O);
continue;
-#ifndef TIME_MAX
-#define TIME_MAX INT64_MAX
-#endif
case 's': /* seconds since the epoch */
{
- time_t sse = 0;
- uint64_t rulim = TIME_MAX;
+ const time_t TIME_MAX = __type_max(time_t);
+ time_t sse;
+ unsigned d;
if (*bp < '0' || *bp > '9') {
bp = NULL;
continue;
}
- do {
+ sse = *bp++ - '0';
+ while (*bp >= '0' && *bp <= '9') {
+ d = *bp++ - '0';
+ if (sse > TIME_MAX/10) {
+ bp = NULL;
+ break;
+ }
sse *= 10;
- sse += *bp++ - '0';
- rulim /= 10;
- } while ((sse * 10 <= TIME_MAX) &&
- rulim && *bp >= '0' && *bp <= '9');
-
- if (sse < 0 || (uint64_t)sse > TIME_MAX) {
- bp = NULL;
- continue;
+ if (sse > TIME_MAX - d) {
+ bp = NULL;
+ break;
+ }
+ sse += d;
}
+ if (bp == NULL)
+ continue;
if (localtime_r(&sse, tm) == NULL)
bp = NULL;