Paper/patches/server/0351-Guard-against-serializing-mismatching-chunk-coordina.patch

52 lines
3.3 KiB
Diff
Raw Normal View History

2021-06-13 21:29:58 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Fri, 27 Dec 2019 09:42:26 -0800
Subject: [PATCH] Guard against serializing mismatching chunk coordinate
Should help if something dumb happens
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkSerializer.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkSerializer.java
2021-11-24 08:37:09 +01:00
index 992a7d6c4372942711a1381ac08ee13910a62cb0..37855d2183212cb043b30418740c0fd047dbc07f 100644
2021-06-13 21:29:58 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkSerializer.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkSerializer.java
2021-11-24 08:37:09 +01:00
@@ -75,6 +75,12 @@ public class ChunkSerializer {
2021-06-13 21:29:58 +02:00
public ChunkSerializer() {}
+ // Paper start - guard against serializing mismatching coordinates
+ // TODO Note: This needs to be re-checked each update
+ public static ChunkPos getChunkCoordinate(CompoundTag chunkData) {
2021-11-24 08:37:09 +01:00
+ return new ChunkPos(chunkData.getInt("xPos"), chunkData.getInt("zPos"));
2021-06-13 21:29:58 +02:00
+ }
+ // Paper end
// Paper start
public static final class InProgressChunkHolder {
2021-11-24 08:37:09 +01:00
@@ -100,7 +106,7 @@ public class ChunkSerializer {
public static InProgressChunkHolder loadChunk(ServerLevel world, PoiManager poiStorage, ChunkPos chunkPos, CompoundTag nbt, boolean distinguish) {
java.util.ArrayDeque<Runnable> tasksToExecuteOnMain = new java.util.ArrayDeque<>();
2021-06-13 21:29:58 +02:00
// Paper end
2021-11-24 08:37:09 +01:00
- ChunkPos chunkcoordintpair1 = new ChunkPos(nbt.getInt("xPos"), nbt.getInt("zPos"));
+ ChunkPos chunkcoordintpair1 = new ChunkPos(nbt.getInt("xPos"), nbt.getInt("zPos")); // Paper - diff on change, see ChunkSerializer#getChunkCoordinate
2021-06-13 21:29:58 +02:00
2021-11-24 08:37:09 +01:00
if (!Objects.equals(chunkPos, chunkcoordintpair1)) {
ChunkSerializer.LOGGER.error("Chunk file at {} is in the wrong location; relocating. (Expected {}, got {})", chunkPos, chunkPos, chunkcoordintpair1);
2021-06-13 21:29:58 +02:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
2021-11-24 08:37:09 +01:00
index 1fc202caf9051f12192ed479898b01b0a02eebbd..0631a5b1af38ace5ad167d1986271081bd2ee7a6 100644
2021-06-13 21:29:58 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
2021-11-24 08:37:09 +01:00
@@ -132,6 +132,13 @@ public class ChunkStorage implements AutoCloseable {
2021-06-13 21:29:58 +02:00
// Paper start - async chunk io
public void write(ChunkPos chunkPos, CompoundTag nbt) throws IOException {
+ // Paper start
+ if (!chunkPos.equals(ChunkSerializer.getChunkCoordinate(nbt))) {
+ String world = (this instanceof net.minecraft.server.level.ChunkMap) ? ((net.minecraft.server.level.ChunkMap)this).level.getWorld().getName() : null;
+ throw new IllegalArgumentException("Chunk coordinate and serialized data do not have matching coordinates, trying to serialize coordinate " + chunkPos.toString()
+ + " but compound says coordinate is " + ChunkSerializer.getChunkCoordinate(nbt).toString() + (world == null ? " for an unknown world" : (" for world: " + world)));
+ }
+ // Paper end
this.regionFileCache.write(chunkPos, nbt);
// Paper end - Async chunk loading
if (this.legacyStructureHandler != null) {