Permalink
Browse files

Improve array_set() handling of invalid normal array keys

  • Loading branch information...
PseudoKnight committed Nov 5, 2017
1 parent fd2e2d9 commit 8101ed3ce00e4554c6dd6b322657757b1d1cc8b6
Showing with 28 additions and 18 deletions.
  1. +28 −18 src/main/java/com/laytonsmith/core/constructs/CArray.java
@@ -317,30 +317,40 @@ public void push(Construct c, Integer index, Target t) throws IllegalArgumentExc
return set;
}
private void setAssociative() {
associative_array = new TreeMap<>(comparator);
for (int i = 0; i < array.size(); i++) {
associative_array.put(Integer.toString(i), array.get(i));
}
associative_mode = true;
array = null; // null out the original array container so it can be GC'd
}
/**
*
* @param index
* @param c
*/
public void set(Construct index, Construct c, Target t) {
if (!associative_mode) {
try {
int indx = Static.getInt32(index, t);
if (indx > next_index || indx < 0) {
throw new CREIndexOverflowException("", Target.UNKNOWN);
} else if(indx == next_index){
this.push(c, t);
} else {
array.set(indx, c);
}
} catch (ConfigRuntimeException e) {
//Not a number. Convert to associative.
associative_array = new TreeMap<String, Construct>(comparator);
for (int i = 0; i < array.size(); i++) {
associative_array.put(Integer.toString(i), array.get(i));
if (index instanceof CNull) {
// Invalid normal array index
setAssociative();
} else {
try {
int indx = Static.getInt32(index, t);
if (indx > next_index || indx < 0) {
// Out of range
setAssociative();
} else if (indx == next_index) {
this.push(c, t);
} else {
array.set(indx, c);
}
} catch (ConfigRuntimeException e) {
// Not a number
setAssociative();
}
associative_mode = true;
array = null; // null out the original array container so it can be GC'd
}
}
if (associative_mode) {
@@ -353,12 +363,12 @@ public void set(Construct index, Construct c, Target t) {
}
public final void set(int index, Construct c, Target t){
this.set(new CInt(index, Target.UNKNOWN), c, t);
this.set(new CInt(index, t), c, t);
}
/* Shortcuts */
public final void set(String index, Construct c, Target t){
set(new CString(index, c.getTarget()), c, t);
set(new CString(index, t), c, t);
}
public final void set(String index, String value, Target t){

0 comments on commit 8101ed3

Please sign in to comment.