From 414c3cf33dc816d018a78a4c11b1d959ade73ad4 Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 4 Oct 2010 17:00:54 -0700 Subject: [PATCH] Added short overflow check for schematic saving. --- src/RegionClipboard.java | 29 +++++++++++++------ src/WorldEdit.java | 4 ++- ...Exception.java => SchematicException.java} | 4 +-- 3 files changed, 25 insertions(+), 12 deletions(-) rename src/com/sk89q/worldedit/{SchematicLoadException.java => SchematicException.java} (87%) diff --git a/src/RegionClipboard.java b/src/RegionClipboard.java index 1cd4ad9a6..5817a3c6c 100644 --- a/src/RegionClipboard.java +++ b/src/RegionClipboard.java @@ -129,12 +129,23 @@ public class RegionClipboard { * * @param path * @throws IOException + * @throws SchematicException */ - public void saveSchematic(String path) throws IOException { + public void saveSchematic(String path) throws IOException, SchematicException { int xs = getWidth(); int ys = getHeight(); int zs = getLength(); + if (xs > 65535) { + throw new SchematicException("Width of region too large for a .schematic"); + } + if (ys > 65535) { + throw new SchematicException("Height of region too large for a .schematic"); + } + if (zs > 65535) { + throw new SchematicException("Length of region too large for a .schematic"); + } + HashMap schematic = new HashMap(); schematic.put("Width", new ShortTag("Width", (short)xs)); schematic.put("Length", new ShortTag("Length", (short)zs)); @@ -174,27 +185,27 @@ public class RegionClipboard { * @param path * @param origin * @return - * @throws SchematicLoadException + * @throws SchematicException * @throws IOException */ public static RegionClipboard loadSchematic(String path, Point origin) - throws SchematicLoadException, IOException { + throws SchematicException, IOException { FileInputStream stream = new FileInputStream(path); NBTInputStream nbtStream = new NBTInputStream(stream); CompoundTag schematicTag = (CompoundTag)nbtStream.readTag(); if (!schematicTag.getName().equals("Schematic")) { - throw new SchematicLoadException("Tag \"Schematic\" does not exist or is not first"); + throw new SchematicException("Tag \"Schematic\" does not exist or is not first"); } Map schematic = schematicTag.getValue(); if (!schematic.containsKey("Blocks")) { - throw new SchematicLoadException("Schematic file is missing a \"Blocks\" tag"); + throw new SchematicException("Schematic file is missing a \"Blocks\" tag"); } short xs = (Short)getChildTag(schematic, "Width", ShortTag.class).getValue(); short zs = (Short)getChildTag(schematic, "Length", ShortTag.class).getValue(); short ys = (Short)getChildTag(schematic, "Height", ShortTag.class).getValue(); String materials = (String)getChildTag(schematic, "Materials", StringTag.class).getValue(); if (!materials.equals("Alpha")) { - throw new SchematicLoadException("Schematic file is not an Alpha schematic"); + throw new SchematicException("Schematic file is not an Alpha schematic"); } byte[] blocks = (byte[])getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue(); @@ -223,13 +234,13 @@ public class RegionClipboard { } private static Tag getChildTag(Map items, String key, Class expected) - throws SchematicLoadException { + throws SchematicException { if (!items.containsKey(key)) { - throw new SchematicLoadException("Schematic file is missing a \"" + key + "\" tag"); + throw new SchematicException("Schematic file is missing a \"" + key + "\" tag"); } Tag tag = items.get(key); if (!expected.isInstance(tag)) { - throw new SchematicLoadException( + throw new SchematicException( key + " tag is not of tag type " + expected.getName()); } return tag; diff --git a/src/WorldEdit.java b/src/WorldEdit.java index 25599d8ad..62a870c65 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -467,7 +467,7 @@ public class WorldEdit extends Plugin { logger.log(Level.INFO, player.getName() + " loaded " + filePath); player.sendMessage(Colors.LightPurple + filename + " loaded."); } - } catch (SchematicLoadException e) { + } catch (SchematicException e) { player.sendMessage(Colors.Rose + "Load error: " + e.getMessage()); } catch (IOException e) { player.sendMessage(Colors.Rose + "Schematic could not read or it does not exist."); @@ -505,6 +505,8 @@ public class WorldEdit extends Plugin { logger.log(Level.INFO, player.getName() + " saved " + filePath); player.sendMessage(Colors.LightPurple + filename + " saved."); } + } catch (SchematicException se) { + player.sendMessage(Colors.Rose + "Save error: " + se.getMessage()); } catch (IOException e) { player.sendMessage(Colors.Rose + "Schematic could not written."); } diff --git a/src/com/sk89q/worldedit/SchematicLoadException.java b/src/com/sk89q/worldedit/SchematicException.java similarity index 87% rename from src/com/sk89q/worldedit/SchematicLoadException.java rename to src/com/sk89q/worldedit/SchematicException.java index b8a9266c7..ea4cfbd55 100644 --- a/src/com/sk89q/worldedit/SchematicLoadException.java +++ b/src/com/sk89q/worldedit/SchematicException.java @@ -23,8 +23,8 @@ package com.sk89q.worldedit; * * @author Albert */ -public class SchematicLoadException extends WorldEditException { - public SchematicLoadException(String error) { +public class SchematicException extends WorldEditException { + public SchematicException(String error) { super(error); } }