Skip to content

Commit

Permalink
correct some comments in cs_disasm()
Browse files Browse the repository at this point in the history
  • Loading branch information
aquynh committed Oct 2, 2014
1 parent 0d1aad1 commit f9d8a89
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,13 @@ size_t cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, si

next_offset = insn_size;
} else {
// encounter a broken instruction

// free memory of @detail pointer
if (handle->detail) {
// free memory of @detail pointer
cs_mem_free(insn_cache->detail);
}

// encounter a broken instruction
// if there is no request to skip data, or remaining data is too small,
// then bail out
if (!handle->skipdata || handle->skipdata_size > size)
Expand Down Expand Up @@ -519,13 +520,16 @@ size_t cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, si

next_offset = skipdata_bytes;
}

// one more instruction entering the cache
f++;

// one more instruction disassembled
c++;
if (count > 0 && c == count)
// disasm requested number of instructions
break;

if (f == cache_size) {
// full cache, so resize total to contain next disasm insns
cache_size = cache_size << 2 / 5; // * 1.6 ~ golden ratio
Expand Down Expand Up @@ -558,11 +562,11 @@ size_t cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, si
}

if (!c) {
// we did not disassemble any instruction
cs_mem_free(total);
total = NULL;
} else if (f != cache_size) {
// no need to resize the cache if f == cache_size
// resize total to contain newly disasm insns
// total did not fully use the last cache, so downsize it
void *tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
if (tmp == NULL) { // insufficient memory
// free all detail pointers
Expand Down

0 comments on commit f9d8a89

Please sign in to comment.