Fix unchecked allocations#2844
Conversation
|
|
||
| if (*cache == NULL) | ||
| *cache = make_id2insn(insns, max); | ||
| if (!(*cache = make_id2insn(insns, max))) |
There was a problem hiding this comment.
There are only two places where handle->insn_id is actually called. Wouldn't it be way simple to just initialize the cache in cs_disasm and cs_disasm_iter?
This would save us all the other changes in the other functions.
There was a problem hiding this comment.
I can move the checked allocation into a new routine which is common between cs_disasm and cs_disasm_iter...
That way the arch-specific ID-translation code can just assume that any needed cache has already been allocated.
But this does mean that we'll need to provide a way for the architecture-init code to signal how much cache it's associated translation table needs.
That will basically entail a new handle->required_insn_cache_size field (which will be zero for archetectures that don't need it).
Does that work for you?
There was a problem hiding this comment.
Yes, this makes sense.
To give some context. The reason I'd like to have as few allocations in the modules as possible is for removing allocations completely in the (far) future.
This makes it potentially easier to refactor then, if we want to introduce a stack only disassembly logic.
There was a problem hiding this comment.
OK, bc5f6b2 should address this.
Lemmie know if that's sufficient.
6fa69eb to
bc5f6b2
Compare
|
Looked briefly and seems good. But can you please fix the build? |
bc5f6b2 to
2a419b6
Compare
|
A missing semicolon? |
|
No problem
Makefile is deprecated though (we still run and fix the build, but don't extend it for developer tasks). I'd recommend to switch to cmake. |
|
@Grond66 There are memory issues. Please rebase though before. I just merged a big RISC-V update. |
2a419b6 to
b2ab68d
Compare
|
OK, looks like the memory safety issue is now fixed. |
Can you tell me what the issue was? Did the build fail, or were docs missing? |
Rot127
left a comment
There was a problem hiding this comment.
lgtm!
Thanks a lot for refactoring it.
Just have those nitpicks.
After those it is good to merge.
|
@Grond66 I want to make a release over the next week. Having your addition in there would be really nice. Do you think you will find time to finish it? It is not much left. |
No, one of the python tests failed. I think it was because it involved doing |
Co-authored-by: Rot127 <45763064+Rot127@users.noreply.github.com>
Your checklist for this pull request
Detailed description
Capstone's source has a few places where memory is allocated, but there are no checks for out-of-memory (OOM) conditions. This can cause problems in memory-constrained environments; instead of having an operation error out with
CS_ERR_MEM, we get a null-pointer dereference. This is fairly simple to fix, but it does touch quite a few files. Please let me know if you want any improvements or further explanation of the fixes.Test plan
The standard test suite should do fine.
Closing issues
None, but I can open a tracking issue if that's helpful.