{"record":{"id":"d2ec943c6066d958","repo":"nodejs/node","slug":"toolutil-getcptriesize-error-ucptrie-tobinary-f","errorCode":null,"errorMessage":"toolutil/getCPTrieSize error: ucptrie_toBinary() failed: %s (length %ld)\n","messagePattern":"toolutil/getCPTrieSize error: ucptrie_toBinary\\(\\) failed: (.+?) \\(length %ld\\)\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"deps/icu-small/source/tools/toolutil/toolutil.cpp","lineNumber":144,"sourceCode":"        }\n        start = rangeEnd + 1;\n    }\n}\n\nint32_t getCPTrieSize(UMutableCPTrie *mt, UCPTrieType type, UCPTrieValueWidth valueWidth) {\n    UErrorCode errorCode = U_ZERO_ERROR;\n    UCPTrie *cpTrie = umutablecptrie_buildImmutable(mt, type, valueWidth, &errorCode);\n    if (U_FAILURE(errorCode)) {\n        fprintf(stderr,\n                \"toolutil/getCPTrieSize error: umutablecptrie_buildImmutable() failed: %s\\n\",\n                u_errorName(errorCode));\n        return -1;\n    }\n    uint8_t block[100000];\n    int32_t size = ucptrie_toBinary(cpTrie, block, sizeof(block), &errorCode);\n    ucptrie_close(cpTrie);\n    if (U_FAILURE(errorCode) && errorCode != U_BUFFER_OVERFLOW_ERROR) {\n        fprintf(stderr,\n                \"toolutil/getCPTrieSize error: ucptrie_toBinary() failed: %s (length %ld)\\n\",\n                u_errorName(errorCode), static_cast<long>(size));\n        return -1;\n    }\n    U_ASSERT((size & 3) == 0);  // multiple of 4 bytes\n    return size;\n}\n\n}  // toolutil\n\nU_NAMESPACE_END\n\nstatic int32_t currentYear = -1;\n\nU_CAPI int32_t U_EXPORT2 getCurrentYear() {\n    if(currentYear == -1) {\n        time_t now = time(nullptr);\n        tm *fields = gmtime(&now);","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/icu-small/source/tools/toolutil/toolutil.cpp#L126-L162","documentation":"Thrown by getCPTrieSize() when ucptrie_toBinary() fails during serialization of a built immutable trie to a binary buffer. The function uses a fixed 100000-byte stack buffer; U_BUFFER_OVERFLOW_ERROR is explicitly filtered out (since the needed size is still returned in that case), so this error fires only on genuine serialization failures.","triggerScenarios":"ucptrie_toBinary() fails with an error other than U_BUFFER_OVERFLOW_ERROR after umutablecptrie_buildImmutable succeeded. This indicates the immutable trie is internally inconsistent or corrupted in a way that prevents serialization, despite building successfully.","commonSituations":"Very rare; could indicate a bug in the trie building logic, memory corruption affecting the trie structure, or an ICU version where buildImmutable and toBinary have inconsistent assumptions about trie validity.","solutions":["Examine the specific ICU error name and reported length in the message.","Rebuild the trie from scratch to rule out memory corruption.","Check for ICU version mismatches between the trie-building and serialization code paths.","Report as a potential ICU bug if the trie builds successfully but fails to serialize.","Verify no memory corruption by running under AddressSanitizer or Valgrind."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Verify trie is valid after build before calling getCPTrieSize\n#include <unicode/ucptrie.h>\n\nbool trieIsBuildable(UMutableCPTrie* mt, UCPTrieType type, UCPTrieValueWidth width) {\n    UErrorCode err = U_ZERO_ERROR;\n    UCPTrie* test = umutablecptrie_buildImmutable(mt, type, width, &err);\n    if (U_FAILURE(err)) return false;\n    // Test serialization with a small buffer to check for non-overflow errors\n    uint8_t smallBuf[64];\n    int32_t sz = ucptrie_toBinary(test, smallBuf, sizeof(smallBuf), &err);\n    ucptrie_close(test);\n    // U_BUFFER_OVERFLOW_ERROR is expected and OK\n    return (err == U_ZERO_ERROR || err == U_BUFFER_OVERFLOW_ERROR);\n}","typeGuard":null,"tryCatchPattern":"int32_t size = getCPTrieSize(mt, type, valueWidth);\nif (size < 0) {\n    // Either buildImmutable or toBinary failed\n    fprintf(stderr, \"getCPTrieSize returned %d; trie may be corrupt\\n\", size);\n    umutablecptrie_close(mt);\n    return U_INVALID_FORMAT_ERROR;\n}","preventionTips":["Check getCPTrieSize return value (-1 indicates failure).","Run build tools under AddressSanitizer to detect memory corruption affecting trie state.","Ensure no concurrent modifications to the trie during size calculation.","Report persistent buildImmutable-succeeds-but-toBinary-fails cases as potential ICU bugs."],"tags":["icu","trie","build-tool","serialization","data-generation"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}