{"record":{"id":"fb98c529e62e58a8","repo":"Humanizr/Humanizer","slug":"unable-to-protect-the-byte-size-unit-token","errorCode":null,"errorMessage":"Unable to protect the byte-size unit token.","messagePattern":"Unable to protect the byte-size unit token\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Humanizer/Bytes/ByteSize.cs","lineNumber":1133,"sourceCode":"                    result[index] = '\\0';\n                }\n            }\n        }\n\n        return new(result);\n    }\n\n    static string FindFormatSentinel(params string[] values)\n    {\n        for (var candidate = '\\uE000'; candidate <= '\\uF8FF'; candidate++)\n        {\n            if (values.All(value => !value.Contains(candidate)))\n            {\n                return candidate.ToString();\n            }\n        }\n\n        throw new FormatException(\"Unable to protect the byte-size unit token.\");\n    }\n\n    static string ReplaceFormatToken(string format, string token, string replacement)\n    {\n        var maskedFormat = MaskFormatLiterals(format);\n        return ReplaceOrdinalIgnoreCase(format, maskedFormat, token, replacement);\n    }\n\n    static string ReplaceOrdinalIgnoreCase(string value, string oldValue, string newValue) =>\n        ReplaceOrdinalIgnoreCase(value, value, oldValue, newValue);\n\n    static string ReplaceOrdinalIgnoreCase(string value, string searchValue, string oldValue, string newValue)\n    {\n        var searchStart = 0;\n        var index = CultureInfo.InvariantCulture.CompareInfo.IndexOf(\n            searchValue,\n            oldValue,\n            searchStart,","sourceCodeStart":1115,"sourceCodeEnd":1151,"githubUrl":"https://github.com/Humanizr/Humanizer/blob/ffc2b77c0f30d2fb176875841424379319d0ae9b/src/Humanizer/Bytes/ByteSize.cs#L1115-L1151","documentation":"Thrown by ByteSize.FindFormatSentinel when no character in the Unicode Private Use Area (U+E000..U+F8FF) is absent from all the supplied values. The library uses a PUA character as a temporary sentinel to protect unit tokens during format substitution; if every PUA code point already appears in the inputs, no sentinel can be chosen.","triggerScenarios":"Calling a format/rendering path that invokes FindFormatSentinel with strings that collectively contain all 6400 Private Use Area characters. In practice this only happens when the format string or unit payload is itself constructed from PUA-heavy data (custom icon fonts, emoji-style ligatures stored in the PUA).","commonSituations":"Embedding icon-font glyphs (U+E000-U+F8FF range) into byte-size format strings, or piping PUA-tagged telemetry through a formatter that builds its format dynamically from such data. Extremely rare in normal usage.","solutions":["Strip Private Use Area characters from the format string before formatting the ByteSize.","Avoid routing arbitrary UI/icon-font text through ByteSize.ToString; format the number first, then concatenate icons.","If unavoidable, pre-process the input to remap PUA characters to a safe range."],"exampleFix":"// before\nvar text = size.ToString(formatWithIconGlyphs);\n\n// after\nvar safeFormat = new string(formatWithIconGlyphs.Where(c => c < '\\uE000' || c > '\\uF8FF').ToArray());\nvar text = size.ToString(safeFormat);","handlingStrategy":"validation","validationCode":"static string StripPrivateUseArea(string format) =>\n    new string(format.Where(c => c < '\\uE000' || c > '\\uF8FF').ToArray());","typeGuard":null,"tryCatchPattern":"try { return size.ToString(format); }\ncatch (FormatException) { throw new ArgumentException(\"Format contains too many Private Use Area characters.\", nameof(format)); }","preventionTips":["Keep icon-font glyphs out of ByteSize format strings","Format the number first, then concatenate UI glyphs","Pre-process PUA-heavy input before formatting"],"tags":["bytesize","format-string","unicode","internal-limit"],"backgroundTag":null,"analyzedSha":"ffc2b77c0f30d2fb176875841424379319d0ae9b","analyzedAt":"2026-08-13T21:42:34.584Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}