Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stricter rules when parsing time values to avoid UBSAN error (backport #3148) #3151

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,20 +925,20 @@ int TimeValue::read(const std::string& buf) {
}

auto hi = std::stoi(buf.substr(0, 2));
if (hi > 23)
if (hi < 0 || hi > 23)
return printWarning();
time_.hour = hi;
if (buf.size() > 3) {
auto mi = std::stoi(buf.substr(mpos, 2));
if (mi > 59)
if (mi < 0 || mi > 59)
return printWarning();
time_.minute = std::stoi(buf.substr(mpos, 2));
} else {
time_.minute = 0;
}
if (buf.size() > 5) {
auto si = std::stoi(buf.substr(spos, 2));
if (si > 60)
if (si < 0 || si > 60)
return printWarning();
time_.second = std::stoi(buf.substr(spos, 2));
} else {
Expand All @@ -955,23 +955,23 @@ int TimeValue::read(const std::string& buf) {
if (posColon == std::string::npos) {
// Extended format
auto tzhi = std::stoi(format.substr(0, 3));
if (tzhi > 23)
if (tzhi < -23 || tzhi > 23)
return printWarning();
time_.tzHour = tzhi;
if (format.size() > 3) {
int minute = std::stoi(format.substr(3));
if (minute > 59)
if (minute < 0 || minute > 59)
return printWarning();
time_.tzMinute = time_.tzHour < 0 ? -minute : minute;
}
} else {
// Basic format
auto tzhi = std::stoi(format.substr(0, posColon));
if (tzhi > 23)
if (tzhi < -23 || tzhi > 23)
return printWarning();
time_.tzHour = tzhi;
int minute = std::stoi(format.substr(posColon + 1));
if (minute > 59)
if (minute < 0 || minute > 59)
return printWarning();
time_.tzMinute = time_.tzHour < 0 ? -minute : minute;
}
Expand Down