{"record":{"id":"b2e69b7a30512ace","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"string-contains-invalid-characters-data","errorCode":null,"errorMessage":"String contains invalid characters. Data: ","messagePattern":"String contains invalid characters\\. Data: ","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Extensions/StringExtensions.cs","lineNumber":427,"sourceCode":"\n            const string pattern = @\"(?<=\\w)(?=[A-Z])\";\n            baseStr = Regex.Replace(baseStr.ToPascalCase(), pattern, \" \", RegexOptions.None);\n            return baseStr.Substring(0, 1).ToUpperInvariant() + baseStr.Substring(1);\n        }\n\n        /// <summary>\n        /// Safe version of normalize that doesn't crash on invalid code points in string.\n        /// Instead the points are replaced with question marks.\n        /// </summary>\n        public static string SafeNormalize(this string input, NormalizationForm normalizationForm = NormalizationForm.FormC)\n        {\n            try\n            {\n                return StringTools.ReplaceNonCharacters(input, '?').Normalize(normalizationForm);\n            }\n            catch (ArgumentException e)\n            {\n                throw new InvalidDataException(\"String contains invalid characters. Data: \" + Encoding.UTF32.GetBytes(input).ToHexString(), e);\n            }\n        }\n\n        #endregion Methods\n    }\n}","sourceCodeStart":409,"sourceCodeEnd":433,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Extensions/StringExtensions.cs#L409-L433","documentation":"SafeNormalize pre-cleans the string by replacing non-characters with '?' before calling Normalize, but if Normalize still throws ArgumentException the input contains code points the requested NormalizationForm cannot process. The rethrown InvalidDataException embeds the UTF-32 hex dump of the input to aid diagnosis.","triggerScenarios":"Input containing lone surrogate fragments, unassigned code points, or combining sequences invalid for the requested form; binary data treated as a string.","commonSituations":"Reading malformed text from external sources; concatenating strings of mixed encodings; corrupt database fields; importing data with broken surrogates.","solutions":["Sanitize or strip invalid code points before normalizing.","Catch InvalidDataException and fall back to a lossy cleaning (e.g. replace offenders with '?').","Verify and correct the source encoding before normalization."],"exampleFix":"// before\nvar n = input.SafeNormalize();\n// after\nstring n;\ntry { n = input.SafeNormalize(); }\ncatch (InvalidDataException) { n = StringTools.ReplaceNonCharacters(input, '?').Normalize(); }","handlingStrategy":"try-catch","validationCode":"// Strip suspect code points before normalising\ninput = StringTools.ReplaceNonCharacters(input, '?');","typeGuard":null,"tryCatchPattern":"try { return input.SafeNormalize(form); }\ncatch (InvalidDataException) { return StringTools.ReplaceNonCharacters(input, '?').Normalize(form); }","preventionTips":["Validate string code points from untrusted sources before normalising.","Use ReplaceNonCharacters as a pre-clean step."],"tags":["string","unicode","normalization"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}