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.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<Path> 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 = "";

View file

@ -44,7 +44,13 @@ public class ChatInputSuggestorMixin {
}
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) {
this.pendingSuggestions = suggestions;
this.show(true);

View file

@ -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();

View file

@ -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);
}
}