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 stack overflow during KState remove #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 12 additions & 9 deletions src/main/java/com/bmwcarit/barefoot/markov/KState.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,18 @@ public void update(Set<C> vector, S sample) {
}

protected void remove(C candidate, int index) {
Set<C> vector = sequence.get(index).one();
counters.remove(candidate);
vector.remove(candidate);

C predecessor = candidate.predecessor();
if (predecessor != null) {
counters.put(predecessor, counters.get(predecessor) - 1);
if (counters.get(predecessor) == 0) {
remove(predecessor, index - 1);
while (candidate != null) {
Set<C> vector = sequence.get(index).one();
counters.remove(candidate);
vector.remove(candidate);
candidate = candidate.predecessor();
if (candidate != null) {
counters.put(candidate, counters.get(candidate) - 1);
if (counters.get(candidate) == 0) {
index -= 1;
} else {
candidate = null;
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/bmwcarit/barefoot/markov/KStateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ public void TestKState() {
}
}

@Test
public void TestKStateRemoveLongSequence() {
// KState remove code was originally recursive, but this could cause a
// stack overflow. This tests that removing a long sequence succeeds
// without causing an exception.
KState<MockElem, StateTransition, Sample> state = new KState<>(-1, -1);
MockElem candidate = null;
int i = 0;
for (i = 0; i < 10000; ++i) {
candidate = new MockElem(i, Math.log10(0.3), 0.3, candidate);
Set<MockElem> vector = new HashSet<>(Arrays.asList(candidate));
state.update(vector, new Sample(i));
}
state.remove(candidate, i - 1);
}

@Test
public void TestTState() {
Map<Integer, MockElem> elements = new HashMap<>();
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/com/bmwcarit/barefoot/matcher/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void stop() {

private void sendRequest(InetAddress host, int port, JSONArray samples)
throws InterruptedException, IOException, JSONException {
int trials = 120;
int timeout = 500;
int trials = 300;
int timeout = 1000;
Socket client = null;

while (client == null || !client.isConnected()) {
Expand All @@ -69,7 +69,9 @@ private void sendRequest(InetAddress host, int port, JSONArray samples)
Thread.sleep(timeout);

if (trials == 0) {
client.close();
if (client != null) {
client.close();
}
throw new IOException(e.getMessage());
} else {
trials -= 1;
Expand Down
20 changes: 12 additions & 8 deletions src/test/java/com/bmwcarit/barefoot/tracker/TrackerServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void stop() {

public void sendSample(InetAddress host, int port, JSONObject sample)
throws InterruptedException, IOException {
int trials = 120;
int timeout = 500;
int trials = 300;
int timeout = 1000;
Socket client = null;

while (client == null || !client.isConnected()) {
Expand All @@ -73,8 +73,10 @@ public void sendSample(InetAddress host, int port, JSONObject sample)

if (trials == 0) {
logger.error(e.getMessage());
client.close();
throw new IOException();
if (client != null) {
client.close();
}
throw new IOException(e.getMessage());
} else {
trials -= 1;
}
Expand All @@ -92,8 +94,8 @@ public void sendSample(InetAddress host, int port, JSONObject sample)

public MatcherKState requestState(InetAddress host, int port, String id)
throws JSONException, InterruptedException, IOException {
int trials = 120;
int timeout = 500;
int trials = 300;
int timeout = 1000;
Socket client = null;

while (client == null || !client.isConnected()) {
Expand All @@ -104,8 +106,10 @@ public MatcherKState requestState(InetAddress host, int port, String id)

if (trials == 0) {
logger.error(e.getMessage());
client.close();
throw new IOException();
if (client != null) {
client.close();
}
throw new IOException(e.getMessage());
} else {
trials -= 1;
}
Expand Down