{"id":"3290cfe83a0d1514","repo":"jackc/pgx","slug":"invalid-backslash","errorCode":null,"errorMessage":"invalid backslash","messagePattern":"invalid backslash","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgconn/config.go","lineNumber":743,"sourceCode":"\t\teqIdx := strings.IndexRune(s, '=')\n\t\tif eqIdx < 0 {\n\t\t\treturn nil, errors.New(\"invalid keyword/value\")\n\t\t}\n\n\t\tkey = strings.Trim(s[:eqIdx], \" \\t\\n\\r\\v\\f\")\n\t\ts = strings.TrimLeft(s[eqIdx+1:], \" \\t\\n\\r\\v\\f\")\n\t\tswitch {\n\t\tcase len(s) == 0:\n\t\tcase s[0] != '\\'':\n\t\t\tend := 0\n\t\t\tfor ; end < len(s); end++ {\n\t\t\t\tif asciiSpace[s[end]] == 1 {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tif s[end] == '\\\\' {\n\t\t\t\t\tend++\n\t\t\t\t\tif end == len(s) {\n\t\t\t\t\t\treturn nil, errors.New(\"invalid backslash\")\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tval = strings.ReplaceAll(strings.ReplaceAll(s[:end], \"\\\\\\\\\", \"\\\\\"), \"\\\\'\", \"'\")\n\t\t\t// Consume the value and trim any subsequent whitespace so that\n\t\t\t// multiple trailing spaces don't cause a spurious parse failure.\n\t\t\ts = strings.TrimLeft(s[end:], \" \\t\\n\\r\\v\\f\")\n\t\tdefault: // quoted string\n\t\t\ts = s[1:]\n\t\t\tend := 0\n\t\t\tfor ; end < len(s); end++ {\n\t\t\t\tif s[end] == '\\'' {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tif s[end] == '\\\\' {\n\t\t\t\t\tend++\n\t\t\t\t}\n\t\t\t}","sourceCodeStart":725,"sourceCodeEnd":761,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgconn/config.go#L725-L761","documentation":"Returned by parseKeywordValueSettings while parsing an UNQUOTED value: a backslash is encountered as the last character of the string with nothing following it. The parser increments past the backslash expecting an escaped character and hits end-of-string.","triggerScenarios":"A keyword/value string whose unquoted value ends with a lone backslash, e.g. 'host=localhost\\' or 'password=ab\\'. The escape sequence is incomplete.","commonSituations":"Shell/env-var interpolation leaving a trailing backslash; mis-escaping of Windows paths or regex-like values in conninfo.","solutions":["Quote the value and escape properly, or remove the trailing backslash.","Double the backslash to represent a literal backslash: 'host=localhost\\\\'.","Prefer a URL-style connection string or a *ConnConfig struct to avoid conninfo escaping pitfalls."],"exampleFix":"// before\npgx.ParseConfig(`host=localhost\\`)\n\n// after (literal backslash)\npgx.ParseConfig(`host=localhost\\\\`)","handlingStrategy":"validation","validationCode":"// Reject conninfo values ending in a lone backslash before parsing.\nfunc checkTrailingBackslash(s string) error {\n    if strings.HasSuffix(strings.TrimSpace(s), \"\\\\\") && !strings.HasSuffix(strings.TrimSpace(s), \"\\\\\\\\\") {\n        return errors.New(\"connection string value ends with a lone backslash\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if _, err := pgx.ParseConfig(connStr); err != nil {\n    if strings.Contains(err.Error(), \"invalid backslash\") {\n        return fmt.Errorf(\"connection string has an incomplete backslash escape: %w\", err)\n    }\n}","preventionTips":["Quote values containing special characters and escape backslashes as \\\\.","Avoid interpolating raw env vars into conninfo; sanitize first.","Use a *ConnConfig or URL DSN to sidestep conninfo escaping."],"tags":["config","connection-string","parsing","escaping"],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}