strptime.c: fix 32-bit range check in epoch_to_tm().

bionic/libc/tzcode/strptime.c:639:11: warning: result of comparison of constant 9223372036854775807 with expression of type 'time_t' (aka 'long') is always false [-Wtautological-constant-out-of-range-compare]
  639 |             secs == LLONG_MAX && errno == ERANGE)
      |             ~~~~ ^  ~~~~~~~~~

Test: new test
Change-Id: I6e9141f4ec9fb9e9fad6519632ad41a189489e47
diff --git a/libc/tzcode/strptime.c b/libc/tzcode/strptime.c
index 20160c9..27b1cd3 100644
--- a/libc/tzcode/strptime.c
+++ b/libc/tzcode/strptime.c
@@ -632,11 +632,19 @@
 	char *ep;
 
 	errno = 0;
+#if defined(__LP64__)
 	secs = strtoll(*buf, &ep, 10);
+#else
+	secs = strtol(*buf, &ep, 10);
+#endif
 	if (*buf == (unsigned char *)ep)
 		goto done;
 	if (secs < 0 ||
+#if defined(__LP64__)
 	    secs == LLONG_MAX && errno == ERANGE)
+#else
+	    secs == LONG_MAX && errno == ERANGE)
+#endif
 		goto done;
 	if (localtime_r(&secs, tm) == NULL)
 		goto done;
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index cf4de06..7c35d18 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -484,6 +484,62 @@
   EXPECT_EQ(0, tm.tm_hour);
 }
 
+TEST(time, strptime_s) {
+  setenv("TZ", "UTC", 1);
+
+  struct tm tm;
+
+  // 0 + 1 --- trivial.
+  tm = {};
+  ASSERT_EQ('\0', *strptime("1", "%s", &tm));
+  EXPECT_EQ(70, tm.tm_year);
+  EXPECT_EQ(0, tm.tm_mon);
+  EXPECT_EQ(1, tm.tm_mday);
+  EXPECT_EQ(0, tm.tm_hour);
+  EXPECT_EQ(0, tm.tm_min);
+  EXPECT_EQ(1, tm.tm_sec);
+
+  // INT32_MAX (aka "time_t max" for ILP32).
+  tm = {};
+  ASSERT_EQ('\0', *strptime("2147483647", "%s", &tm));
+  EXPECT_EQ(138, tm.tm_year);
+  EXPECT_EQ(0, tm.tm_mon);
+  EXPECT_EQ(19, tm.tm_mday);
+  EXPECT_EQ(3, tm.tm_hour);
+  EXPECT_EQ(14, tm.tm_min);
+  EXPECT_EQ(7, tm.tm_sec);
+
+  // INT32_MAX + 1 (aka overflow for ILP32).
+  // This should be easy to detect because it'll be negative.
+  tm = {};
+#if defined(__LP64__)
+  ASSERT_EQ('\0', *strptime("2147483648", "%s", &tm));
+#else
+  ASSERT_EQ(nullptr, strptime("2147483648", "%s", &tm));
+#endif
+
+  // This wraps to 1 as an int32_t.
+  tm = {};
+#if defined(__LP64__)
+  ASSERT_EQ('\0', *strptime("4294967297", "%s", &tm));
+  EXPECT_EQ(206, tm.tm_year);
+  EXPECT_EQ(1, tm.tm_mon);
+  EXPECT_EQ(7, tm.tm_mday);
+  EXPECT_EQ(6, tm.tm_hour);
+  EXPECT_EQ(28, tm.tm_min);
+  EXPECT_EQ(17, tm.tm_sec);
+#else
+  ASSERT_EQ(nullptr, strptime("4294967297", "%s", &tm));
+#endif
+
+  // INT64_MAX (aka "time_t max" for LP64).
+  // This actually fails for LP64 too...
+  // ...but in localtime_r() because the year is too large.
+  // (Wolfram Alpha says this is 21 times the age of the universe!)
+  tm = {};
+  ASSERT_EQ(nullptr, strptime("9223372036854775807", "%s", &tm));
+}
+
 TEST(time, strptime_u) {
   setenv("TZ", "UTC", 1);