Fix input validation check for TimeFractionFormat in the DateTime ToString method (#5927)

* Fix input validation check for TimeFractionFormat in the DateTime ToString() method.

* Add changelog entry.

* Use switch-case instead of static casting the enum values.

* Address CL PR feedback.

* Fix the features added CL to put ResourceIdentifier in the right release.

* Address PR feedback - use type traits
This commit is contained in:
Ahson Khan 2024-09-10 14:35:07 -07:00 committed by GitHub
parent cfcfac67f7
commit c3265d3e5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View File

@ -6,11 +6,13 @@
- Request logs to now include the `accept-range`, `content-range`, `range`, `WWW-Authenticate`, `x-ms-date`, `x-ms-error-code`, `x-ms-range`, and `x-ms-version` headers.
- Added default constructor, `Parse()`, and equality comparison operators to `Azure::Core::Uuid`.
- Added an `Azure::Core::ResourceIdentifier` type.
### Breaking Changes
### Bugs Fixed
- Throw `std::invalid_argument` if the value of `TimeFractionFormat` enum passed in to `DateTime::ToString()` is invalid.
- `Azure::Core::Uuid::ToString()` is now `const`.
### Other Changes
@ -20,7 +22,6 @@
### Features Added
- Added new constructor for `Azure::Core::Context` that takes a `std::chrono::system_clock::time_point` deadline. This enables creating a new context directly with a deadline.
- Added an `Azure::Core::ResourceIdentifier` type.
### Breaking Changes

View File

@ -12,6 +12,7 @@
#include <limits>
#include <sstream>
#include <stdexcept>
#include <type_traits>
using namespace Azure;
@ -863,6 +864,20 @@ std::string DateTime::ToString(DateFormat format, TimeFractionFormat fractionFor
"Unrecognized date format (" + std::to_string(static_cast<int64_t>(format)) + ").");
}
switch (fractionFormat)
{
case TimeFractionFormat::DropTrailingZeros:
case TimeFractionFormat::AllDigits:
case TimeFractionFormat::Truncate:
break;
default:
throw std::invalid_argument(
"Unrecognized time fraction format ("
+ std::to_string(
static_cast<std::underlying_type<TimeFractionFormat>::type>(fractionFormat))
+ ").");
}
ThrowIfUnsupportedYear();
int16_t year = 1;

View File

@ -494,6 +494,13 @@ TEST(DateTime, ToStringInvalid)
EXPECT_THROW(
dt.ToString(DateTime::DateFormat::Rfc1123, static_cast<DateTime::TimeFractionFormat>(3)),
std::invalid_argument);
EXPECT_THROW(
dt.ToString(DateTime::DateFormat::Rfc3339, static_cast<DateTime::TimeFractionFormat>(3)),
std::invalid_argument);
EXPECT_THROW(
dt.ToString(DateTime::DateFormat::Rfc3339, static_cast<DateTime::TimeFractionFormat>(-3)),
std::invalid_argument);
}
TEST(DateTime, ParseTimeInvalid2)