|
11 | 11 | #include "temporal_rs/TimeZone.hpp" |
12 | 12 |
|
13 | 13 | #ifdef V8_INTL_SUPPORT |
14 | | -#ifndef V8_ENABLE_TEMPORAL_SYSTEMICU |
| 14 | +#if !defined(V8_ENABLE_TEMPORAL_SYSTEMICU) && !defined(USING_SYSTEM_ICU) |
15 | 15 | // ICU internal header file |
16 | 16 | #include "udatamem.h" |
17 | 17 | #else |
@@ -58,7 +58,7 @@ ZoneInfo64Provider::ZoneInfo64Provider() { |
58 | 58 | provider = temporal_rs::Provider::empty(); |
59 | 59 | return; |
60 | 60 | } |
61 | | -#ifndef V8_ENABLE_TEMPORAL_SYSTEMICU |
| 61 | +#if !defined(V8_ENABLE_TEMPORAL_SYSTEMICU) && !defined(USING_SYSTEM_ICU) |
62 | 62 | // NOT udata_getLength: this ignores the header, |
63 | 63 | // and we're parsing resb files with the header |
64 | 64 | auto length = memory->length; |
@@ -71,6 +71,51 @@ ZoneInfo64Provider::ZoneInfo64Provider() { |
71 | 71 | auto length = shadowMemory->length; |
72 | 72 | auto data = shadowMemory->data; |
73 | 73 | #endif |
| 74 | + DCHECK_WITH_MSG(data != nullptr, "ICU4C returned nullptr"); |
| 75 | + if (data == nullptr) { |
| 76 | + provider = temporal_rs::Provider::empty(); |
| 77 | + return; |
| 78 | + } |
| 79 | + // ICU4C sets length = -1 for the last entry in an offset TOC (.dat) archive |
| 80 | + // (e.g. when zoneinfo64.res is the final entry in a small-icu build) and for |
| 81 | + // all entries in a pointer TOC archive. Determine the exact byte length of |
| 82 | + // the ResB bundle from its self-describing index table (see res_init() in |
| 83 | + // ICU4C's uresdata.cpp). |
| 84 | + int32_t resb_length = -1; |
| 85 | + const void* body = udata_getMemory(memory); |
| 86 | + if (body != nullptr) { |
| 87 | + ptrdiff_t header_length = |
| 88 | + static_cast<const uint8_t*>(body) - static_cast<const uint8_t*>(data); |
| 89 | + constexpr uint32_t kUresIndexBundleTop = 3; |
| 90 | + constexpr ptrdiff_t kMinResbBytes = static_cast<ptrdiff_t>( |
| 91 | + (1 + 1 + kUresIndexBundleTop) * sizeof(uint32_t)); |
| 92 | + if (header_length >= 0 && header_length % 4 == 0 && |
| 93 | + (length == -1 || length >= header_length + kMinResbBytes)) { |
| 94 | + const uint32_t* body_32 = static_cast<const uint32_t*>(body); |
| 95 | + // body_32[0] is the root resource word; body_32[1..] is indexes[]. |
| 96 | + // indexes[URES_INDEX_LENGTH] & 0xff holds the length of indexes[]. |
| 97 | + uint32_t index_length = body_32[1] & 0xff; |
| 98 | + if (index_length > kUresIndexBundleTop) { |
| 99 | + // indexes[URES_INDEX_BUNDLE_TOP] is the number of 32-bit words in the |
| 100 | + // bundle body. |
| 101 | + uint32_t bundle_top = body_32[1 + kUresIndexBundleTop]; |
| 102 | + if (bundle_top >= 1 + index_length && bundle_top <= 0x1000000) { |
| 103 | + resb_length = static_cast<int32_t>(header_length) + |
| 104 | + static_cast<int32_t>(bundle_top * sizeof(uint32_t)); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + DCHECK_WITH_MSG(resb_length != -1, |
| 110 | + "zoneinfo64.res should have a valid ResB index table"); |
| 111 | + DCHECK_IMPLIES(length != -1, resb_length <= length); |
| 112 | + if (resb_length == -1 || (length != -1 && resb_length > length)) { |
| 113 | + provider = temporal_rs::Provider::empty(); |
| 114 | + return; |
| 115 | + } |
| 116 | + if (length == -1) { |
| 117 | + length = resb_length; |
| 118 | + } |
74 | 119 | // This really shouldn't happen: ICU4C pads these files |
75 | 120 | // to 4 when baking them in |
76 | 121 | DCHECK_WITH_MSG(length % 4 == 0, "ICU4C should pad udata to uint32_t"); |
@@ -80,7 +125,6 @@ ZoneInfo64Provider::ZoneInfo64Provider() { |
80 | 125 | provider = temporal_rs::Provider::empty(); |
81 | 126 | return; |
82 | 127 | } |
83 | | - DCHECK_WITH_MSG(data != nullptr, "ICU4C returned nullptr"); |
84 | 128 |
|
85 | 129 | const uint32_t* data_32 = static_cast<const uint32_t*>(data); |
86 | 130 | std::span<const uint32_t> data_span(data_32, length / 4); |
|