ICU-23353 Fix double-delete in Region::cleanupRegionData
Checklist
- [x] Required: Issue filed: ICU-23353
- [x] Required: The PR title must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
- [x] Required: Each commit message must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
- [ ] Issue accepted (done by Technical Committee after discussion)
- [x] Tests included, if applicable
- [ ] API docs and/or User Guide docs changed or added, if applicable
Summary
Region::cleanupRegionData() in region.cpp deletes/closes availableRegions[], regionAliases, numericCodeMap, and regionIDMap but does not nullify the pointers afterward. If cleanup is called multiple times (e.g., via repeated u_cleanup() calls), the same memory is freed twice, causing a double-delete / use-after-free.
Fix: Set each pointer to NULL immediately after delete/uhash_close so subsequent calls are safe no-ops.
Test plan
- [x]
TestDoubleCleanupadded toregiontst.cpp: initializes Region data, callsu_cleanup()twice in a row, then verifies Region can be re-initialized and used normally. Without the fix this crashes on the secondu_cleanup(). - [x] Existing ICU region tests should continue to pass.
Could a maintainer please transition the Jira ticket ICU-23353 from New to Accepted? The jira-ticket CI check requires the ticket to be in "Accepted" status, but as a new contributor I don't have permission to change the ticket status. Thank you!
Hooray! The files in the branch are the same across the force-push. 😃
~ Your Friendly Jira-GitHub PR Checker Bot
While working on this fix, I noticed the same pattern (free-without-nullify in cleanup functions) exists in several other places on the main branch. These would also crash or corrupt memory if u_cleanup() is called twice.
1. dayPeriodRulesCleanup() — nullptr dereference
File: icu4c/source/i18n/dayperiodrules.cpp:307-313
U_CFUNC UBool U_CALLCONV dayPeriodRulesCleanup() {
delete[] data->rules; // 2nd call: data is nullptr → crash
uhash_close(data->localeToRuleSetNumMap);
delete data;
data = nullptr;
return true;
}
First call sets data = nullptr, but the function has no if (data) guard, so a second call dereferences nullptr.
2. allowedHourFormatsCleanup() — double uhash_close
File: icu4c/source/i18n/dtptngen.cpp:468-471
U_CFUNC UBool U_CALLCONV allowedHourFormatsCleanup() {
uhash_close(localeToAllowedHourFormatsMap);
// missing: localeToAllowedHourFormatsMap = nullptr;
return true;
}
No nullification, no UInitOnce reset. Second call closes already-freed hash.
3. cleanupKnownCanonicalized() — double uhash_close
File: icu4c/source/common/locid.cpp:640-644
UBool U_CALLCONV cleanupKnownCanonicalized() {
gKnownCanonicalizedInitOnce.reset();
if (gKnownCanonicalized) { uhash_close(gKnownCanonicalized); }
// missing: gKnownCanonicalized = nullptr;
return true;
}
The if guard passes on second call because the pointer is dangling (non-null), not nullptr.
4. AliasData::cleanup() — double delete
File: icu4c/source/common/locid.cpp:806-812
UBool U_CALLCONV AliasData::cleanup() {
gInitOnce.reset();
delete gSingleton;
// missing: gSingleton = nullptr;
return true;
}
Happy to submit a separate PR for these if you'd like, or leave it to you. Just wanted to flag them since they're the same class of bug.
Hooray! The files in the branch are the same across the force-push. 😃
~ Your Friendly Jira-GitHub PR Checker Bot
This PR has been quiet since 2026-04-10. I have picked the work up in #4342: on current main the Region double-delete is already fixed (the nullification has been in place since ICU-21763 / the ICU-21833 NULL->nullptr lineage, so this PR's remaining value is its maint/maint-54 base), and what was still open is the sibling free-without-nullify cleanup functions from your 2026-04-10 comment. #4342 hardens dayPeriodRulesCleanup, allowedHourFormatsCleanup, cleanupKnownCanonicalized and AliasData::cleanup at current HEAD, with a regression test that answers Markus's question (the end-of-suite double u_cleanup() plus the documented u_cleanup()/u_init() re-initialization contract — the double call alone is a dispatcher no-op at HEAD; the reproducing shape is post-cleanup reuse and the failed-re-arm double close). Happy to drop #4342 if you plan to continue here.