1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-14 00:12:06 +01:00

Improve syntax error reporting

This commit is contained in:
Paul Schaub 2022-10-26 18:39:20 +02:00
parent 0d4dc7d170
commit dd543b63cc
2 changed files with 14 additions and 3 deletions

View file

@ -8,6 +8,9 @@ import org.pgpainless.exception.MalformedOpenPgpMessageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import static org.pgpainless.decryption_verification.syntax_check.StackAlphabet.msg;
@ -169,6 +172,7 @@ public class PDA {
}
private final Stack<StackAlphabet> stack = new Stack<>();
private final List<InputAlphabet> inputs = new ArrayList<>(); // keep track of inputs for debugging / error reporting
private State state;
public PDA() {
@ -180,9 +184,12 @@ public class PDA {
public void next(InputAlphabet input) throws MalformedOpenPgpMessageException {
try {
state = state.transition(input, this);
inputs.add(input);
} catch (MalformedOpenPgpMessageException e) {
LOGGER.debug("Unexpected Packet or Token '" + input + "' encountered. Message is malformed.", e);
throw e;
MalformedOpenPgpMessageException wrapped = new MalformedOpenPgpMessageException("Malformed message: After reading stream " + Arrays.toString(inputs.toArray()) +
", token '" + input + "' is unexpected and illegal.", e);
LOGGER.debug("Invalid input '" + input + "'", wrapped);
throw wrapped;
}
}

View file

@ -21,6 +21,10 @@ public class MalformedOpenPgpMessageException extends RuntimeException {
}
public MalformedOpenPgpMessageException(PDA.State state, InputAlphabet input, StackAlphabet stackItem) {
this("Invalid input: There is no legal transition from state '" + state + "' for input '" + input + "' when '" + stackItem + "' is on top of the stack.");
this("There is no legal transition from state '" + state + "' for input '" + input + "' when '" + stackItem + "' is on top of the stack.");
}
public MalformedOpenPgpMessageException(String s, MalformedOpenPgpMessageException e) {
super(s, e);
}
}