{"record":{"id":"9981f36224d9c578","repo":"antonmedv/fx","slug":"invalid-escape-sequence-c","errorCode":null,"errorMessage":"Invalid escape sequence '\\%c'","messagePattern":"Invalid escape sequence '\\\\%c'","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/jsonx/json.go","lineNumber":231,"sourceCode":"\t\t\tescaped = false\n\t\t\tif p.strict {\n\t\t\t\tswitch p.char {\n\t\t\t\tcase 'u':\n\t\t\t\t\tvar s string\n\t\t\t\t\tfor i := 0; i < 4; i++ {\n\t\t\t\t\t\tp.next()\n\t\t\t\t\t\tif !utils.IsHexDigit(p.char) {\n\t\t\t\t\t\t\tpanic(fmt.Sprintf(\"Invalid Unicode escape sequence '\\\\u%s%c'\", s, p.char))\n\t\t\t\t\t\t}\n\t\t\t\t\t\ts += string(p.char)\n\t\t\t\t\t}\n\t\t\t\t\t_, err := strconv.ParseInt(s, 16, 32)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\tpanic(fmt.Sprintf(\"Invalid Unicode escape sequence '\\\\u%s'\", s))\n\t\t\t\t\t}\n\t\t\t\tcase '\"', '\\\\', '/', 'b', 'f', 'n', 'r', 't':\n\t\t\t\tdefault:\n\t\t\t\t\tpanic(fmt.Sprintf(\"Invalid escape sequence '\\\\%c'\", p.char))\n\t\t\t\t}\n\t\t\t}\n\t\t} else if p.char == '\\\\' {\n\t\t\tescaped = true\n\t\t} else if p.char == '\"' {\n\t\t\tbreak\n\t\t} else if p.char == 0 {\n\t\t\tpanic(\"Unexpected end of input in string\")\n\t\t} else if rune(p.char) > unicode.MaxRune {\n\t\t\tpanic(fmt.Sprintf(\"Invalid character code point %d in string\", p.char))\n\t\t}\n\t\tp.next()\n\t}\n\n\tstr := string(p.data[start:p.end])\n\tp.next()\n\n\treturn str","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/antonmedv/fx/blob/4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe/internal/jsonx/json.go#L213-L249","documentation":"In strict mode, scanString encountered a backslash escape whose following character is not one of the JSON-legal escapes (\" \\ / b f n r t u). The library rejects the document because such escapes are invalid JSON.","triggerScenarios":"Parsing a string like \"bad \\x41 escape\" or \"C:\\temp\\new\" with strict parsing enabled; single-quoted or template strings pasted into JSON; Windows paths embedded without doubling backslashes.","commonSituations":"Windows file paths in config files (\"C:\\Users\\name\"), regex patterns copied unescaped, hand-written JSON where \\a or \\x was used, shell-generated JSON with improper quoting.","solutions":["Double the backslashes in the input (\"C:\\\\Users\\\\name\") or use forward slashes","Remove the invalid escape or replace it with the correct one (\\n for newline, \\t for tab, etc.)","Serialize the string with encoding/json Marshal instead of concatenating it manually","Disable strict mode if you control the parser options and only need lenient scanning (escapes then pass through unvalidated)"],"exampleFix":"// before\n{\"path\": \"C:\\temp\\file\"}\n// after\n{\"path\": \"C:\\\\temp\\\\file\"}","handlingStrategy":"validation","validationCode":"// Reject invalid JSON escapes before parsing\nvalid := `\"\\\\/bfnrtu`\ns := string(b)\nfor i := 0; i < len(s); i++ {\n\tif s[i] == '\\\\' && i+1 < len(s) && !strings.ContainsRune(valid, rune(s[i+1])) {\n\t\treturn fmt.Errorf(\"invalid escape at offset %d\", i)\n\t}\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n\tif r := recover(); r != nil {\n\t\tif msg, ok := r.(string); ok && strings.Contains(msg, \"Invalid escape sequence\") {\n\t\t\terr = errors.New(\"string contains a non-JSON backslash escape\")\n\t\t}\n\t}\n}()","preventionTips":["Always double backslashes in Windows paths (C:\\\\dir) or use forward slashes","Build JSON with marshaling libraries rather than string concatenation","Escape regex and shell strings before embedding them in JSON","Enable strict-mode unit tests on representative fixtures"],"tags":["json","escape-sequence","syntax-error","strict-mode"],"backgroundTag":"invalid-escape-sequence","analyzedSha":"4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe","analyzedAt":"2026-09-02T02:17:47.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}