Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash in regex filters if invalid regex is entered #146

Merged
merged 2 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/zorbatron/zbgt/api/ZBGTAPI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.zorbatron.zbgt.api;

import java.util.regex.Pattern;

import net.minecraft.block.state.IBlockState;
import net.minecraftforge.fluids.FluidStack;

Expand Down Expand Up @@ -33,4 +35,6 @@ public class ZBGTAPI {

public static FluidStack pyrotheum;
public static FluidStack cryotheum;

public static final Pattern EMPTY_PATTERN = Pattern.compile("");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidStack;

import org.jetbrains.annotations.NotNull;

import com.zorbatron.zbgt.api.ZBGTAPI;
import com.zorbatron.zbgt.api.util.FluidStackHashStrategy;
import com.zorbatron.zbgt.api.util.ZBGTUtility;
import com.zorbatron.zbgt.client.widgets.FilterTestFluidSlot;
Expand All @@ -22,7 +24,7 @@
public class LocalNameFluidFilterCover extends FluidFilter {

@NotNull
private String regex = "";
private Pattern regex = ZBGTAPI.EMPTY_PATTERN;
private final Object2ObjectOpenCustomHashMap<FluidStack, Boolean> matchCache = new Object2ObjectOpenCustomHashMap<>(
FluidStackHashStrategy.builder().compareFluid().build());

Expand All @@ -31,7 +33,7 @@ public class LocalNameFluidFilterCover extends FluidFilter {
@Override
public boolean testFluid(@NotNull FluidStack fluidStack) {
return ZBGTUtility.computeIfAbsentDiffKey(matchCache, fluidStack, fluidStack::copy,
stack -> Pattern.matches(regex, stack.getUnlocalizedName()));
stack -> regex.matcher(stack.getUnlocalizedName()).matches());
}

@Override
Expand All @@ -53,13 +55,19 @@ public void initUI(Consumer<Widget> widgetGroup) {
}

widgetGroup.accept(new ImageWidget(10 - 22, 22 + 5, 154, 18, GuiTextures.DISPLAY));
widgetGroup.accept(new TextFieldWidget2(14 - 22, 26 + 5, 150, 14, this::getRegex, this::setRegex));
widgetGroup.accept(new TextFieldWidget2(14 - 22, 26 + 5, 150, 14, this::getPattern, this::setRegex));
}

private void setRegex(String newRegex) {
if (!regex.equals(newRegex)) {
regex = newRegex;
if (!newRegex.equals(getPattern())) {
matchCache.clear();

try {
regex = Pattern.compile(newRegex);
} catch (PatternSyntaxException e) {
regex = ZBGTAPI.EMPTY_PATTERN;
}

for (FilterTestFluidSlot testSlot : testSlots) {
if (testSlot == null) continue;
testSlot.update();
Expand All @@ -68,13 +76,13 @@ private void setRegex(String newRegex) {
}

@NotNull
private String getRegex() {
return regex;
private String getPattern() {
return regex.pattern();
}

@Override
public void writeToNBT(NBTTagCompound tagCompound) {
tagCompound.setString("Filter", getRegex());
tagCompound.setString("Filter", getPattern());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -10,6 +11,7 @@

import org.jetbrains.annotations.NotNull;

import com.zorbatron.zbgt.api.ZBGTAPI;
import com.zorbatron.zbgt.api.util.ZBGTUtility;
import com.zorbatron.zbgt.client.widgets.FilterTestSlot;

Expand All @@ -24,7 +26,7 @@
public class RegistryNameItemFilterCover extends ItemFilter {

@NotNull
private String regex = "";
private Pattern regex = ZBGTAPI.EMPTY_PATTERN;
private final Object2ObjectOpenCustomHashMap<ItemStack, Boolean> matchCache = new Object2ObjectOpenCustomHashMap<>(
ItemStackHashStrategy.builder().compareItem(true).build());

Expand All @@ -46,11 +48,13 @@ public Object matchItemStack(ItemStack itemStack) {
}

public boolean matchesItemStack(@NotNull ItemStack itemStack) {
if (regex == ZBGTAPI.EMPTY_PATTERN) return false;

return ZBGTUtility.computeIfAbsentDiffKey(matchCache, itemStack, itemStack::copy, stack -> {
ResourceLocation resloc = Item.REGISTRY.getNameForObject(itemStack.getItem());
if (resloc == null) return false;
String name = resloc.toString();
return Pattern.matches(regex, name);
return regex.matcher(name).matches();
});
}

Expand All @@ -68,13 +72,19 @@ public void initUI(Consumer<Widget> widgetGroup) {
}

widgetGroup.accept(new ImageWidget(10, 22, 154, 18, GuiTextures.DISPLAY));
widgetGroup.accept(new TextFieldWidget2(14, 26, 150, 14, this::getRegex, this::setRegex));
widgetGroup.accept(new TextFieldWidget2(14, 26, 150, 14, this::getPattern, this::setRegex));
}

private void setRegex(String newRegex) {
if (!regex.equals(newRegex)) {
regex = newRegex;
if (!newRegex.equals(getPattern())) {
matchCache.clear();

try {
regex = Pattern.compile(newRegex);
} catch (PatternSyntaxException e) {
regex = ZBGTAPI.EMPTY_PATTERN;
}

for (FilterTestSlot testSlot : testSlots) {
if (testSlot == null) continue;
testSlot.update();
Expand All @@ -83,13 +93,13 @@ private void setRegex(String newRegex) {
}

@NotNull
private String getRegex() {
return regex;
private String getPattern() {
return regex.pattern();
}

@Override
public void writeToNBT(NBTTagCompound tagCompound) {
tagCompound.setString("Filter", getRegex());
tagCompound.setString("Filter", getPattern());
}

@Override
Expand Down