Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Catch json decode errors (#774)

Kilian Lieretd913dd8b7f18
diff --git a/config/commands/decompile.py b/config/commands/decompile.py
index 857041f6..0e7827f1 100644
--- a/config/commands/decompile.py
+++ b/config/commands/decompile.py
@@ -59,14 +59,22 @@ class Decompile:
# Nothing found
return None
- def decompile(self, binary, function):
+ def decompile(self, binary: str, function: str) -> str:
# Look for the decompilation output in "decomp"
basename = Path(binary).name
- decomp_output = Path(f"/ghidra_out/{basename}.decomp.json")
- if not decomp_output.exists():
- if not self.run_ghidra(basename, decomp_output):
+ decomp_output_path = Path(f"/ghidra_out/{basename}.decomp.json")
+ if not decomp_output_path.exists():
+ if not self.run_ghidra(basename, decomp_output_path):
return f"Error: Decompilation for {binary} not available"
- self.ghidra_out = json.loads(decomp_output.read_text())
+ decomp_output = decomp_output_path.read_text()
+ try:
+ self.ghidra_out = json.loads(decomp_output)
+ except json.JSONDecodeError as e:
+ msg = (
+ f"Error: Failed to parse Ghidra output {decomp_output!r} for {binary}. "
+ "Please report this bug."
+ )
+ raise ValueError(msg) from e
if found := self.find_function(self.ghidra_out, function):
ret = self.ghidra_out["functions"][found]
@@ -79,7 +87,12 @@ class Decompile:
funclist = ", ".join(self.ghidra_out['functions'].keys())
return f"Error: Function {function} not found in {binary}.\nThese are the available functions found: {funclist}"
- def run_ghidra(self, binary, output):
+ def run_ghidra(self, binary: str, output: Path) -> bool:
+ """Run ghidra if possible.
+
+ Returns:
+ bool: True if successful, False otherwise
+ """
real_binary = Path(binary)
if not real_binary or not real_binary.exists():
return False
diff --git a/config/commands/disassemble.py b/config/commands/disassemble.py
index cc593d51..26d1a41a 100644
--- a/config/commands/disassemble.py
+++ b/config/commands/disassemble.py
@@ -62,11 +62,19 @@ class Disassemble:
def disassemble(self, binary, function):
# Look for the disassembly output in "disas"
basename = Path(binary).name
- disas_output = Path(f"/ghidra_out/{basename}.disas.json")
- if not disas_output.exists():
- if not self.run_ghidra(basename, disas_output):
+ disas_output_path = Path(f"/ghidra_out/{basename}.disas.json")
+ if not disas_output_path.exists():
+ if not self.run_ghidra(basename, disas_output_path):
return f"Error: Disassembly for {binary} not available"
- self.ghidra_out = json.loads(disas_output.read_text())
+ disas_output = disas_output_path.read_text()
+ try:
+ self.ghidra_out = json.loads(disas_output)
+ except json.JSONDecodeError as e:
+ msg = (
+ f"Error: Failed to parse Ghidra output {disas_output!r} for {binary}. "
+ "Please report this bug."
+ )
+ raise ValueError(msg) from e
if found := self.find_function(self.ghidra_out, function):
ret = self.ghidra_out["functions"][found]
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 3649a539..7e1b90b8 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -657,8 +657,11 @@ class Agent:
output: raw model output (not output of the command)
"""
assert self.config is not None # mypy
-
- state_vars = json.loads(state)
+ try:
+ state_vars = json.loads(state)
+ except json.JSONDecodeError as e:
+ msg = f"State {state!r} is not valid json. This is an internal error, please report it."
+ raise ValueError(msg) from e
templates: list[str] = []
# Determine observation template based on what prior observation was