Indy 9.18: IdGlobal.PosIdx possible issue (repost)

Giganews Newsgroups
Subject: Indy 9.18: IdGlobal.PosIdx possible issue (repost)
Posted by:  ilya ovsishcher (iomong…@nobody.com)
Date: Fri, 28 Jan 2005

Hi,

No reposnse so far, neither from borland,, neither from Indy team.

Story: POP3 component was returning Access Violation while geting one
particular email (POP.Retrieve).

I went as deep as I can and I did find out that issue is in PosIdx. Please
check comments
and changes below. For now I have compiler warning, but application doesn'
fail
and capable of downloading email the same way as Outlook Express or any
other tool. Please let me know if it is a bug and if yes, please spank coder
a few times.

Function  PosIdx (const ASubStr,AStr: AnsiString; AStartPos: cardinal):
Cardinal;
var
  lpSubStr,lpS: PChar;
  //IO was  LenSubStr,LenS: cardinal;
  //IO changed to longint, below  Dec(LenS) done. if LenS=0 and Dec is done
it will be huge positive number = Access Violation
  LenSubStr,LenS: longint;
  LChar: Char;
Begin
  Result := 0; //not found

  LenSubStr := Length(ASubStr);
  LenS := Length(AStr);

  if (LenSubStr = 0) or (LenSubStr > LenS) or (AStartPos > LenS) then begin
    Exit;
  end;//if

  lpSubStr := Pointer(ASubStr);
  lpS := Pointer(AStr);

  if AStartPos > 1 then begin
    Inc(lpS, AStartPos - 1);
    Dec(LenS, AStartPos - 1);
  end;//if
  if LenS <= 0 then begin
    Exit;
  end;

  LChar := lpSubStr[0]; //first char
  Inc(lpSubStr); //next char
  Dec(LenSubStr); //len w/o first char

  Dec(LenS, LenSubStr); //Length(S)-Length(SubStr) +1(!) MUST BE >0
  if LenS <= 0 then begin
    Exit;
  end;//if

  // RLebeau - shouldn't this code use StrPos() instead?

  while LenS > 0 do begin
    if lpS^ = LChar then begin
      Inc(lpS);
      if CompareMem(lpS, lpSubStr, LenSubStr) then begin
        Result := lpS-Pointer(AStr); //+1 already here
        Exit;
      end;
    end else begin
      Inc(lpS);
    end;
    Dec(LenS);
  end;//while
End;//PosIdx

I understand that that particular email header was miformed, but in the same
time doing Dec for cardinal variuable and check if varuable <0 is not making
much sense either.

All the best,
ilya

Replies