From 0a6aef6b6695537ac05bbe34b03907e3f1fe93d5 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Sat, 7 Dec 2019 11:25:52 +1030 Subject: [PATCH] S12Z disassembler memory leak * s12z-opc.c (exg_sex_discrim): Don't leak memory on invalid registers. --- opcodes/ChangeLog | 5 +++++ opcodes/s12z-opc.c | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/opcodes/ChangeLog b/opcodes/ChangeLog index 7832435374e..2183b2383fa 100644 --- a/opcodes/ChangeLog +++ b/opcodes/ChangeLog @@ -1,3 +1,8 @@ +2019-12-08 Alan Modra + + * s12z-opc.c (exg_sex_discrim): Don't leak memory on invalid + registers. + 2019-12-05 Jan Beulich * aarch64-tbl.h (aarch64_feature_crypto, diff --git a/opcodes/s12z-opc.c b/opcodes/s12z-opc.c index e7a3577ef7d..475c9eb5f1e 100644 --- a/opcodes/s12z-opc.c +++ b/opcodes/s12z-opc.c @@ -2195,32 +2195,32 @@ loop_prim_n_bytes (struct mem_read_abstraction_base *mra) static enum optr -exg_sex_discrim (struct mem_read_abstraction_base *mra, enum optr hint ATTRIBUTE_UNUSED) +exg_sex_discrim (struct mem_read_abstraction_base *mra, + enum optr hint ATTRIBUTE_UNUSED) { uint8_t eb; int status = mra->read (mra, 0, 1, &eb); + enum optr operator = OP_INVALID; if (status < 0) - return OP_INVALID; + return operator; struct operand *op0 = create_register_operand ((eb & 0xf0) >> 4); struct operand *op1 = create_register_operand (eb & 0xf); int reg0 = ((struct register_operand *) op0)->reg; - if (reg0 < 0 || reg0 >= S12Z_N_REGISTERS) - return OP_INVALID; - int reg1 = ((struct register_operand *) op1)->reg; - if (reg1 < 0 || reg1 >= S12Z_N_REGISTERS) - return OP_INVALID; - - const struct reg *r0 = registers + reg0; - const struct reg *r1 = registers + reg1; + if (reg0 >= 0 && reg0 < S12Z_N_REGISTERS + && reg1 >= 0 && reg1 < S12Z_N_REGISTERS) + { + const struct reg *r0 = registers + reg0; + const struct reg *r1 = registers + reg1; - enum optr operator = (r0->bytes < r1->bytes) ? OP_sex : OP_exg; + operator = r0->bytes < r1->bytes ? OP_sex : OP_exg; + } free (op0); free (op1); - + return operator; } -- 2.30.2