Recent

Author Topic: FPC 3.3.1 Bug Report: fpjson Fails to Parse Backslashes Correctly  (Read 325 times)

EsequiasTorres

  • Newbie
  • Posts: 2
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:

Code: Pascal  [Select][+][-]
  1. program TestJSONBackslash;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils, fpjson, jsonparser;
  7.  
  8. var
  9.   JObject: TJSONObject;
  10.   JParser: TJSONParser;
  11.   JSONString: String;
  12.   ParsedPath: String;
  13. begin
  14.   // A simple JSON object with a Windows path: {"path": "C:\Windows"}
  15.   // In JSON, backslashes must be escaped: "C:\\Windows"
  16.   JSONString := '{"path": "C:\\Windows"}';
  17.  
  18.   WriteLn('Input JSON: ', JSONString);
  19.  
  20.   JParser := TJSONParser.Create(JSONString, [joUTF8]);
  21.   try
  22.     try
  23.       JObject := JParser.Parse as TJSONObject;
  24.       ParsedPath := JObject.Strings['path'];
  25.      
  26.       WriteLn('Parsed Path: ', ParsedPath);
  27.      
  28.       if ParsedPath = 'C:\Windows' then
  29.         WriteLn('RESULT: PASS')
  30.       else
  31.         WriteLn('RESULT: FAIL (Expected "C:\Windows", got "', ParsedPath, '")');
  32.        
  33.     except
  34.       on E: Exception do
  35.         WriteLn('RESULT: EXCEPTION - ', E.Message);
  36.     end;
  37.   finally
  38.     JParser.Free;
  39.     if Assigned(JObject) then JObject.Free;
  40.   end;
  41. end.
  42.  

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)

dsiders

  • Hero Member
  • *****
  • Posts: 1509
Re: FPC 3.3.1 Bug Report: fpjson Fails to Parse Backslashes Correctly
« Reply #1 on: December 29, 2025, 07:51:48 pm »
FPC 3.3.1 Bug Report: fpjson Fails to Parse Backslashes Correctly
...

This forum is not the bug tracker.That is located at https://gitlab.com/freepascal.org/fpc/source/-/issues.

EsequiasTorres

  • Newbie
  • Posts: 2
« Last Edit: December 29, 2025, 08:20:10 pm by EsequiasTorres »

 

TinyPortal © 2005-2018