Skip to content

Commit 54ac181

Browse files
author
ronierys2@hotmail.com
committed
Correções
Correção de memleak nas classes JSON do REST Dataware nativas. Correção para não passar na validação quando a requisição for do CORS / RequestType = rtOption.
1 parent b4263cb commit 54ac181

3 files changed

Lines changed: 88 additions & 48 deletions

File tree

CORE/Source/Basic/uRESTDWBasic.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2887,7 +2887,7 @@ procedure TRESTClientPoolerBase.SetIpVersion(IpV: TRESTDWClientIpVersions);
28872887
vToken,
28882888
vErrorCode, vErrorMessage, vAcceptAuth);
28892889

2890-
If Not vAcceptAuth Then
2890+
If Not vAcceptAuth and not ((vCORS) And (RequestType = rtOption)) Then //Roniery
28912891
Begin
28922892
//Eloy
28932893
if vAuthenticator is TRESTDWAuthBasic then

CORE/Source/utils/JSON/uRESTDWJSON.pas

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ function JSONTokener.nextValue: TZAbstractObject;
768768
c, b : char;
769769
s , sb: string;
770770
begin
771+
result := Nil;
771772
c := nextClean();
772773

773774
case (c) of
@@ -781,8 +782,13 @@ function JSONTokener.nextValue: TZAbstractObject;
781782
exit;
782783
end;
783784
'[': begin
785+
b := nextClean();
784786
back();
785-
result := TJSONArray.create(self);
787+
back();
788+
if b <> ']' then
789+
result := TJSONArray.create(self)
790+
Else
791+
c := nextClean();
786792
exit;
787793
end;
788794
end;
@@ -844,6 +850,9 @@ function JSONTokener.nextValue: TZAbstractObject;
844850
16));
845851
exit;
846852
Except
853+
on e:Exception do begin
854+
///* Ignore the error */
855+
end;
847856
end;
848857
end else begin
849858
If Not((Pos(',', s) > 0) or (Pos('.', s) > 0)) Then
@@ -853,12 +862,18 @@ function JSONTokener.nextValue: TZAbstractObject;
853862
8));
854863
exit;
855864
Except
865+
on e:Exception do begin
866+
///* Ignore the error */
867+
end;
856868
end;
857869
End;
858870
try
859871
result := _Double.create(s);
860872
exit;
861873
Except
874+
on e:Exception do begin
875+
///* Ignore the error */
876+
end;
862877
end;
863878
end;
864879
end;
@@ -871,12 +886,18 @@ function JSONTokener.nextValue: TZAbstractObject;
871886
result := _Int64.create(s);
872887
exit;
873888
Except
889+
on e:Exception do begin
890+
///* Ignore the error */
891+
end;
874892
end;
875893
End;
876894
try
877895
result := _Double.create(s);
878896
exit;
879897
Except
898+
on e:Exception do begin
899+
///* Ignore the error */
900+
end;
880901
end;
881902
end;
882903
result := _String.create(s);
@@ -1023,7 +1044,8 @@ constructor TJSONObject.create(x: JSONTokener);
10231044
with x.nextValue() do
10241045
begin
10251046
key := toString();
1026-
Free; //Fix memory leak. By creation_zy
1047+
If Assigned(Self) Then
1048+
Free; //Fix memory leak. By creation_zy
10271049
end;
10281050
end
10291051
end; //fim do case
@@ -1057,7 +1079,7 @@ constructor TJSONObject.create(x: JSONTokener);
10571079
exit;
10581080
end
10591081
else begin
1060-
raise x.syntaxError('Expected a "," or "}"');
1082+
// raise x.syntaxError('Expected a "," or "}"');
10611083
end
10621084
end;
10631085
end; //while
@@ -1303,9 +1325,9 @@ function TJSONObject.keys: TStringList;
13031325
i : integer;
13041326
begin
13051327
result := TStringList.Create;
1306-
for i := 0 to myHashMap.Count -1 do begin
1328+
If myHashMap <> Nil Then
1329+
for i := 0 to myHashMap.Count -1 do
13071330
result.add (myHashMap[i]);
1308-
end;
13091331
end;
13101332

13111333
function TJSONObject.length: integer;
@@ -2258,28 +2280,31 @@ constructor TJSONArray.create(s: string);
22582280
FreeAndNil(token);
22592281
end;
22602282

2261-
destructor TJSONArray.destroy;
2262-
var
2283+
Destructor TJSONArray.destroy;
2284+
Var
22632285
obj : TObject;
2264-
begin
2265-
while myArrayList.Count > 0 do begin
2266-
obj := TObject(myArrayList[0]);
2267-
if (obj <> CONST_FALSE)
2268-
and (obj <> CONST_TRUE)
2269-
and (obj <> CNULL)
2270-
and (Assigned(obj)) then
2271-
Begin
2272-
// {$IFNDEF FPC}
2273-
// Dispose(myArrayList[0]);
2274-
// {$ELSE}
2275-
FreeAndNil(obj);
2276-
// {$ENDIF}
2286+
Begin
2287+
If Assigned(myArrayList) Then
2288+
While myArrayList.Count > 0 do
2289+
Begin
2290+
obj := TObject(myArrayList[0]);
2291+
If (obj <> CONST_FALSE)
2292+
And (obj <> CONST_TRUE)
2293+
And (obj <> CNULL)
2294+
And (Assigned(obj)) Then
2295+
Begin
2296+
Try
2297+
obj.Free;
2298+
Except
2299+
// Dispose(myArrayList[0]);
22772300
End;
2301+
End;
22782302
myArrayList.Delete(0);
2279-
end;
2303+
End;
2304+
If Assigned(myArrayList) Then
22802305
FreeAndNil(myArrayList);
2281-
inherited;
2282-
end;
2306+
Inherited;
2307+
End;
22832308

22842309
(**
22852310
* Get the object value associated with an index.
@@ -3052,13 +3077,16 @@ function TJSONArray.put(value: int64): TJSONArray;
30523077
Begin
30533078
If UpperCase(myHashMap.Objects[0].classname) = 'TJSONARRAY' Then
30543079
Begin
3055-
vTempString := TJSONArray(myHashMap.Objects[0]).toString;
3056-
vTempStringSize := StrDWLength(vTempString);
3057-
If ((vTempString[InitStrPos] = '[') or
3058-
(vTempString[InitStrPos] = '{')) And
3059-
((vTempString[vTempStringSize - FinalStrPos] = ']') or
3060-
(vTempString[vTempStringSize - FinalStrPos] = '}')) Then
3061-
myHashMap.Objects[0].Free;
3080+
If TJSONArray(myHashMap.Objects[0]).length > 0 Then
3081+
Begin
3082+
vTempString := TJSONArray(myHashMap.Objects[0]).toString;
3083+
vTempStringSize := StrDWLength(vTempString);
3084+
If ((vTempString[InitStrPos] = '[') or
3085+
(vTempString[InitStrPos] = '{')) And
3086+
((vTempString[vTempStringSize - FinalStrPos] = ']') or
3087+
(vTempString[vTempStringSize - FinalStrPos] = '}')) Then
3088+
myHashMap.Objects[0].Free;
3089+
End;
30623090
End
30633091
Else
30643092
myHashMap.Objects[0].Free;
@@ -3072,6 +3100,7 @@ function TJSONArray.put(value: int64): TJSONArray;
30723100
Except
30733101
Exit;
30743102
End;
3103+
vTempString := '';
30753104
End;
30763105
End;
30773106
End;

CORE/Source/utils/JSON/uRESTDWJSONInterface.pas

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ implementation
212212

213213
Destructor TRESTDWJSONInterfaceArray.Destroy;
214214
Begin
215+
If Assigned(vJSONObject) Then
216+
FreeAndNil(TJSONBaseClass(vJSONObject));
215217
inherited;
216218
End;
217219

@@ -289,27 +291,24 @@ implementation
289291

290292
Constructor TRESTDWJSONInterfaceObject.Create(JSONValue: String);
291293
Begin
292-
Inherited Create;
293-
If JSONValue <> '' Then
294+
Inherited Create;
295+
If JSONValue <> '' Then
294296
Begin
295-
{$IFDEF RESTDWFMX}
297+
{$IFDEF RESTDWFMX}
296298
If JSONValue[InitStrPos] = '[' then
297-
vJSONObject := TJSONBaseClass(TJSONObject.ParseJSONValue(JSONValue)
298-
as TJSONArray)
299+
vJSONObject := TJSONBaseClass(TJSONObject.ParseJSONValue(JSONValue) as TJSONArray)
299300
Else If JSONValue[InitStrPos] = '{' then
300-
vJSONObject := TJSONBaseClass(TJSONObject.ParseJSONValue(JSONValue)
301-
as TJSONObject)
301+
vJSONObject := TJSONBaseClass(TJSONObject.ParseJSONValue(JSONValue) as TJSONObject)
302302
Else
303-
vJSONObject := TJSONBaseClass(TJSONObject.ParseJSONValue('{}')
304-
as TJSONObject)
305-
{$ELSE}
303+
vJSONObject := TJSONBaseClass(TJSONObject.ParseJSONValue('{}') as TJSONObject)
304+
{$ELSE}
306305
If JSONValue[InitStrPos] = '[' then
307-
vJSONObject := TJSONBaseClass(TJSONArray.Create(JSONValue))
306+
vJSONObject := TJSONBaseClass(TJSONArray.Create(JSONValue))
308307
Else If JSONValue[InitStrPos] = '{' then
309-
vJSONObject := TJSONBaseClass(TJSONObject.Create(JSONValue))
308+
vJSONObject := TJSONBaseClass(TJSONObject.Create(JSONValue))
310309
Else
311-
vJSONObject := TJSONBaseClass(TJSONObject.Create('{}'))
312-
{$ENDIF}
310+
vJSONObject := TJSONBaseClass(TJSONObject.Create('{}'))
311+
{$ENDIF}
313312
End;
314313
End;
315314

@@ -582,13 +581,24 @@ implementation
582581
End
583582
Else If Uppercase(vClassName) = Uppercase('TJSONArray') Then
584583
Begin
585-
If LowerCase(TJSONArray(vJSONObject).Get(index).ClassName)
586-
= LowerCase('_String') Then
584+
If LowerCase(TJSONArray(vJSONObject).Get(index).ClassName) = LowerCase('_String') Then
587585
Begin
588586
result.ClassName := '_String';
589587
result.Name := 'arrayobj' + IntToStr(Index);
590588
result.Value := TJSONArray(vJSONObject).Get(index).toString;
591589
End
590+
Else If LowerCase(TJSONArray(vJSONObject).Get(index).ClassName) = LowerCase('_Integer') Then
591+
Begin
592+
result.ClassName := '_Integer';
593+
result.Name := 'arrayobj' + IntToStr(Index);
594+
result.Value := TJSONArray(vJSONObject).Get(index).toString;
595+
End
596+
Else If LowerCase(TJSONArray(vJSONObject).Get(index).ClassName) = LowerCase('_Double') Then
597+
Begin
598+
result.ClassName := '_Double';
599+
result.Name := 'arrayobj' + IntToStr(Index);
600+
result.Value := TJSONArray(vJSONObject).Get(index).toString;
601+
End
592602
Else
593603
Begin
594604
vClassName := TJSONArray(vJSONObject).optJSONObject(index).ClassName;
@@ -743,7 +753,8 @@ constructor TRESTDWJSONInterfaceBase.Create(ParentJSON: TJSONBaseClass);
743753

744754
Destructor TRESTDWJSONInterfaceBase.Destroy;
745755
Begin
746-
756+
If Assigned(vJSONObject) Then
757+
FreeAndNil(TJSONBaseClass(vJSONObject));
747758
inherited;
748759
End;
749760

0 commit comments

Comments
 (0)