Skip to content

Bug, Linux: TSocketAPI.Readable #177

@RoLex

Description

@RoLex

Hello.

Posix.SysSelect.select first argument is 0 for Linux, but this is not correct according to manual: https://man7.org/linux/man-pages/man2/select.2.html

       nfds   This argument should be set to the highest-numbered file
              descriptor in any of the three sets, plus 1.
              ...

Posix.SysSelect.select first argument needs to be ASocket + 1 for Linux.

class function TSocketAPI.Readable(const ASocket: TSocket; const ATimeout: Integer): Integer;
var
  {$IFDEF MSWINDOWS}
  LFDSet: TFDSet;
  LTime_val: TTimeval;
  {$ELSE}
  LFDSet: {$IFDEF DELPHI}fd_set{$ELSE}TFDSet{$ENDIF};
  LTime_val: timeval;
  {$ENDIF}
  P: PTimeVal;
begin
  if (ATimeout >= 0) then
  begin
    LTime_val.tv_sec := ATimeout div 1000;
    LTime_val.tv_usec :=  1000 * (ATimeout mod 1000);
    P := @LTime_val;
  end else
    P := nil;

  {$IFDEF MSWINDOWS}
  FD_ZERO(LFDSet);
  FD_SET(ASocket, LFDSet);
  Result := Net.Winsock2.select(0, @LFDSet, nil, nil, P);
  {$ELSE MSWINDOWS}
  {$IFDEF DELPHI}
  FD_ZERO(LFDSet);
  _FD_SET(ASocket, LFDSet);
  // not correct for linux
  //Result := Posix.SysSelect.select(0, @LFDSet, nil, nil, P);
  // correct for linux
  Result := Posix.SysSelect.select(ASocket + 1, @LFDSet, nil, nil, P);
  {$ELSE DELPHI}
  fpFD_ZERO(LFDSet);
  fpFD_SET(ASocket, LFDSet);
  Result := fpSelect(0, @LFDSet, nil, nil, P);
  {$ENDIF DELPHI}
  {$ENDIF MSWINDOWS}
end;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions