From babec0fafa7141515c606790947f66bde46396b8 Mon Sep 17 00:00:00 2001 From: Daniel Anselmi Date: Tue, 11 Apr 2023 10:18:33 +0200 Subject: [PATCH] server/ipdbg: add error checks after allocating memory Change-Id: Icf18a855eb66d2b09789a9ee27f5fbc4cd9afc89 Signed-off-by: Daniel Anselmi Reviewed-on: https://review.openocd.org/c/openocd/+/7605 Tested-by: jenkins Reviewed-by: Antonio Borneo --- src/server/ipdbg.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/server/ipdbg.c b/src/server/ipdbg.c index 755d0510e..69d0f5755 100644 --- a/src/server/ipdbg.c +++ b/src/server/ipdbg.c @@ -315,6 +315,10 @@ static int ipdbg_shift_instr(struct ipdbg_hub *hub, uint32_t instr) } uint8_t *ir_out_val = calloc(DIV_ROUND_UP(tap->ir_length, 8), 1); + if (!ir_out_val) { + LOG_ERROR("Out of memory"); + return ERROR_FAIL; + } buf_set_u32(ir_out_val, 0, tap->ir_length, instr); struct scan_field fields; @@ -344,6 +348,10 @@ static int ipdbg_shift_vir(struct ipdbg_hub *hub) return ERROR_FAIL; uint8_t *dr_out_val = calloc(DIV_ROUND_UP(hub->virtual_ir->length, 8), 1); + if (!dr_out_val) { + LOG_ERROR("Out of memory"); + return ERROR_FAIL; + } buf_set_u32(dr_out_val, 0, hub->virtual_ir->length, hub->virtual_ir->value); struct scan_field fields; @@ -366,8 +374,21 @@ static int ipdbg_shift_data(struct ipdbg_hub *hub, uint32_t dn_data, uint32_t *u return ERROR_FAIL; uint8_t *dr_out_val = calloc(DIV_ROUND_UP(hub->data_register_length, 8), 1); + if (!dr_out_val) { + LOG_ERROR("Out of memory"); + return ERROR_FAIL; + } buf_set_u32(dr_out_val, 0, hub->data_register_length, dn_data); - uint8_t *dr_in_val = up_data ? calloc(DIV_ROUND_UP(hub->data_register_length, 8), 1) : NULL; + + uint8_t *dr_in_val = NULL; + if (up_data) { + dr_in_val = calloc(DIV_ROUND_UP(hub->data_register_length, 8), 1); + if (!dr_in_val) { + LOG_ERROR("Out of memory"); + free(dr_out_val); + return ERROR_FAIL; + } + } struct scan_field fields; ipdbg_init_scan_field(&fields, dr_in_val, hub->data_register_length, dr_out_val);