Skip to content

Commit

Permalink
Merge pull request #105 from gombasg/deep_resource_protection
Browse files Browse the repository at this point in the history
Resource protection should be deep, not shallow
  • Loading branch information
jrha authored Aug 4, 2016
2 parents 9bb8b60 + a5743f1 commit cabb3d0
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 23 deletions.
14 changes: 12 additions & 2 deletions panc/src/main/java/org/quattor/pan/dml/data/HashResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ public String toString() {

@Override
public Resource.Iterator iterator() {
return new HashResourceIterator(map);
return new HashResourceIterator(map, false);
}

public Resource.Iterator protectedIterator() {
return new HashResourceIterator(map, true);
}

private static class HashResourceIterator implements Resource.Iterator {
Expand All @@ -218,9 +222,12 @@ private static class HashResourceIterator implements Resource.Iterator {

private final Map<String, Element> backingHash;

public HashResourceIterator(Map<String, Element> backingHash) {
private final boolean isProtected;

public HashResourceIterator(Map<String, Element> backingHash, boolean isProtected) {
assert (backingHash != null);
this.backingHash = backingHash;
this.isProtected = isProtected;
iterator = backingHash.keySet().iterator();
}

Expand All @@ -244,6 +251,9 @@ public Resource.Entry next() {
MessageUtils.format(MSG_CONCURRENT_MODIFICATION),
null);
}
if (isProtected) {
value = value.protect();
}
entry = new HashResourceEntry(key, value);
} catch (NoSuchElementException nsee) {
throw new EvaluationException(
Expand Down
25 changes: 19 additions & 6 deletions panc/src/main/java/org/quattor/pan/dml/data/ListResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ public int size() {

@Override
public Resource.Iterator iterator() {
return new ListResourceIterator(list);
return new ListResourceIterator(list, false);
}

public Resource.Iterator protectedIterator() {
return new ListResourceIterator(list, true);
}

@Override
Expand Down Expand Up @@ -286,13 +290,16 @@ public String toString() {

private static class ListResourceIterator implements Resource.Iterator {

private final AtomicInteger index = new AtomicInteger(0);
private final AtomicInteger index = new AtomicInteger(0);

private final List<Element> backingList;

public ListResourceIterator(List<Element> backingList) {
private final boolean isProtected;

public ListResourceIterator(List<Element> backingList, boolean isProtected) {
assert (backingList != null);
this.backingList = backingList;
this.isProtected = isProtected;
}

public void remove() {
Expand All @@ -306,10 +313,16 @@ public boolean hasNext() {

public Resource.Entry next() {
try {
int i = index.getAndIncrement();
int i = index.getAndIncrement();

Element value = backingList.get(i);
if (isProtected && value != null) {
value = value.protect();
}

Resource.Entry entry = new ListResourceEntry(LongProperty.getInstance(i),
backingList.get(i));
return entry;
value);
return entry;
} catch (NoSuchElementException nsee) {
throw new EvaluationException(MessageUtils
.format(MSG_CONCURRENT_MODIFICATION), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public Element duplicate() {

@Override
public Element get(Term key) throws InvalidTermException {
return baseHash.get(key);
final Element value = baseHash.get(key);
if (value != null) {
return value.protect();
}
return value;
}

@Override
Expand Down Expand Up @@ -84,7 +88,12 @@ public String toString() {

@Override
public Resource.Iterator iterator() {
return baseHash.iterator();
return baseHash.protectedIterator();
}

@Override
public Resource.Iterator protectedIterator() {
return baseHash.protectedIterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public Element duplicate() {

@Override
public Element get(Term key) throws InvalidTermException {
return baseList.get(key);
final Element value = baseList.get(key);
if (value != null) {
return value.protect();
}
return value;
}

@Override
Expand All @@ -38,14 +42,29 @@ public Element put(int index, Element newValue) {
throw CompilerError.create(MSG_ILLEGAL_WRITE_TO_PROTECTED_LIST);
}

@Override
public void append(Element e) {
throw CompilerError.create(MSG_ILLEGAL_WRITE_TO_PROTECTED_LIST);
}

@Override
public void prepend(Element e) {
throw CompilerError.create(MSG_ILLEGAL_WRITE_TO_PROTECTED_LIST);
}

@Override
public int size() {
return baseList.size();
}

@Override
public Resource.Iterator iterator() {
return baseList.iterator();
return baseList.protectedIterator();
}

@Override
public Resource.Iterator protectedIterator() {
return baseList.protectedIterator();
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions panc/src/main/java/org/quattor/pan/parser/ASTBaseTypeSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Centre National de la Recherche Scientifique (CNRS).
public class ASTBaseTypeSpec extends SimpleNode {

private Range range = null;

private boolean extensible = false;

private String identifier = null;

public ASTBaseTypeSpec(int id) {
Expand All @@ -45,19 +45,19 @@ public void setExtensible(boolean extensible) {
public boolean isExtensible() {
return extensible;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

public String getIdentifier() {
return identifier;
}

public void setRange(Range range) {
this.range = range;
}

public Range getRange() {
return range;
}
Expand Down
4 changes: 2 additions & 2 deletions panc/src/main/java/org/quattor/pan/parser/ASTFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public ASTFunction(int id) {
public ASTFunction(PanParser p, int id) {
super(p, id);
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public void setObjectAndLoadpath() {
setGlobalVariable("OBJECT", sname, true);
setGlobalVariable("LOADPATH", new ListResource(), false);
}

public String getObjectName() {
return (objectTemplate != null) ? objectTemplate.name : "unknown";
}
Expand Down
2 changes: 1 addition & 1 deletion panc/src/main/java/org/quattor/pan/template/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public interface Context {
* Set the name of the object template. Define the necessary variables.
*/
public void setObjectAndLoadpath();

/**
* Get the name of the object template.
*/
Expand Down
6 changes: 5 additions & 1 deletion panc/src/main/java/org/quattor/pan/type/FullType.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public FullType(String source, SourceRange sourceRange, BaseType baseType,
assert (baseType != null);

this.baseType = baseType;
this.defaultValue = defaultValue;
if (defaultValue != null) {
this.defaultValue = defaultValue.protect();
} else {
this.defaultValue = null;
}
this.dml = dml;
}

Expand Down
17 changes: 17 additions & 0 deletions panc/src/test/pan/Functionality/global/global6.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# see if globals can be modified indirectly via iterators in dml
#
# @expect="/nlist[@name='profile']/list[@name='data']/*[1]/string[@name='a']='OK'"
#
object template global6;

variable GLOBAL = list(nlist("a", "OK"));

variable GLOBAL = {
foreach (idx; item; GLOBAL) {
item["a"] = "BAD";
};
SELF;
};

"/data" = GLOBAL;

0 comments on commit cabb3d0

Please sign in to comment.