From fe77fedbbf1804c6f763cfea983cac2a0b789b57 Mon Sep 17 00:00:00 2001 From: RedHeadphone Date: Wed, 13 Apr 2022 15:19:21 +0530 Subject: [PATCH] validate input in Time.get_datetime_string_from_dict() format fix better fix to handle ERR_FAIL_COND_V_MSG return data type (cherry picked from commit 7e35af3dc32870dcbbba95c13ccf6273ed7357ea) --- core/os/time.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/core/os/time.cpp b/core/os/time.cpp index cab28ee6a..0204aef49 100644 --- a/core/os/time.cpp +++ b/core/os/time.cpp @@ -95,16 +95,16 @@ VARIANT_ENUM_CAST(Time::Weekday); day = day_number_copy + 1; \ } -#define VALIDATE_YMDHMS \ - 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."); \ - ERR_FAIL_COND_V_MSG(month > 12, 0, "Invalid month value of: " + itos(month) + ". See the Time.Month enum for valid values."); \ - ERR_FAIL_COND_V_MSG(hour > 23, 0, "Invalid hour value of: " + itos(hour) + "."); \ - ERR_FAIL_COND_V_MSG(minute > 59, 0, "Invalid minute value of: " + itos(minute) + "."); \ - ERR_FAIL_COND_V_MSG(second > 59, 0, "Invalid second value of: " + itos(second) + " (leap seconds are not supported)."); \ - /* Do this check after month is tested as valid. */ \ - ERR_FAIL_COND_V_MSG(day == 0, 0, "Invalid day value of: " + itos(month) + ", days are 1-indexed and cannot be 0."); \ - uint8_t days_in_this_month = MONTH_DAYS_TABLE[IS_LEAP_YEAR(year)][month - 1]; \ - 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) + "."); +#define VALIDATE_YMDHMS(ret) \ + 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."); \ + ERR_FAIL_COND_V_MSG(month > 12, ret, "Invalid month value of: " + itos(month) + ". See the Time.Month enum for valid values."); \ + ERR_FAIL_COND_V_MSG(hour > 23, ret, "Invalid hour value of: " + itos(hour) + "."); \ + ERR_FAIL_COND_V_MSG(minute > 59, ret, "Invalid minute value of: " + itos(minute) + "."); \ + ERR_FAIL_COND_V_MSG(second > 59, ret, "Invalid second value of: " + itos(second) + " (leap seconds are not supported)."); \ + /* Do this check after month is tested as valid. */ \ + ERR_FAIL_COND_V_MSG(day == 0, ret, "Invalid day value of: " + itos(month) + ", days are 1-indexed and cannot be 0."); \ + uint8_t days_in_this_month = MONTH_DAYS_TABLE[IS_LEAP_YEAR(year)][month - 1]; \ + 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) + "."); #define YMD_TO_DAY_NUMBER \ /* 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 String Time::get_datetime_string_from_dict(const Dictionary p_datetime, bool p_use_space) const { ERR_FAIL_COND_V_MSG(p_datetime.empty(), "", "Invalid datetime Dictionary: Dictionary is empty."); EXTRACT_FROM_DICTIONARY + VALIDATE_YMDHMS("") // vformat only supports up to 6 arguments, so we need to split this up into 2 parts. String timestamp = vformat("%04d-%02d-%02d", year, (uint8_t)month, day); if (p_use_space) { @@ -286,14 +287,14 @@ String Time::get_datetime_string_from_dict(const Dictionary p_datetime, bool p_u int64_t Time::get_unix_time_from_datetime_dict(const Dictionary p_datetime) const { ERR_FAIL_COND_V_MSG(p_datetime.empty(), 0, "Invalid datetime Dictionary: Dictionary is empty"); EXTRACT_FROM_DICTIONARY - VALIDATE_YMDHMS + VALIDATE_YMDHMS(0) YMD_TO_DAY_NUMBER return day_number * SECONDS_PER_DAY + hour * 3600 + minute * 60 + second; } int64_t Time::get_unix_time_from_datetime_string(String p_datetime) const { PARSE_ISO8601_STRING - VALIDATE_YMDHMS + VALIDATE_YMDHMS(0) YMD_TO_DAY_NUMBER return day_number * SECONDS_PER_DAY + hour * 3600 + minute * 60 + second; }