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

Copy the SimpleLiteral backing data instead of exposing it. #6683

Merged
merged 1 commit into from
May 10, 2024
Merged
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
15 changes: 10 additions & 5 deletions src/main/java/ch/njol/skript/lang/util/SimpleLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.skriptlang.skript.lang.converter.Converters;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
* Represents a literal, i.e. a static value like a number or a string.
Expand Down Expand Up @@ -95,24 +96,28 @@ public boolean init() {
return true;
}

private T[] data() {
return Arrays.copyOf(data, data.length);
}

@Override
public T[] getArray() {
return data;
return this.data();
}

@Override
public T[] getArray(Event event) {
return data;
return this.data();
}

@Override
public T[] getAll() {
return data;
return this.data();
}

@Override
public T[] getAll(Event event) {
return data;
return this.data();
}

@Override
Expand All @@ -136,7 +141,7 @@ public Class<T> getReturnType() {
public <R> Literal<? extends R> getConvertedExpression(Class<R>... to) {
if (CollectionUtils.containsSuperclass(to, type))
return (Literal<? extends R>) this;
R[] parsedData = Converters.convert(data, to, (Class<R>) Utils.getSuperType(to));
R[] parsedData = Converters.convert(this.data(), to, (Class<R>) Utils.getSuperType(to));
if (parsedData.length != data.length)
return null;
return new ConvertedLiteral<>(this, parsedData, (Class<R>) Utils.getSuperType(to));
Expand Down