FPC 3.3.1 fpjson Fails to Parse Backslashes Correctly

FPC 3.3.1 Bug Report: fpjson Fails to Parse Backslashes Correctly

Date: December 29, 2025
Last Verified: December 29, 2025 (Bug still present in FPC 3.3.1-19122-ge8d90a3042)
Affected Version: Free Pascal Compiler 3.3.1 (Trunk)
IDE: Lazarus 4.5
Operating System: Windows 10 Component: FCL-JSON (fpjson, jsonscanner)
Severity: Critical (Breaks compatibility with standard JSON, especially Windows paths)

Description

The fpjson unit in recent FPC 3.3.1 trunk builds fails to parse JSON strings containing escaped backslashes (e.g., "\\") or backslashes used in file paths (e.g., "C:\\Windows").

When parsing a string literal in JSON that contains an escaped backslash, the parser eliminates all content before the last backslash in the string. This behavior represents a regression from FPC 3.2.2/3.2.3, where JSON parsing works as expected.

Verified Bug Pattern (December 2025): The parser removes everything before the final backslash character:

  • Input: "C:\\Windows" → Output: \Windows (Expected: C:\Windows)
  • Input: "A\\B" → Output: \B (Expected: A\B)

Steps to Reproduce

Compile and run the following Pascal program using FPC 3.3.1:

program TestJSONBackslash;

{$mode objfpc}{$H+}

uses
  SysUtils, fpjson, jsonparser;

var
  JObject: TJSONObject;
  JParser: TJSONParser;
  JSONString: String;
  ParsedPath: String;
begin
  // A simple JSON object with a Windows path: {"path": "C:\Windows"}
  // In JSON, backslashes must be escaped: "C:\\Windows"
  JSONString := '{"path": "C:\\Windows"}';

  WriteLn('Input JSON: ', JSONString);

  JParser := TJSONParser.Create(JSONString, [joUTF8]);
  try
    try
      JObject := JParser.Parse as TJSONObject;
      ParsedPath := JObject.Strings['path'];
      
      WriteLn('Parsed Path: ', ParsedPath);
      
      if ParsedPath = 'C:\Windows' then
        WriteLn('RESULT: PASS')
      else
        WriteLn('RESULT: FAIL (Expected "C:\Windows", got "', ParsedPath, '")');
        
    except
      on E: Exception do
        WriteLn('RESULT: EXCEPTION - ', E.Message);
    end;
  finally
    JParser.Free;
    if Assigned(JObject) then JObject.Free;
  end;
end.

Expected Behavior (FPC 3.2.x)

Input JSON: {"path": "C:\\Windows"}
Parsed Path: C:\Windows
RESULT: PASS

Actual Behavior (FPC 3.3.1)

The output varies depending on the exact revision, but typically:

Input JSON: {"path": "C:\\Windows"}
Parsed Path: C:Windows  <-- Missing backslash
RESULT: FAIL

Or in some cases, it may corrupt adjacent characters.

Analysis

The issue appears to be located in jsonscanner.pp or jsonreader.pp within the tokenization logic for string literals. Accessing the raw source code of 3.3.1 reveals changes in how escape sequences are handled compared to 3.2.x.

Workaround

We have confirmed that replacing the FPC 3.3.1 fcl-json units (fpjson.pp, jsonparser.pp, jsonscanner.pp, jsonreader.pp) with the stable versions from FPC 3.2.3 resolves the issue immediately, allowing the application to compile and run correctly under FPC 3.3.1 (with minor interface patches for TJSONOption).

Recommendation

Please review the recent changes to jsonscanner.pp regarding backslash escape handling logic.

Verification History

December 29, 2025 - Bug Still Present

Verified with FPC 3.3.1-19122-ge8d90a3042 that the bug persists. All tests failed:

  • Windows Path test: FAIL (got \Windows instead of C:\Windows)
  • Simple Backslash test: FAIL (got \B instead of A\B)
  • Double Backslash test: FAIL (got \More instead of Test\\More)
  • Path with Spaces test: FAIL (got \Test instead of C:\Program Files\Test)