Skip to content

Commit c7922af

Browse files
authored
Merge pull request godotengine#60203 from RedHeadphone/master
Fix Time.get_datetime_string_from_dict() does not validate input
2 parents bd9ba4b + 7e35af3 commit c7922af

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

core/os/time.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ VARIANT_ENUM_CAST(Time::Weekday);
9595
day = day_number_copy + 1; \
9696
}
9797

98-
#define VALIDATE_YMDHMS \
99-
ERR_FAIL_COND_V_MSG(month == 0, 0, "Invalid month value of: " + itos(month) + ", months are 1-indexed and cannot be 0. See the Time.Month enum for valid values."); \
100-
ERR_FAIL_COND_V_MSG(month > 12, 0, "Invalid month value of: " + itos(month) + ". See the Time.Month enum for valid values."); \
101-
ERR_FAIL_COND_V_MSG(hour > 23, 0, "Invalid hour value of: " + itos(hour) + "."); \
102-
ERR_FAIL_COND_V_MSG(minute > 59, 0, "Invalid minute value of: " + itos(minute) + "."); \
103-
ERR_FAIL_COND_V_MSG(second > 59, 0, "Invalid second value of: " + itos(second) + " (leap seconds are not supported)."); \
104-
/* Do this check after month is tested as valid. */ \
105-
ERR_FAIL_COND_V_MSG(day == 0, 0, "Invalid day value of: " + itos(month) + ", days are 1-indexed and cannot be 0."); \
106-
uint8_t days_in_this_month = MONTH_DAYS_TABLE[IS_LEAP_YEAR(year)][month - 1]; \
107-
ERR_FAIL_COND_V_MSG(day > days_in_this_month, 0, "Invalid day value of: " + itos(day) + " which is larger than the maximum for this month, " + itos(days_in_this_month) + ".");
98+
#define VALIDATE_YMDHMS(ret) \
99+
ERR_FAIL_COND_V_MSG(month == 0, ret, "Invalid month value of: " + itos(month) + ", months are 1-indexed and cannot be 0. See the Time.Month enum for valid values."); \
100+
ERR_FAIL_COND_V_MSG(month > 12, ret, "Invalid month value of: " + itos(month) + ". See the Time.Month enum for valid values."); \
101+
ERR_FAIL_COND_V_MSG(hour > 23, ret, "Invalid hour value of: " + itos(hour) + "."); \
102+
ERR_FAIL_COND_V_MSG(minute > 59, ret, "Invalid minute value of: " + itos(minute) + "."); \
103+
ERR_FAIL_COND_V_MSG(second > 59, ret, "Invalid second value of: " + itos(second) + " (leap seconds are not supported)."); \
104+
/* Do this check after month is tested as valid. */ \
105+
ERR_FAIL_COND_V_MSG(day == 0, ret, "Invalid day value of: " + itos(month) + ", days are 1-indexed and cannot be 0."); \
106+
uint8_t days_in_this_month = MONTH_DAYS_TABLE[IS_LEAP_YEAR(year)][month - 1]; \
107+
ERR_FAIL_COND_V_MSG(day > days_in_this_month, ret, "Invalid day value of: " + itos(day) + " which is larger than the maximum for this month, " + itos(days_in_this_month) + ".");
108108

109109
#define YMD_TO_DAY_NUMBER \
110110
/* The day number since Unix epoch (0-index). Days before 1970 are negative. */ \
@@ -273,6 +273,7 @@ Dictionary Time::get_datetime_dict_from_string(String p_datetime, bool p_weekday
273273
String Time::get_datetime_string_from_dict(const Dictionary p_datetime, bool p_use_space) const {
274274
ERR_FAIL_COND_V_MSG(p_datetime.is_empty(), "", "Invalid datetime Dictionary: Dictionary is empty.");
275275
EXTRACT_FROM_DICTIONARY
276+
VALIDATE_YMDHMS("")
276277
// vformat only supports up to 6 arguments, so we need to split this up into 2 parts.
277278
String timestamp = vformat("%04d-%02d-%02d", year, (uint8_t)month, day);
278279
if (p_use_space) {
@@ -286,14 +287,14 @@ String Time::get_datetime_string_from_dict(const Dictionary p_datetime, bool p_u
286287
int64_t Time::get_unix_time_from_datetime_dict(const Dictionary p_datetime) const {
287288
ERR_FAIL_COND_V_MSG(p_datetime.is_empty(), 0, "Invalid datetime Dictionary: Dictionary is empty");
288289
EXTRACT_FROM_DICTIONARY
289-
VALIDATE_YMDHMS
290+
VALIDATE_YMDHMS(0)
290291
YMD_TO_DAY_NUMBER
291292
return day_number * SECONDS_PER_DAY + hour * 3600 + minute * 60 + second;
292293
}
293294

294295
int64_t Time::get_unix_time_from_datetime_string(String p_datetime) const {
295296
PARSE_ISO8601_STRING
296-
VALIDATE_YMDHMS
297+
VALIDATE_YMDHMS(0)
297298
YMD_TO_DAY_NUMBER
298299
return day_number * SECONDS_PER_DAY + hour * 3600 + minute * 60 + second;
299300
}

0 commit comments

Comments
 (0)