Skip to content

Commit

Permalink
avoid memory leak in ZlibReader
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtThiemann committed Nov 13, 2023
1 parent 6b2a928 commit b3f34ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Chunk/AnvilChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,12 @@ public function getGlobalYPos(): int
{
return $this->regionPosition[1] * 32 + $this->yPos;
}

/**
* @return void
*/
public function close(): void
{
$this->zlibReader->reset();
}
}
5 changes: 5 additions & 0 deletions src/Chunk/ChunkInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ public function save(): void;
* @return bool
*/
public function isSaved(): bool;

/**
* @return void
*/
public function close(): void;
}
5 changes: 5 additions & 0 deletions src/Reader/ReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public function tell(): int;
* @return bool
*/
public function eof(): bool;

/**
* @return void
*/
public function reset(): void;
}
11 changes: 11 additions & 0 deletions src/Reader/ZlibReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ public function rewind(): void
$this->pointer = 0;
}

/**
* @inheritDoc
*/
public function reset(): void
{
$this->data = '';
$this->pointer = 0;
$this->resourcePointer = $this->offset;
$this->inflateContext = inflate_init($this->compression);
}

public function eof(): bool
{
return ($this->resourcePointer >= $this->offset + $this->length || feof($this->resource))
Expand Down
1 change: 1 addition & 0 deletions src/Task/RegionTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function __construct(
} else {
$removedChunks++;
}
$chunk->close();
}
$region->save();
return $removedChunks;
Expand Down

0 comments on commit b3f34ca

Please sign in to comment.