From 23c4d27e2c1bc7b1122d154743f93d4d774ce016 Mon Sep 17 00:00:00 2001 From: hhhzzzsss Date: Wed, 21 Jun 2023 22:44:49 -0500 Subject: [PATCH] Fix crashing on invalid path resolution --- .../com/github/hhhzzzsss/songplayer/Util.java | 24 +++++++++++++++++-- .../mixin/ChatInputSuggestorMixin.java | 8 ++++++- .../hhhzzzsss/songplayer/song/Playlist.java | 14 +++++------ .../songplayer/song/SongLoaderThread.java | 5 ++-- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/Util.java b/src/main/java/com/github/hhhzzzsss/songplayer/Util.java index fa0c620..effd97f 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/Util.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/Util.java @@ -14,6 +14,7 @@ import net.minecraft.text.Text; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -31,6 +32,15 @@ public class Util { catch (IOException e) {} } + public static Path resolveWithIOException(Path path, String other) throws IOException { + try { + return path.resolve(other); + } + catch (InvalidPathException e) { + throw new IOException(e.getMessage()); + } + } + public static class LimitedSizeInputStream extends InputStream { private final InputStream original; private final long maxSize; @@ -113,7 +123,12 @@ public class Util { Path dir = SongPlayer.SONG_DIR; if (lastSlash >= 0) { dirString = arg.substring(0, lastSlash+1); - dir = dir.resolve(dirString); + try { + dir = resolveWithIOException(dir, dirString); + } + catch (IOException e) { + return null; + } } Stream songFiles; @@ -166,7 +181,12 @@ public class Util { Path dir = SongPlayer.SONG_DIR; if (lastSlash >= 0) { dirString = arg.substring(0, lastSlash+1); - dir = dir.resolve(dirString); + try { + dir = resolveWithIOException(dir, dirString); + } + catch (IOException e) { + return null; + } } else { dirString = ""; diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ChatInputSuggestorMixin.java b/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ChatInputSuggestorMixin.java index a37e996..fc7d59e 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ChatInputSuggestorMixin.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ChatInputSuggestorMixin.java @@ -44,7 +44,13 @@ public class ChatInputSuggestorMixin { } int wordStart = getStartOfCurrentWord(preStr); - CompletableFuture suggestions = CommandProcessor.handleSuggestions(preStr, new SuggestionsBuilder(preStr, wordStart)); + CompletableFuture suggestions; + try { + suggestions = CommandProcessor.handleSuggestions(preStr, new SuggestionsBuilder(preStr, wordStart)); + } + catch (Throwable e) { + suggestions = null; + } if (suggestions != null) { this.pendingSuggestions = suggestions; this.show(true); diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/song/Playlist.java b/src/main/java/com/github/hhhzzzsss/songplayer/song/Playlist.java index 549aa19..293cd79 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/song/Playlist.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/song/Playlist.java @@ -197,9 +197,9 @@ public class Playlist { return validateAndLoadIndex(directory); } - public static void createPlaylist(String playlist) { - Path playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(playlist); - Util.createDirectoriesSilently(playlistDir); + public static void createPlaylist(String playlist) throws IOException { + Path playlistDir = Util.resolveWithIOException(SongPlayer.PLAYLISTS_DIR, playlist); + Files.createDirectories(playlistDir); } public static void addSong(Path directory, Path songFile) throws IOException { @@ -214,7 +214,7 @@ public class Playlist { if (index.contains(songFile.getFileName().toString())) { throw new IOException("Playlist already contains a song by this name"); } - Files.copy(songFile, directory.resolve(songFile.getFileName().toString())); + Files.copy(songFile, Util.resolveWithIOException(directory, songFile.getFileName().toString())); index.add(songFile.getFileName().toString()); saveIndex(directory, index); } @@ -223,7 +223,7 @@ public class Playlist { if (!Files.exists(directory)) { throw new IOException("Playlist does not exist"); } - Path songFile = directory.resolve(songName); + Path songFile = Util.resolveWithIOException(directory, songName); if (!Files.exists(songFile)) { throw new IOException("Playlist does not contain a song by this name"); } @@ -250,8 +250,8 @@ public class Playlist { if (pos < 0 || pos >= index.size()) { throw new IOException("Index out of bounds"); } - Path oldPath = directory.resolve(index.get(pos)); - Files.move(oldPath, directory.resolve(newName)); + Path oldPath = Util.resolveWithIOException(directory, index.get(pos)); + Files.move(oldPath, Util.resolveWithIOException(directory, newName)); index.set(pos, newName); saveIndex(directory, index); return oldPath.getFileName().toString(); diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java b/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java index a88c053..cc89583 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java @@ -1,6 +1,7 @@ package com.github.hhhzzzsss.songplayer.song; import com.github.hhhzzzsss.songplayer.SongPlayer; +import com.github.hhhzzzsss.songplayer.Util; import com.github.hhhzzzsss.songplayer.conversion.MidiConverter; import com.github.hhhzzzsss.songplayer.conversion.NBSConverter; @@ -83,7 +84,7 @@ public class SongLoaderThread extends Thread{ } } - private Path getSongFile(String name) { - return SongPlayer.SONG_DIR.resolve(name); + private Path getSongFile(String name) throws IOException { + return Util.resolveWithIOException(SongPlayer.SONG_DIR, name); } }