Commit 63a9a63f authored by davidben's avatar davidben Committed by Commit bot

Fix more integer underflow cases in base::Time::FromExploded.

https://codereview.chromium.org/2405453002 fixed some of it, but there
were still others. The subtractions to pick a different starting point
for the year and month may also underflow.

BUG=668445

Review-Url: https://codereview.chromium.org/2532243005
Cr-Commit-Position: refs/heads/master@{#435073}
parent f00c76e4
......@@ -16,6 +16,7 @@
#include <ostream>
#include "base/logging.h"
#include "base/numerics/safe_math.h"
#include "build/build_config.h"
#if defined(OS_ANDROID)
......@@ -227,19 +228,28 @@ void Time::Explode(bool is_local, Exploded* exploded) const {
// static
bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) {
CheckedNumeric<int> month = exploded.month;
month--;
CheckedNumeric<int> year = exploded.year;
year -= 1900;
if (!month.IsValid() || !year.IsValid()) {
*time = Time(0);
return false;
}
struct tm timestruct;
timestruct.tm_sec = exploded.second;
timestruct.tm_min = exploded.minute;
timestruct.tm_hour = exploded.hour;
timestruct.tm_mday = exploded.day_of_month;
timestruct.tm_mon = exploded.month - 1;
timestruct.tm_year = exploded.year - 1900;
timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this
timestruct.tm_yday = 0; // mktime/timegm ignore this
timestruct.tm_isdst = -1; // attempt to figure it out
timestruct.tm_sec = exploded.second;
timestruct.tm_min = exploded.minute;
timestruct.tm_hour = exploded.hour;
timestruct.tm_mday = exploded.day_of_month;
timestruct.tm_mon = month.ValueOrDie();
timestruct.tm_year = year.ValueOrDie();
timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this
timestruct.tm_yday = 0; // mktime/timegm ignore this
timestruct.tm_isdst = -1; // attempt to figure it out
#if !defined(OS_NACL) && !defined(OS_SOLARIS)
timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore
timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore
timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore
timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore
#endif
SysTime seconds;
......
......@@ -59,6 +59,11 @@ TEST(TimeTestOutOfBounds, FromExplodedOutOfBoundsTime) {
{{9840633, 1, 0, 1, 1, 1, 0, 0}, true},
// Underflow will fail as well.
{{-9840633, 1, 0, 1, 1, 1, 0, 0}, true},
// Test integer overflow and underflow cases for the values themselves.
{{std::numeric_limits<int>::min(), 1, 0, 1, 1, 1, 0, 0}, true},
{{std::numeric_limits<int>::max(), 1, 0, 1, 1, 1, 0, 0}, true},
{{2016, std::numeric_limits<int>::min(), 0, 1, 1, 1, 0, 0}, false},
{{2016, std::numeric_limits<int>::max(), 0, 1, 1, 1, 0, 0}, false},
};
for (const auto& test : kDateTestData) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment