Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2024-02

[fcl-web] [jsonrpc] [fpjsonrpc.pp] DoCheckParamArray in TCustomJSONRPCHandler

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Anochin

Hello,

the TCustomJSONRPCHandler.DoCheckParamArray in fpjsonrpc.pp causes an exception with the message "List index (0) out of bounds".

This happens, for example, if ParamArray is empty and there are not required parameter in ParamDef (required not set). In that case ParamArray[i] not exists, so Param:=ParamArray[i] raises "index out of bounds" exception.

I would like to check this before assignment, may be like this

procedure TCustomJSONRPCHandler.DoCheckParamArray(const ParamArray:
TJSONArray);
var
   I : Integer;
   Param: TJSONData;
   Def : TJSONParamDef;
begin
   for I:=0 to ParamDefs.Count-1 do
     begin
     Def:=ParamDefs[i];
     if I>=ParamArray.Count then
       if ParamDefs[i].Required then
         JSONRPCParamError(SErrParamsRequiredParamNotFound,[def.Name]);
     //Check ParamArray.Count first
     if (I<ParamArray.Count) then begin //<< added
       Param:=ParamArray[i];
       // jtUnkown accepts all data types
       if (def.DataTypejtUnknown) and not
(Param.JSONType=def.DataType) then
JSONRPCParamError(SErrParamsDataTypeMismatch,[def.Name,JSONTypeName(def.DataType),JSONTypeName(Param.JSONType)]);
       end;
     end;  //<< added
end;

Reply by email

Michael Van Canneyt
to Michael Anochin
Michael Anochin wrote 34 lines
Hello, the TCustomJSONRPCHandler.DoCheckParamArray in fpjsonrpc.pp causes an exception with the message "List index (0) out of bounds". This happens, for example, if ParamArray is empty and there are not required parameter in ParamDef (required not set). In that case ParamArray[i] not exists, so Param:=ParamArray[i] raises "index out of bounds" exception. I would like to check this before assignment, may be like this procedure TCustomJSONRPCHandler.DoCheckParamArray(const ParamArray: TJSONArray); var I : Integer; Param: TJSONData; Def : TJSONParamDef; begin for I:=0 to ParamDefs.Count-1 do begin Def:=ParamDefs[i]; if I>=ParamArray.Count then if ParamDefs[i].Required then JSONRPCParamError(SErrParamsRequiredParamNotFound,[def.Name]); //Check ParamArray.Count first if (I<ParamArray.Count) then begin //<< added Param:=ParamArray[i]; // jtUnkown accepts all data types if (def.DataTypejtUnknown) and not (Param.JSONType=def.DataType) then JSONRPCParamError(SErrParamsDataTypeMismatch,[def.Name,JSONTypeName(def.DataType),JSONTypeName(Param.JSONType)]);

As it happens, I was working on adding some things to fpjsonrpc, so I fixed this issue by putting the code in an else branch. I pushed that change.

Michael.

Reply by email