Bug in IdSSLOpenSSL.pas; fix included

Giganews Newsgroups
Subject: Bug in IdSSLOpenSSL.pas; fix included
Posted by:  Nando Dessena (desse…@sedoc.it)
Date: Wed, 12 Jan 2005

Hello,
I am using Indy 9.0.18 on Delphi 7, and I have run into a reproducible
error that shows up as "error creating SSL context". Steps:

1- dynamically load the Indy package (through LoadPackage); the
initialization section of IdSSLOpenSSL.pas is executed, but all code is
commented out in there, so no-op:

initialization
  // Let's load the library    {Do not Localize}
  //if DLLLoadCount <= 0 then begin
  {
      if not LoadOpenSLLibrary then begin
        raise EIdException.Create(RSOSSLCouldNotLoadSSLLibrary);
    end;
  }
  //end;
  //Inc(DLLLoadCount);

2- use it (f. ex. make an HTTPS connection through TIdHTTP +
TIdSSLIOHandlerSocket).

3- unload the Indy package (through UnloadPackage); the finalization
section of IsSSLOpenSSL.pas is executed, and it unconditionally unloads
the SSL libraries:

finalization
  // if DLLLoadCount = 0 then begin
  UnLoadOpenSLLibrary;
  // end;

4- goto 1: you'll get the aforementioned error at step 2.

The exception is thrown by TIdSSLContext.Create, which does:

constructor TIdSSLContext.Create;
begin
  inherited Create;

  if DLLLoadCount <= 0 then begin
    if not IdSSLOpenSSL.LoadOpenSLLibrary then begin
      raise EIdOSSLCouldNotLoadSSLLibrary.Create
        (RSOSSLCouldNotLoadSSLLibrary);
    end;
  end;
  Inc(DLLLoadCount);

  ...

DLLLoadCount is a global variable that is incremented only here and never
decremented. Unloading the package apparently does not reset it. No wonder
that once you unload the SSL libraries with the Indy package they are
never reloaded anymore and thus you get the error. The use of DLLLoadCount
is apparently half-baked, since all the code using that variable is
commented out *except* the portion above.

I see two solutions:

a) restore the use of the counter: increment it in LoadOpenSLLibrary,
decrement it in UnLoadOpenSLLibrary, and never ever look at it from
outside those two functions.

b) comment out the use of the variable in the code above:

  //if DLLLoadCount <= 0 then begin
      if not IdSSLOpenSSL.LoadOpenSLLibrary then begin
        raise EIdOSSLCouldNotLoadSSLLibrary.Create
        (RSOSSLCouldNotLoadSSLLibrary);
    end;
  //end;
  //Inc(DLLLoadCount);

then you can comment out the variable's declaration as well.

I have applied b) for now and it works for me.

Any chance to see this fixed in an upcoming 9.0.19 maintenance release?
I'd hate having to maintain a private fork.

Thanks

--
Nando

Replies