Merge remote-tracking branch 'origin/GT-3569_nsadeveloper789_LongErrors'

This commit is contained in:
ghidra1 2021-04-16 16:21:30 -04:00
commit 486cee0f10
3 changed files with 47 additions and 9 deletions

View file

@ -15,13 +15,20 @@
*/
package ghidra.app.plugin.assembler.sleigh.parse;
import java.util.Collections;
import java.util.Set;
import java.util.*;
import org.apache.commons.collections4.IterableUtils;
/**
* An unsuccessful result from parsing
*/
public class AssemblyParseErrorResult extends AssemblyParseResult {
/**
* The maximum number of suggestions to print when describing this error, e.g., when reported in
* exception messages.
*/
private static final int SUGGESTIONS_THRESHOLD = 10;
private final String buffer;
private final Set<String> suggestions;
@ -40,15 +47,28 @@ public class AssemblyParseErrorResult extends AssemblyParseResult {
/**
* Get a description of the error
*
* @return a description
*/
public String describeError() {
return "Syntax Error: Expected " + suggestions + ". Got " + buffer;
Collection<String> truncSuggestions;
if (suggestions.size() <= SUGGESTIONS_THRESHOLD) {
truncSuggestions = suggestions;
}
else {
truncSuggestions = new ArrayList<>();
for (String s : IterableUtils.boundedIterable(suggestions, SUGGESTIONS_THRESHOLD)) {
truncSuggestions.add(s);
}
truncSuggestions.add("...");
}
return "Syntax Error: Expected " + truncSuggestions + ". Got " + buffer;
}
/**
* Get a set of suggested tokens that would have allowed parsing to continue
* @return the set
*
* @return the token set
*/
public Set<String> getSuggestions() {
return Collections.unmodifiableSet(suggestions);
@ -56,7 +76,8 @@ public class AssemblyParseErrorResult extends AssemblyParseResult {
/**
* Get the leftover contents of the input buffer when the error occurred
* @return
*
* @return the remaining buffer contents
*/
public String getBuffer() {
return buffer;

View file

@ -303,7 +303,7 @@ public abstract class AbstractAssemblyTest extends AbstractGenericTest {
/**
* Run a test with the given checks
*
* @param assembly the text to assembly
* @param assembly the text to assemble
* @param instr an instruction pattern that must appear in the results
* @param disassembly a set of acceptable disassembly texts
* @param addr the address for assembly and disassembly

View file

@ -15,6 +15,9 @@
*/
package ghidra.app.plugin.assembler.sleigh;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import ghidra.app.plugin.assembler.*;
@ -31,6 +34,20 @@ public class x86AssemblyTest extends AbstractAssemblyTest {
return new LanguageID("x86:LE:64:default");
}
@Test
public void testReasonableErrorMessageLength() throws AssemblySemanticException {
Assembler assembler = Assemblers.getAssembler(lang);
Address addr = lang.getDefaultSpace().getAddress(DEFAULT_ADDR);
try {
assembler.assembleLine(addr, "UNLIKELY qword ptr [RAX],RBX");
fail(); // The exception must be thrown
}
catch (AssemblySyntaxException e) {
Msg.info(this, "Got expected syntax error: " + e);
assertTrue(e.getMessage().length() < 1000);
}
}
@Test
public void testAssemble_ADD_m0x12_RAXm_RBX() {
// Again, a little odd. Imm8 does not have the I+R form.