Using getBytes with Python script broke with Jython->PyGhidra migration
#9291
Replies: 1 comment
-
|
This looks like an expected PyGhidra/JPype type-boundary issue rather than a change in The Java signatures line up with the error:
So the explicit conversion is the right workaround: max_length = int(next_addr_with_ref.subtract(start_addr))
data = getBytes(start_addr, max_length)I would add a guard if the range is computed dynamically: max_length = next_addr_with_ref.subtract(start_addr)
if max_length > 0x7fffffff:
raise ValueError(f"range too large for FlatProgramAPI.getBytes(): {max_length}")
data = getBytes(start_addr, int(max_length))Changing A Pythonic |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
Last year I have written some Python scripts which invoked
getBytes(addr, size)(fromFlatProgramAPI). It ran fine with Jython on Ghidra 11.3.2 and 11.4.x.Testing the same scripts from the PyGhidra window in Ghidra 12.1.2 now reports a Python exception:
The failing code is a loop which reads bytes between addresses with references:
The issue is that
max_lengthis aJLongwhilegetBytesexpects anint:ghidra/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java
Line 2006 in f9e1384
As it is very unlikely that
lengthgets over 2GB, I can imagine why this function takes anintinstead oflong. Nonetheless this means that instead of writing:... PyGhidra now requires adding an explicit cast to
int:Here are 2 questions:
lengthin functiongetDatatolong? Is this kind of inconsistency in scripting API considered as something which can be improved in the future?intcan actually hold any arbitrary large integer (contrary to Java int). Would it make sense to makeAddress.subtract's Python binding return anintinstead of aJLong? Or to define a method__sub__such that subtracting two addresses (witha1 - a2) returns a Pythonint?What do you think?
Beta Was this translation helpful? Give feedback.
All reactions