Fix crashing on invalid path resolution

This commit is contained in:
hhhzzzsss 2023-06-21 22:44:49 -05:00
parent 20a4aaf3ed
commit 23c4d27e2c
4 changed files with 39 additions and 12 deletions

View file

@ -14,6 +14,7 @@ import net.minecraft.text.Text;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -31,6 +32,15 @@ public class Util {
catch (IOException e) {} 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 { public static class LimitedSizeInputStream extends InputStream {
private final InputStream original; private final InputStream original;
private final long maxSize; private final long maxSize;
@ -113,7 +123,12 @@ public class Util {
Path dir = SongPlayer.SONG_DIR; Path dir = SongPlayer.SONG_DIR;
if (lastSlash >= 0) { if (lastSlash >= 0) {
dirString = arg.substring(0, lastSlash+1); dirString = arg.substring(0, lastSlash+1);
dir = dir.resolve(dirString); try {
dir = resolveWithIOException(dir, dirString);
}
catch (IOException e) {
return null;
}
} }
Stream<Path> songFiles; Stream<Path> songFiles;
@ -166,7 +181,12 @@ public class Util {
Path dir = SongPlayer.SONG_DIR; Path dir = SongPlayer.SONG_DIR;
if (lastSlash >= 0) { if (lastSlash >= 0) {
dirString = arg.substring(0, lastSlash+1); dirString = arg.substring(0, lastSlash+1);
dir = dir.resolve(dirString); try {
dir = resolveWithIOException(dir, dirString);
}
catch (IOException e) {
return null;
}
} }
else { else {
dirString = ""; dirString = "";

View file

@ -44,7 +44,13 @@ public class ChatInputSuggestorMixin {
} }
int wordStart = getStartOfCurrentWord(preStr); int wordStart = getStartOfCurrentWord(preStr);
CompletableFuture<Suggestions> suggestions = CommandProcessor.handleSuggestions(preStr, new SuggestionsBuilder(preStr, wordStart)); CompletableFuture<Suggestions> suggestions;
try {
suggestions = CommandProcessor.handleSuggestions(preStr, new SuggestionsBuilder(preStr, wordStart));
}
catch (Throwable e) {
suggestions = null;
}
if (suggestions != null) { if (suggestions != null) {
this.pendingSuggestions = suggestions; this.pendingSuggestions = suggestions;
this.show(true); this.show(true);

View file

@ -197,9 +197,9 @@ public class Playlist {
return validateAndLoadIndex(directory); return validateAndLoadIndex(directory);
} }
public static void createPlaylist(String playlist) { public static void createPlaylist(String playlist) throws IOException {
Path playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(playlist); Path playlistDir = Util.resolveWithIOException(SongPlayer.PLAYLISTS_DIR, playlist);
Util.createDirectoriesSilently(playlistDir); Files.createDirectories(playlistDir);
} }
public static void addSong(Path directory, Path songFile) throws IOException { public static void addSong(Path directory, Path songFile) throws IOException {
@ -214,7 +214,7 @@ public class Playlist {
if (index.contains(songFile.getFileName().toString())) { if (index.contains(songFile.getFileName().toString())) {
throw new IOException("Playlist already contains a song by this name"); 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()); index.add(songFile.getFileName().toString());
saveIndex(directory, index); saveIndex(directory, index);
} }
@ -223,7 +223,7 @@ public class Playlist {
if (!Files.exists(directory)) { if (!Files.exists(directory)) {
throw new IOException("Playlist does not exist"); throw new IOException("Playlist does not exist");
} }
Path songFile = directory.resolve(songName); Path songFile = Util.resolveWithIOException(directory, songName);
if (!Files.exists(songFile)) { if (!Files.exists(songFile)) {
throw new IOException("Playlist does not contain a song by this name"); 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()) { if (pos < 0 || pos >= index.size()) {
throw new IOException("Index out of bounds"); throw new IOException("Index out of bounds");
} }
Path oldPath = directory.resolve(index.get(pos)); Path oldPath = Util.resolveWithIOException(directory, index.get(pos));
Files.move(oldPath, directory.resolve(newName)); Files.move(oldPath, Util.resolveWithIOException(directory, newName));
index.set(pos, newName); index.set(pos, newName);
saveIndex(directory, index); saveIndex(directory, index);
return oldPath.getFileName().toString(); return oldPath.getFileName().toString();

View file

@ -1,6 +1,7 @@
package com.github.hhhzzzsss.songplayer.song; package com.github.hhhzzzsss.songplayer.song;
import com.github.hhhzzzsss.songplayer.SongPlayer; 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.MidiConverter;
import com.github.hhhzzzsss.songplayer.conversion.NBSConverter; import com.github.hhhzzzsss.songplayer.conversion.NBSConverter;
@ -83,7 +84,7 @@ public class SongLoaderThread extends Thread{
} }
} }
private Path getSongFile(String name) { private Path getSongFile(String name) throws IOException {
return SongPlayer.SONG_DIR.resolve(name); return Util.resolveWithIOException(SongPlayer.SONG_DIR, name);
} }
} }