mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(rtc): validate ticks before timezone conversion
This commit is contained in:
@@ -376,12 +376,16 @@ int APS5_VABI sceRtcGetCurrentClockLocalTime(RtcDateTime* time) {
|
||||
|
||||
int APS5_VABI sceRtcConvertUtcToLocalTime(const RtcTick* utc, RtcTick* local_time) {
|
||||
if (!utc || !local_time) return SCE_RTC_ERROR_INVALID_POINTER;
|
||||
if (utc->tick > MAX_TICK) return SCE_RTC_ERROR_INVALID_VALUE;
|
||||
return addTicks(local_time, utc, localOffsetSeconds(utc->tick), TICKS_PER_SECOND);
|
||||
}
|
||||
|
||||
int APS5_VABI sceRtcConvertLocalTimeToUtc(const RtcTick* local_time, RtcTick* utc) {
|
||||
if (!local_time || !utc) return SCE_RTC_ERROR_INVALID_POINTER;
|
||||
const std::int64_t offset = localOffsetSeconds(local_time->tick - static_cast<std::uint64_t>(localOffsetSeconds(local_time->tick) * TICKS_PER_SECOND));
|
||||
if (local_time->tick > MAX_TICK) return SCE_RTC_ERROR_INVALID_VALUE;
|
||||
RtcTick estimate{};
|
||||
if (const int result = addTicks(&estimate, local_time, -localOffsetSeconds(local_time->tick), TICKS_PER_SECOND); result != 0) return result;
|
||||
const std::int64_t offset = localOffsetSeconds(estimate.tick);
|
||||
return addTicks(utc, local_time, -offset, TICKS_PER_SECOND);
|
||||
}
|
||||
|
||||
@@ -460,6 +464,7 @@ int APS5_VABI sceRtcFormatRFC3339(char* date_time, const RtcTick* utc, int time_
|
||||
|
||||
int APS5_VABI sceRtcFormatRFC3339LocalTime(char* date_time, const RtcTick* utc) {
|
||||
if (!date_time || !utc) return SCE_RTC_ERROR_INVALID_POINTER;
|
||||
if (utc->tick > MAX_TICK) return SCE_RTC_ERROR_INVALID_VALUE;
|
||||
return sceRtcFormatRFC3339(date_time, utc, localOffsetMinutes(*utc));
|
||||
}
|
||||
|
||||
@@ -477,6 +482,7 @@ int APS5_VABI sceRtcFormatRFC2822(char* date_time, const RtcTick* utc, int time_
|
||||
|
||||
int APS5_VABI sceRtcFormatRFC2822LocalTime(char* date_time, const RtcTick* utc) {
|
||||
if (!date_time || !utc) return SCE_RTC_ERROR_INVALID_POINTER;
|
||||
if (utc->tick > MAX_TICK) return SCE_RTC_ERROR_INVALID_VALUE;
|
||||
return sceRtcFormatRFC2822(date_time, utc, localOffsetMinutes(*utc));
|
||||
}
|
||||
|
||||
|
||||
@@ -402,4 +402,29 @@ int main() {
|
||||
Require(sceRtcFormatRFC2822LocalTime(rfc2822, nullptr) == invalidPointer);
|
||||
Require(sceRtcFormatRFC3339LocalTime(nullptr, &tick) == invalidPointer);
|
||||
Require(sceRtcFormatRFC3339LocalTime(text, nullptr) == invalidPointer);
|
||||
for (const char* zone : {"UTC0", "XXX-5:30", "XXX+3"}) {
|
||||
SetTimeZone(zone);
|
||||
for (std::uint64_t invalidTick : {maxTick + 1, std::uint64_t{1} << 63u, std::numeric_limits<std::uint64_t>::max()}) {
|
||||
source.tick = invalidTick;
|
||||
result.tick = 123;
|
||||
Require(sceRtcConvertUtcToLocalTime(&source, &result) == invalidValue && result.tick == 123);
|
||||
Require(sceRtcConvertLocalTimeToUtc(&source, &result) == invalidValue && result.tick == 123);
|
||||
Require(sceRtcConvertUtcToLocalTime(&source, &source) == invalidValue && source.tick == invalidTick);
|
||||
Require(sceRtcConvertLocalTimeToUtc(&source, &source) == invalidValue && source.tick == invalidTick);
|
||||
std::memset(text, 'x', sizeof(text));
|
||||
char originalText[sizeof(text)];
|
||||
std::memcpy(originalText, text, sizeof(text));
|
||||
Require(sceRtcFormatRFC3339LocalTime(text, &source) == invalidValue);
|
||||
Require(std::memcmp(text, originalText, sizeof(text)) == 0);
|
||||
std::memset(rfc2822, 'x', sizeof(rfc2822));
|
||||
char originalRfc2822[sizeof(rfc2822)];
|
||||
std::memcpy(originalRfc2822, rfc2822, sizeof(rfc2822));
|
||||
Require(sceRtcFormatRFC2822LocalTime(rfc2822, &source) == invalidValue);
|
||||
Require(std::memcmp(rfc2822, originalRfc2822, sizeof(rfc2822)) == 0);
|
||||
}
|
||||
source.tick = leapDayTick;
|
||||
Require(sceRtcConvertUtcToLocalTime(&source, &result) == 0);
|
||||
Require(sceRtcConvertLocalTimeToUtc(&result, &result) == 0 && result.tick == source.tick);
|
||||
}
|
||||
SetTimeZone("UTC0");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user