Skip to content

Commit

Permalink
Fix resource leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Nov 29, 2023
1 parent 5159346 commit 8a3a96c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 deletions handwritten/hexagon_arch_c/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ static HexPkt *hex_get_stale_pkt(HexState *state) {
stale_state_pkt = &state->pkts[i];
}
}
hex_clear_pkt(stale_state_pkt);
return stale_state_pkt;
}

Expand Down Expand Up @@ -253,30 +254,23 @@ RZ_API void hex_insn_free(RZ_NULLABLE HexInsn *i) {
* \param i The instruction container to be freed.
*/
RZ_API void hex_insn_container_free(RZ_NULLABLE HexInsnContainer *c) {
if (c && c->is_duplex) {
if (c) {
// bin is a union. Just free all of them.
hex_insn_free(c->bin.sub[0]);
hex_insn_free(c->bin.sub[1]);
} else if (c) {
hex_insn_free(c->bin.insn);
}
free(c);
}

/**
* \brief Copies one instruction container to another.
* \brief Moves one instruction container to another.
*
* \param dest The destination insruction container.
* \param src The source instruction container.
*/
RZ_API void hex_copy_insn_container(RZ_OUT HexInsnContainer *dest, const HexInsnContainer *src) {
RZ_API void hex_move_insn_container(RZ_OUT HexInsnContainer *dest, const HexInsnContainer *src) {
rz_return_if_fail(dest && src);
memcpy(dest, src, sizeof(HexInsnContainer));
if (src->is_duplex) {
memcpy(dest->bin.sub[0], src->bin.sub[0], sizeof(HexInsn));
memcpy(dest->bin.sub[1], src->bin.sub[1], sizeof(HexInsn));
} else {
memcpy(dest->bin.insn, src->bin.insn, sizeof(HexInsn));
}
memmove(dest, src, sizeof(HexInsnContainer));
}

/**
Expand Down Expand Up @@ -673,7 +667,8 @@ static HexInsnContainer *hex_add_to_pkt(HexState *state, const HexInsnContainer
RZ_LOG_FATAL("Instruction could not be set! A packet can only hold four instructions but k=%d.", k);
}
HexInsnContainer *hic = hexagon_alloc_instr_container();
hex_copy_insn_container(hic, new_hic);
hex_move_insn_container(hic, new_hic);
rz_list_del_n(pkt->bin, k);
rz_list_insert(pkt->bin, k, hic);

if (k == 0) {
Expand Down Expand Up @@ -706,7 +701,8 @@ static HexInsnContainer *hex_to_new_pkt(HexState *state, const HexInsnContainer
hex_clear_pkt(new_pkt);

HexInsnContainer *hic = hexagon_alloc_instr_container();
hex_copy_insn_container(hic, new_hic);
hex_move_insn_container(hic, new_hic);
rz_list_del_n(new_pkt->bin, 0);
rz_list_insert(new_pkt->bin, 0, hic);

new_pkt->last_instr_present |= is_last_instr(hic->parse_bits);
Expand Down Expand Up @@ -734,7 +730,7 @@ static HexInsnContainer *hex_add_to_stale_pkt(HexState *state, const HexInsnCont
hex_clear_pkt(pkt);

HexInsnContainer *hic = hexagon_alloc_instr_container();
hex_copy_insn_container(hic, new_hic);
hex_move_insn_container(hic, new_hic);
rz_list_insert(pkt->bin, 0, hic);

pkt->last_instr_present |= is_last_instr(hic->parse_bits);
Expand Down Expand Up @@ -821,6 +817,7 @@ static void print_state_pkt(const HexState *state, st32 index, HexBufferAction a
RZ_LOG_DEBUG("╰─────┴──────────────┴─────┴──────────────────┴───┴───┴───┴───╯\n");
}
}
rz_strbuf_free(pkt_line);
#endif
}

Expand Down Expand Up @@ -939,7 +936,10 @@ static void setup_new_hic(HexInsnContainer *hic, const HexReversedOpcode *rz_rev

hic->asm_op.size = 4;
hic->ana_op.size = 4;
hic->bin.sub[0] = NULL;
hic->bin.sub[1] = NULL;
if (parse_bits == 0b00) {
hic->is_duplex = true;
hic->bin.sub[0] = hexagon_alloc_instr();
hic->bin.sub[1] = hexagon_alloc_instr();
} else {
Expand Down
2 changes: 1 addition & 1 deletion handwritten/hexagon_arch_h/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ RZ_API HexLoopAttr hex_get_loop_flag(const HexPkt *p);
RZ_API const HexOp *hex_isa_to_reg(const HexInsn *hi, const char isa_id, bool new_reg);
RZ_API ut64 hex_isa_to_imm(const HexInsn *hi, const char isa_id);
void hex_set_hic_text(RZ_INOUT HexInsnContainer *hic);
RZ_API void hex_copy_insn_container(RZ_OUT HexInsnContainer *dest, const HexInsnContainer *src);
RZ_API void hex_move_insn_container(RZ_OUT HexInsnContainer *dest, const HexInsnContainer *src);
RZ_API HexPkt *hex_get_pkt(RZ_BORROW HexState *state, const ut32 addr);
RZ_API HexInsnContainer *hex_get_hic_at_addr(HexState *state, const ut32 addr);
RZ_API const HexOp hex_nreg_to_op(const HexInsnPktBundle *bundle, const char isa_id);
3 changes: 1 addition & 2 deletions handwritten/hexagon_disas_c/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ static void hex_disasm_with_templates(const HexInsnTemplate *tpl, HexState *stat
rz_strbuf_append_n(&sb, tpl->syntax + syntax_cur, syntax_len - syntax_cur);
}
strncpy(hi->text_infix, rz_strbuf_get(&sb), sizeof(hi->text_infix) - 1);
rz_strbuf_fini(&sb);

// RzAnalysisOp contents
hic->ana_op.addr = hic->addr;
Expand Down Expand Up @@ -357,8 +358,6 @@ int hexagon_disasm_instruction(HexState *state, const ut32 hi_u32, RZ_INOUT HexI
}
if (hic->identifier == HEX_INS_INVALID_DECODE) {
hic->ana_op.type = RZ_ANALYSIS_OP_TYPE_ILL;
HexInsn *hi = hexagon_alloc_instr();
hic->bin.insn = hi;
snprintf(hic->bin.insn->text_infix, sizeof(hic->bin.insn->text_infix), "invalid");
}
hex_set_hic_text(hic);
Expand Down
1 change: 1 addition & 0 deletions handwritten/hexagon_il_c/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ static RZ_OWN RzILOpEffect *hex_pkt_to_il_seq(HexPkt *pkt) {
if (rz_vector_len(pkt->il_ops) == 1) {
// We need at least the instruction op and the packet commit.
// So if there aren't at least two ops something went wrong.
rz_vector_clear(pkt->il_ops);
RZ_LOG_WARN("Invalid il ops sequence! There should be at least two il ops per packet.\n");
return NULL;
}
Expand Down

0 comments on commit 8a3a96c

Please sign in to comment.