Forum > FPC development

Bug regarding function TBufferedFileStream.ReadPageBeforeWrite

(1/1)

lagprogramming:
packages/fcl-base/src/bufstream.pp has the following function:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TBufferedFileStream.ReadPageBeforeWrite: Boolean;var  j: integer;  pCache: PStreamCacheEntry=nil;  lStreamPosition: int64;  lExpectedBytesToRead: integer;  lEffectiveRead: integer;begin  // Find free page entry  for j := 0 to Pred(FStreamCachePageMaxCount) do begin    if not Assigned(FCachePages[j]^.Buffer) then begin      pCache:=FCachePages[j];      FCacheLastUsedPage:=j;      break;    end;  end;  if not Assigned(pCache) then begin    // Free last used page    pCache:=FreeOlderInUsePage(false);  end;  if not Assigned(pCache^.Buffer) then begin    Getmem(pCache^.Buffer,FStreamCachePageSize);  end;  lStreamPosition:=(FCacheStreamPosition div FStreamCachePageSize)*FStreamCachePageSize;  inherited Seek(lStreamPosition,soBeginning);  if (lStreamPosition+FStreamCachePageSize) > FCacheStreamSize then begin    lExpectedBytesToRead:=FCacheStreamSize-lStreamPosition;  end else begin    lExpectedBytesToRead:=FStreamCachePageSize;  end;  pCache^.PageBegin:=lStreamPosition;  pCache^.PageRealSize:=inherited Read(pCache^.Buffer^,FStreamCachePageSize);  if pCache^.PageRealSize<>lExpectedBytesToRead then begin    lEffectiveRead:=pCache^.PageRealSize;    pCache^.IsDirty:=false;    pCache^.LastTick:=0;    pCache^.PageBegin:=0;    pCache^.PageRealSize:=0;    Freemem(pCache^.Buffer);    pCache^.Buffer:=nil;    Raise EStreamError.CreateFmt(SErrCacheUnableToReadExpected,[lExpectedBytesToRead,lEffectiveRead]);  end;  pCache^.LastTick:=GetOpCounter;  Result:=true;end;The function never returns false. Either it returns true, either it raises an exception.
ReadPageBeforeWrite is called by function TBufferedFileStream.DoCacheWrite, which expects boolean results, not exceptions.
This is how DoCacheWrite tries to use the result:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if ReadPageBeforeWrite then begin  Result:=DoCacheWrite(Buffer,Count);end else begin  Result:=0;end;

AlexTP:
Posted to https://gitlab.com/freepascal.org/fpc/source/-/issues/40391

Navigation

[0] Message Index

Go to full version