From 09a846ba71ca261fdd28489c13f376a1b64c2e46 Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 23 Aug 2020 10:25:14 +0000 Subject: [PATCH] * Merging revisions r46412,r46413 from trunk: ------------------------------------------------------------------------ r46412 | michael | 2020-08-13 12:14:13 +0200 (Thu, 13 Aug 2020) | 1 line * Avoid reallocating string when parsing unicode char (bug ID 0037562) ------------------------------------------------------------------------ r46413 | michael | 2020-08-13 12:15:19 +0200 (Thu, 13 Aug 2020) | 1 line * Fix memleak ------------------------------------------------------------------------ git-svn-id: branches/fixes_3_2@46639 - --- packages/fcl-json/src/fpjson.pp | 32 ++++++++++++++++++++++++++--- packages/fcl-json/src/jsonparser.pp | 13 ++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/packages/fcl-json/src/fpjson.pp b/packages/fcl-json/src/fpjson.pp index 3fbf9a11cd..03d7db41f5 100644 --- a/packages/fcl-json/src/fpjson.pp +++ b/packages/fcl-json/src/fpjson.pp @@ -939,10 +939,35 @@ begin Raise EConvertError.Create('Invalid JSON String:'+S); end; {$ELSE} + + function BufferHexToInt(P : PAnsiChar): integer; + var + N, i: integer; + ch: char; + begin + Result:= 0; + for i:= 1 to 4 do + begin + ch:= p^; + case ch of + '0'..'9': + N:= Ord(ch)-Ord('0'); + 'a'..'f': + N:= Ord(ch)-(Ord('a')-10); + 'A'..'F': + N:= Ord(ch)-(Ord('A')-10); + else + exit(-1); + end; + Inc(P); + Result:= Result*16+N; + end; + end; + Var I,J,L,U1,U2 : Integer; - App,W : String; + App : String; Procedure MaybeAppendUnicode; @@ -982,9 +1007,10 @@ begin 'f' : App:=#12; 'r' : App:=#13; 'u' : begin - W:=Copy(S,I+1,4); + U2:=BufferHexToInt(PAnsiChar(@S[I+1])); + if U2=-1 then + Raise EJSON.Create('Invalid unicode hex code: '+Copy(S,I+1,4)); Inc(I,4); - u2:=StrToInt('$'+W); if (U1<>0) then begin App:={$IFDEF FPC_HAS_CPSTRING}UTF8Encode({$ENDIF}WideChar(U1)+WideChar(U2){$IFDEF FPC_HAS_CPSTRING}){$ENDIF}; diff --git a/packages/fcl-json/src/jsonparser.pp b/packages/fcl-json/src/jsonparser.pp index 8c51eae9da..2d9baed596 100644 --- a/packages/fcl-json/src/jsonparser.pp +++ b/packages/fcl-json/src/jsonparser.pp @@ -129,8 +129,17 @@ begin // Add to existing structural type if (FStruct is TJSONObject) then begin - if (Not (joIgnoreDuplicates in options)) or (TJSONObject(FStruct).IndexOfName(FKey)=-1) then - TJSONObject(FStruct).Add(FKey,AValue); + if (Not (joIgnoreDuplicates in options)) then + try + TJSONObject(FStruct).Add(FKey,AValue); + except + AValue.Free; + Raise; + end + else if (TJSONObject(FStruct).IndexOfName(FKey)=-1) then + TJSONObject(FStruct).Add(FKey,AValue) + else + AValue.Free; FKey:=''; end else if (FStruct is TJSONArray) then