Skip to content

Commit

Permalink
Fix errors and potential infinite loops caused by invalid page numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
espidev committed Nov 13, 2023
1 parent 01d784e commit 15d8ba2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/main/java/dev/espi/protectionstones/PSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum PSL {
MUST_BE_PLAYER("must_be_player", ChatColor.RED + "You must be a player to execute this command."),
GO_BACK_PAGE("go_back_page", "Go back a page."),
GO_NEXT_PAGE("go_next_page", "Go to next page."),
PAGE_DOES_NOT_EXIST("page_does_not_exist", ChatColor.RED + "Page does not exist."),

HELP("help", ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "=====" + ChatColor.RESET + " PS Help " + ChatColor.DARK_GRAY + ChatColor.STRIKETHROUGH + "=====\n" + ChatColor.AQUA + "> " + ChatColor.GRAY + "/ps help"),
HELP_NEXT("help_next", ChatColor.GRAY + "Do /ps help %page% to go to the next page!"),
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/dev/espi/protectionstones/commands/ArgFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.sk89q.worldguard.protection.flags.*;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import dev.espi.protectionstones.*;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.WGUtils;
import net.md_5.bungee.api.chat.*;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -66,18 +67,24 @@ private String getDots(int num) {

// flag gui that has ability to use pages
private boolean openFlagGUI(Player p, PSRegion r, int page) {
List<String> allowedFlags = new ArrayList<>(r.getTypeOptions().allowedFlags.keySet());

// ensure the page is valid and in range
if (page < 0 || (page * GUI_SIZE) > allowedFlags.size()) {
PSL.msg(p, PSL.PAGE_DOES_NOT_EXIST.msg());
return true;
}

// add blank space if gui not long enough
for (int i = 0; i < (GUI_SIZE * page + GUI_SIZE) - (Math.min(r.getTypeOptions().allowedFlags.size(), GUI_SIZE * page + GUI_SIZE) - GUI_SIZE * page); i++) {
for (int i = 0; i < (GUI_SIZE * page + GUI_SIZE) - (Math.min(allowedFlags.size(), GUI_SIZE * page + GUI_SIZE) - GUI_SIZE * page); i++) {
PSL.msg(p, ChatColor.WHITE + "");
}

PSL.msg(p, PSL.FLAG_GUI_HEADER.msg());

List<String> allowedFlags = new ArrayList<>(r.getTypeOptions().allowedFlags.keySet());

// send actual flags
for (int i = GUI_SIZE * page; i < Math.min(allowedFlags.size(), GUI_SIZE * page + GUI_SIZE); i++) {
if (i >= r.getTypeOptions().allowedFlags.size()) {
if (i >= allowedFlags.size()) {
PSL.msg(p, ChatColor.WHITE + "");
} else {
String flag = allowedFlags.get(i);
Expand Down Expand Up @@ -244,9 +251,15 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap<String, S

// /ps flag GUI
if (args.length == 1) return openFlagGUI(p, r, 0);

// go to GUI page
if (args.length == 2 && StringUtils.isNumeric(args[1])) {
return openFlagGUI(p, r, Integer.parseInt(args[1]));
if (args.length == 2) {
if (MiscUtil.isValidInteger(args[1])) {
return openFlagGUI(p, r, Integer.parseInt(args[1]));
}

PSL.msg(p, PSL.FLAG_HELP.msg());
return true;
}

if (args.length < 3) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/dev/espi/protectionstones/commands/ArgHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import dev.espi.protectionstones.PSL;
import dev.espi.protectionstones.ProtectionStones;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.TextGUI;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
Expand Down Expand Up @@ -104,7 +105,7 @@ public HashMap<String, Boolean> getRegisteredFlags() {
@Override
public boolean executeArgument(CommandSender p, String[] args, HashMap<String, String> flags) {
int page = 0;
if (args.length > 1 && StringUtils.isNumeric(args[1])) {
if (args.length > 1 && MiscUtil.isValidInteger(args[1])) {
page = Integer.parseInt(args[1]) - 1;
}

Expand All @@ -125,7 +126,9 @@ public boolean executeArgument(CommandSender p, String[] args, HashMap<String, S

TextGUI.displayGUI(p, PSL.HELP.msg(), "/" + ProtectionStones.getInstance().getConfigOptions().base_command + " help %page%", page, GUI_SIZE, entries, false);

if (page * GUI_SIZE + GUI_SIZE < entries.size()) PSL.msg(p, PSL.HELP_NEXT.msg().replace("%page%", page + 2 + ""));
if (page >= 0 && page * GUI_SIZE + GUI_SIZE < entries.size()) {
PSL.msg(p, PSL.HELP_NEXT.msg().replace("%page%", page + 2 + ""));
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import dev.espi.protectionstones.*;
import dev.espi.protectionstones.utils.ChatUtil;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.TextGUI;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
Expand Down Expand Up @@ -136,7 +137,7 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap<String, S
if (regions.size() == 1) { // teleport to home if there is only one home
ArgTp.teleportPlayer(p, regions.get(0));
} else { // otherwise, open the GUI
openHomeGUI(psp, regions, (flags.get("-p") == null || !StringUtils.isNumeric(flags.get("-p")) ? 0 : Integer.parseInt(flags.get("-p")) - 1));
openHomeGUI(psp, regions, (flags.get("-p") == null || !MiscUtil.isValidInteger(flags.get("-p")) ? 0 : Integer.parseInt(flags.get("-p")) - 1));
}
} else {// /ps home [id]
// get regions from the query
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/espi/protectionstones/commands/ArgTax.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dev.espi.protectionstones.commands;

import dev.espi.protectionstones.*;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.TextGUI;
import dev.espi.protectionstones.utils.UUIDCache;
import net.md_5.bungee.api.chat.ClickEvent;
Expand Down Expand Up @@ -105,7 +106,7 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap<String, S
public boolean taxInfo(String[] args, HashMap<String, String> flags, PSPlayer p) {
if (args.length == 2) { // /ps tax info
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
int pageNum = (flags.get("-p") == null || !StringUtils.isNumeric(flags.get("-p")) ? 0 : Integer.parseInt(flags.get("-p"))-1);
int pageNum = (flags.get("-p") == null || !MiscUtil.isValidInteger(flags.get("-p")) ? 0 : Integer.parseInt(flags.get("-p"))-1);

List<TextComponent> entries = new ArrayList<>();
for (PSRegion r : p.getTaxEligibleRegions()) {
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/dev/espi/protectionstones/utils/MiscUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static int getPermissionNumber(List<String> permissions, String perm, int
for (String permission : permissions) {
if (permission.startsWith(perm)) {
String value = permission.substring(perm.length());
if (StringUtils.isNumeric(value)) {
if (MiscUtil.isValidInteger(value)) {
n = Math.max(n, Integer.parseInt(value));
}
}
Expand Down Expand Up @@ -126,4 +126,17 @@ public static List<String> getLuckPermsUserPermissions(UUID uniqueId) throws Exe
return permissions;
}

public static boolean isValidInteger(String str) {
if (str == null) {
return false;
}

try {
// this apparently throws NumberFormatException if it is beyond the integer limit, so we catch that
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
} catch (NullPointerException e) {}
return false;
}
}
5 changes: 5 additions & 0 deletions src/main/java/dev/espi/protectionstones/utils/TextGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class TextGUI {
// page starts at zero, but displays start at one
// pageCommand will be replacing %page%
public static void displayGUI(CommandSender s, String header, String pageCommand, int currentPage, int guiSize, List<TextComponent> lines, boolean sendBlankLines) {
int currentLine = currentPage * guiSize;
if (currentPage < 0 || currentLine > lines.size()) {
return;
}

PSL.msg(s, header);

for (int i = currentPage*guiSize; i < Math.min((currentPage+1) * guiSize, lines.size()); i++) {
Expand Down

0 comments on commit 15d8ba2

Please sign in to comment.