Skip to content

Commit b917b34

Browse files
Merge pull request #3148 from kevinbackhouse/TimeValue-UBSAN
Stricter rules when parsing time values to avoid UBSAN error
2 parents 3b58bda + 94e1389 commit b917b34

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/value.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -924,20 +924,20 @@ int TimeValue::read(const std::string& buf) {
924924
}
925925

926926
auto hi = std::stoi(buf.substr(0, 2));
927-
if (hi > 23)
927+
if (hi < 0 || hi > 23)
928928
return printWarning();
929929
time_.hour = hi;
930930
if (buf.size() > 3) {
931931
auto mi = std::stoi(buf.substr(mpos, 2));
932-
if (mi > 59)
932+
if (mi < 0 || mi > 59)
933933
return printWarning();
934934
time_.minute = std::stoi(buf.substr(mpos, 2));
935935
} else {
936936
time_.minute = 0;
937937
}
938938
if (buf.size() > 5) {
939939
auto si = std::stoi(buf.substr(spos, 2));
940-
if (si > 60)
940+
if (si < 0 || si > 60)
941941
return printWarning();
942942
time_.second = std::stoi(buf.substr(spos, 2));
943943
} else {
@@ -954,23 +954,23 @@ int TimeValue::read(const std::string& buf) {
954954
if (posColon == std::string::npos) {
955955
// Extended format
956956
auto tzhi = std::stoi(format.substr(0, 3));
957-
if (tzhi > 23)
957+
if (tzhi < -23 || tzhi > 23)
958958
return printWarning();
959959
time_.tzHour = tzhi;
960960
if (format.size() > 3) {
961961
int minute = std::stoi(format.substr(3));
962-
if (minute > 59)
962+
if (minute < 0 || minute > 59)
963963
return printWarning();
964964
time_.tzMinute = time_.tzHour < 0 ? -minute : minute;
965965
}
966966
} else {
967967
// Basic format
968968
auto tzhi = std::stoi(format.substr(0, posColon));
969-
if (tzhi > 23)
969+
if (tzhi < -23 || tzhi > 23)
970970
return printWarning();
971971
time_.tzHour = tzhi;
972972
int minute = std::stoi(format.substr(posColon + 1));
973-
if (minute > 59)
973+
if (minute < 0 || minute > 59)
974974
return printWarning();
975975
time_.tzMinute = time_.tzHour < 0 ? -minute : minute;
976976
}

0 commit comments

Comments
 (0)