neslib / Neslib.Json

Fast and memory-efficient JSON for Delphi
Other
79 stars 23 forks source link

EAsertionFailed -> line 1280 Assert((UIntPtr(P) and TYPE_MASK) = 0); #10

Closed vanillamasonry closed 3 years ago

vanillamasonry commented 3 years ago

Hi,

There is a problem Neslib.Json.pas on Linux, Android, and other platforms except Windows

The following lines will raise an Assertion Failed Error (line 1280 - Assert((UIntPtr(P) and TYPE_MASK) = 0); )

FJSONData:=TJsonDocument.CreateDictionary; FJSONData.Root.AddOrSetValue('data','0'); <- will raise ASSERTION ERROR only if the VALUE IS one character long ON Android, Linux ... EXCEPT Windows

and in Neslib.Json

class function TJsonValue.Create(const AValue: JsonString): TJsonValue; var P: Pointer; begin P := nil; JsonString(P) := AValue; // Increases ref count Assert((UIntPtr(P) and TYPE_MASK) = 0); <- here Result.FBits := TYPE_STR or UIntPtr(P); end;

NOTE:

  1. I use delphi 10.4.2, http://docwiki.embarcadero.com/RADStudio/Sydney/en/Zero-based_strings_(Delphi)

(Workaround)

  1. If I Typecast AValue to PChar the APP is working JsonString(P) := PChar(AValue); // Increases ref count This change will impact the code in any other place?

Thank you, Paul

neslib commented 3 years ago

Thanks for reporting this issue! Unfortunately, your workaround didn't fixed the issue. It could return invalid data when requesting the value later, and also result in a memory leak. A added a fix that should solve the issue. It works in my tests. Feel free to reopen this issue if it doesn't work for you.