mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-06 03:47:16 +00:00
Sign: add validity checks on text encoding, scrub invalid UTF-8 on load
this works around a bug where corrupted text on preexisting signs can mess up the client. This also prevents corrupted text getting onto signs in the future by having them scrubbed and validated before applying them.
This commit is contained in:
parent
65529ff2ce
commit
44697e784a
@ -33,6 +33,8 @@ use function array_pad;
|
||||
use function array_slice;
|
||||
use function explode;
|
||||
use function implode;
|
||||
use function mb_check_encoding;
|
||||
use function mb_scrub;
|
||||
use function sprintf;
|
||||
|
||||
class Sign extends Spawnable{
|
||||
@ -57,6 +59,9 @@ class Sign extends Spawnable{
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->text = array_map(function(string $line) : string{
|
||||
return mb_scrub($line, 'UTF-8');
|
||||
}, $this->text);
|
||||
}
|
||||
|
||||
protected function writeSaveData(CompoundTag $nbt) : void{
|
||||
@ -79,16 +84,16 @@ class Sign extends Spawnable{
|
||||
*/
|
||||
public function setText(?string $line1 = "", ?string $line2 = "", ?string $line3 = "", ?string $line4 = "") : void{
|
||||
if($line1 !== null){
|
||||
$this->text[0] = $line1;
|
||||
$this->setLine(0, $line1, false);
|
||||
}
|
||||
if($line2 !== null){
|
||||
$this->text[1] = $line2;
|
||||
$this->setLine(1, $line2, false);
|
||||
}
|
||||
if($line3 !== null){
|
||||
$this->text[2] = $line3;
|
||||
$this->setLine(2, $line3, false);
|
||||
}
|
||||
if($line4 !== null){
|
||||
$this->text[3] = $line4;
|
||||
$this->setLine(3, $line4, false);
|
||||
}
|
||||
|
||||
$this->onChanged();
|
||||
@ -103,6 +108,9 @@ class Sign extends Spawnable{
|
||||
if($index < 0 or $index > 3){
|
||||
throw new \InvalidArgumentException("Index must be in the range 0-3!");
|
||||
}
|
||||
if(!mb_check_encoding($line, 'UTF-8')){
|
||||
throw new \InvalidArgumentException("Text must be valid UTF-8");
|
||||
}
|
||||
|
||||
$this->text[$index] = $line;
|
||||
if($update){
|
||||
|
Loading…
x
Reference in New Issue
Block a user