helper/binarybuffer: fix clang static analyzer warnings 83/5383/3
authorTomas Vanek <vanekt@fbl.cz>
Fri, 20 Dec 2019 22:56:08 +0000 (23:56 +0100)
committerTomas Vanek <vanekt@fbl.cz>
Sat, 7 Mar 2020 15:30:05 +0000 (15:30 +0000)
Writing bits to an uninitialized buffer generated false warnings.
Zero buffers before setting them by buf_set_u32|64()
(do it only if bit-by-bit copy loop is used,
zeroed buffer is not necessary if a fast path write is used)

Change-Id: I2f7f8ddb45b0cbd08d3e249534fc51f4b5cc6694
Signed-off-by: Tomas Vanek <vanekt@fbl.cz>
Reviewed-on: http://openocd.zylin.com/5383
Tested-by: jenkins
Reviewed-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
16 files changed:
src/flash/nor/jtagspi.c
src/helper/binarybuffer.h
src/jtag/core.c
src/jtag/tcl.c
src/target/arm_jtag.c
src/target/avr32_jtag.c
src/target/esirisc_jtag.c
src/target/etb.c
src/target/etm.c
src/target/ls1_sap.c
src/target/mips_ejtag.c
src/target/openrisc/or1k_tap_vjtag.c
src/target/riscv/riscv-011.c
src/target/riscv/riscv-013.c
src/target/riscv/riscv.c
src/target/xscale.c

index a9f2dd4a4b8e0111bed8f01678933eb351b1de9b..f6e311ab820c22e25ab2adf36805ec374834b334 100644 (file)
@@ -59,7 +59,7 @@ static void jtagspi_set_ir(struct flash_bank *bank)
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
        struct scan_field field;
-       uint8_t buf[4];
+       uint8_t buf[4] = { 0 };
 
        LOG_DEBUG("loading jtagspi ir");
        buf_set_u32(buf, 0, info->tap->ir_length, info->ir);
index 7ac221e47b345e84c3dd2673097bc4a5b561a729..3f2481d9a215e7d6e48cb634f1ee5ad32cda6fbb 100644 (file)
@@ -33,6 +33,7 @@
  * using the bits in @c value.  This routine fast-paths writes
  * of little-endian, byte-aligned, 32-bit words.
  * @param _buffer The buffer whose bits will be set.
+ *     Do not use uninitialized buffer or clang static analyzer emits a warning.
  * @param first The bit offset in @c _buffer to start writing (0-31).
  * @param num The number of bits from @c value to copy (1-32).
  * @param value Up to 32 bits that will be copied to _buffer.
@@ -62,6 +63,7 @@ static inline void buf_set_u32(uint8_t *_buffer,
  * using the bits in @c value.  This routine fast-paths writes
  * of little-endian, byte-aligned, 64-bit words.
  * @param _buffer The buffer whose bits will be set.
+ *     Do not use uninitialized buffer or clang static analyzer emits a warning.
  * @param first The bit offset in @c _buffer to start writing (0-63).
  * @param num The number of bits from @c value to copy (1-64).
  * @param value Up to 64 bits that will be copied to _buffer.
index 1d59712d17fa099d9b82770f7185ebc431d13140..c5011e5226850e0061de1d70a01cdde261d54c60 100644 (file)
@@ -1233,7 +1233,7 @@ static int jtag_examine_chain(void)
        /* Add room for end-of-chain marker. */
        max_taps++;
 
-       uint8_t *idcode_buffer = malloc(max_taps * 4);
+       uint8_t *idcode_buffer = calloc(4, max_taps);
        if (idcode_buffer == NULL)
                return ERROR_JTAG_INIT_FAILED;
 
index ba0cb1d1ecb683581d527d72545bcda040ee4875..734b9c1cbd4c75f71fde13e257ee4a6edb87687d 100644 (file)
@@ -1131,7 +1131,7 @@ COMMAND_HANDLER(handle_irscan_command)
                }
                int field_size = tap->ir_length;
                fields[i].num_bits = field_size;
-               uint8_t *v = malloc(DIV_ROUND_UP(field_size, 8));
+               uint8_t *v = calloc(1, DIV_ROUND_UP(field_size, 8));
 
                uint64_t value;
                retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
index 9b73d4ea8439a80c7b6cb41a3f2c02e59dda9a3b..49aca3487b6448133a44424432cbd2686a96f8eb 100644 (file)
@@ -33,7 +33,7 @@ int arm_jtag_set_instr_inner(struct jtag_tap *tap,
                uint32_t new_instr, void *no_verify_capture, tap_state_t end_state)
 {
        struct scan_field field;
-       uint8_t t[4];
+       uint8_t t[4] = { 0 };
 
        field.num_bits = tap->ir_length;
        field.out_value = t;
@@ -56,7 +56,7 @@ int arm_jtag_scann_inner(struct arm_jtag *jtag_info, uint32_t new_scan_chain, ta
 {
        int retval = ERROR_OK;
 
-       uint8_t out_value[4];
+       uint8_t out_value[4] = { 0 };
        buf_set_u32(out_value, 0, jtag_info->scann_size, new_scan_chain);
        struct scan_field field = { .num_bits = jtag_info->scann_size, .out_value = out_value, };
 
index c17fbe7f02f14f20b1121a1057ca7fb049697c8b..6a4d4b3e7ca02bb443d869cb9bbdec5188fae9df 100644 (file)
@@ -35,7 +35,7 @@ static int avr32_jtag_set_instr(struct avr32_jtag *jtag_info, int new_instr)
        if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
                do {
                        struct scan_field field;
-                       uint8_t t[4];
+                       uint8_t t[4] = { 0 };
                        uint8_t ret[4];
 
                        field.num_bits = tap->ir_length;
index 333a622252450b65f11ae380836143fa2e55315d..700ae3a60dfb6b6bc0a15e241f99a9b6620687e7 100644 (file)
@@ -36,7 +36,7 @@ static void esirisc_jtag_set_instr(struct esirisc_jtag *jtag_info, uint32_t new_
 
        if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
                struct scan_field field;
-               uint8_t t[4];
+               uint8_t t[4] = { 0 };
 
                field.num_bits = tap->ir_length;
                field.out_value = t;
index 392c6ad7fe11febb3b366b8b84921e39f9cda080..0c03c4dbec8a9b4bfdfe5307bb4048987d594500 100644 (file)
@@ -176,13 +176,13 @@ static int etb_read_ram(struct etb *etb, uint32_t *data, int num_frames)
        fields[0].in_value = NULL;
 
        fields[1].num_bits = 7;
-       uint8_t temp1;
+       uint8_t temp1 = 0;
        fields[1].out_value = &temp1;
        buf_set_u32(&temp1, 0, 7, 4);
        fields[1].in_value = NULL;
 
        fields[2].num_bits = 1;
-       uint8_t temp2;
+       uint8_t temp2 = 0;
        fields[2].out_value = &temp2;
        buf_set_u32(&temp2, 0, 1, 0);
        fields[2].in_value = NULL;
@@ -229,7 +229,7 @@ static int etb_read_reg_w_check(struct reg *reg,
        fields[0].check_mask = NULL;
 
        fields[1].num_bits = 7;
-       uint8_t temp1;
+       uint8_t temp1 = 0;
        fields[1].out_value = &temp1;
        buf_set_u32(&temp1, 0, 7, reg_addr);
        fields[1].in_value = NULL;
@@ -237,7 +237,7 @@ static int etb_read_reg_w_check(struct reg *reg,
        fields[1].check_mask = NULL;
 
        fields[2].num_bits = 1;
-       uint8_t temp2;
+       uint8_t temp2 = 0;
        fields[2].out_value = &temp2;
        buf_set_u32(&temp2, 0, 1, 0);
        fields[2].in_value = NULL;
@@ -310,13 +310,13 @@ static int etb_write_reg(struct reg *reg, uint32_t value)
        fields[0].in_value = NULL;
 
        fields[1].num_bits = 7;
-       uint8_t temp1;
+       uint8_t temp1 = 0;
        fields[1].out_value = &temp1;
        buf_set_u32(&temp1, 0, 7, reg_addr);
        fields[1].in_value = NULL;
 
        fields[2].num_bits = 1;
-       uint8_t temp2;
+       uint8_t temp2 = 0;
        fields[2].out_value = &temp2;
        buf_set_u32(&temp2, 0, 1, 1);
        fields[2].in_value = NULL;
index d1cfe61f693e17d7a02599391ab62bdfc770595a..5218a9e480bfb19f0fac9998c7ff2a46e56dc940 100644 (file)
@@ -533,7 +533,7 @@ static int etm_read_reg_w_check(struct reg *reg,
        fields[0].check_mask = NULL;
 
        fields[1].num_bits = 7;
-       uint8_t temp1;
+       uint8_t temp1 = 0;
        fields[1].out_value = &temp1;
        buf_set_u32(&temp1, 0, 7, reg_addr);
        fields[1].in_value = NULL;
@@ -541,7 +541,7 @@ static int etm_read_reg_w_check(struct reg *reg,
        fields[1].check_mask = NULL;
 
        fields[2].num_bits = 1;
-       uint8_t temp2;
+       uint8_t temp2 = 0;
        fields[2].out_value = &temp2;
        buf_set_u32(&temp2, 0, 1, 0);
        fields[2].in_value = NULL;
@@ -620,13 +620,13 @@ static int etm_write_reg(struct reg *reg, uint32_t value)
        fields[0].in_value = NULL;
 
        fields[1].num_bits = 7;
-       uint8_t tmp2;
+       uint8_t tmp2 = 0;
        fields[1].out_value = &tmp2;
        buf_set_u32(&tmp2, 0, 7, reg_addr);
        fields[1].in_value = NULL;
 
        fields[2].num_bits = 1;
-       uint8_t tmp3;
+       uint8_t tmp3 = 0;
        fields[2].out_value = &tmp3;
        buf_set_u32(&tmp3, 0, 1, 1);
        fields[2].in_value = NULL;
index bc46ed4db814c1f5101d6f0dffa98827a5190378..330042f00fdfd8448df50e373f8cb622557f100e 100644 (file)
@@ -113,7 +113,7 @@ static void ls1_sap_set_instr(struct jtag_tap *tap, uint32_t new_instr)
 static void ls1_sap_set_addr_high(struct jtag_tap *tap, uint16_t addr_high)
 {
        struct scan_field field;
-       uint8_t buf[2];
+       uint8_t buf[2] = { 0 };
 
        ls1_sap_set_instr(tap, 0x21);
 
@@ -130,7 +130,7 @@ static void ls1_sap_memory_cmd(struct jtag_tap *tap, uint32_t address,
                               int32_t size, bool rnw)
 {
        struct scan_field field;
-       uint8_t cmd[8];
+       uint8_t cmd[8] = { 0 };
 
        ls1_sap_set_instr(tap, 0x24);
 
index 6d35e211dde943fa39e4bd94521c5986c9660de8..00bafd0336eee7bfd6379b754c85abc417d1b291 100644 (file)
@@ -43,7 +43,7 @@ void mips_ejtag_set_instr(struct mips_ejtag *ejtag_info, uint32_t new_instr)
                struct scan_field field;
                field.num_bits = tap->ir_length;
 
-               uint8_t t[4];
+               uint8_t t[4] = { 0 };
                field.out_value = t;
                buf_set_u32(t, 0, field.num_bits, new_instr);
 
@@ -100,7 +100,7 @@ int mips_ejtag_drscan_64(struct mips_ejtag *ejtag_info, uint64_t *data)
        if (tap == NULL)
                return ERROR_FAIL;
        struct scan_field field;
-       uint8_t t[8], r[8];
+       uint8_t t[8] = { 0 }, r[8];
        int retval;
 
        field.num_bits = 64;
@@ -130,7 +130,7 @@ void mips_ejtag_drscan_32_queued(struct mips_ejtag *ejtag_info, uint32_t data_ou
        struct scan_field field;
        field.num_bits = 32;
 
-       uint8_t scan_out[4];
+       uint8_t scan_out[4] = { 0 };
        field.out_value = scan_out;
        buf_set_u32(scan_out, 0, field.num_bits, data_out);
 
index 607451a7c2f1d5a0ee9bcc9b5b75ab3fd5b72898..db10f103b363ed17575afedf0738e257576d052a 100644 (file)
@@ -149,7 +149,7 @@ static int or1k_tap_vjtag_init(struct or1k_jtag *jtag_info)
         * into the USER1 DR is sufficient to cover the most conservative case for m and n.
         */
 
-       uint8_t t[4];
+       uint8_t t[4] = { 0 };
        struct scan_field field;
        struct jtag_tap *tap = jtag_info->tap;
 
index eded86246cc21f8c51d104e059051a4fe497412d..cb7b744da58244b4243beb112588ab1a536ca152 100644 (file)
@@ -280,7 +280,7 @@ static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
 {
        struct scan_field field;
        uint8_t in_value[4];
-       uint8_t out_value[4];
+       uint8_t out_value[4] = { 0 };
 
        buf_set_u32(out_value, 0, 32, out);
 
@@ -422,7 +422,7 @@ static dbus_status_t dbus_scan(struct target *target, uint16_t *address_in,
 {
        riscv011_info_t *info = get_info(target);
        uint8_t in[8] = {0};
-       uint8_t out[8];
+       uint8_t out[8] = {0};
        struct scan_field field = {
                .num_bits = info->addrbits + DBUS_OP_SIZE + DBUS_DATA_SIZE,
                .out_value = out,
index 1e5c027647791b7a3de7551d12cf2cd8bc4860b7..66218b76ead8cc50c9cc5b892f956517e4b0cf9a 100644 (file)
@@ -402,7 +402,7 @@ static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
 {
        struct scan_field field;
        uint8_t in_value[4];
-       uint8_t out_value[4];
+       uint8_t out_value[4] = { 0 };
 
        buf_set_u32(out_value, 0, 32, out);
 
@@ -468,6 +468,7 @@ static dmi_status_t dmi_scan(struct target *target, uint32_t *address_in,
        }
 
        memset(in, 0, num_bytes);
+       memset(out, 0, num_bytes);
 
        assert(info->abits != 0);
 
index 8b5a361bb8cd019d045f210f1a2553d07b239e39..1d6f66699b57c17b6f833a4f7d2c1c79bd6ddac7 100644 (file)
@@ -203,7 +203,7 @@ static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
 {
        struct scan_field field;
        uint8_t in_value[4];
-       uint8_t out_value[4];
+       uint8_t out_value[4] = { 0 };
 
        buf_set_u32(out_value, 0, 32, out);
 
@@ -540,7 +540,7 @@ int riscv_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
                        return ERROR_FAIL;
                }
 
-               uint8_t buff[4];
+               uint8_t buff[4] = { 0 };
                buf_set_u32(buff, 0, breakpoint->length * CHAR_BIT, breakpoint->length == 4 ? ebreak() : ebreak_c());
                int const retval = target_write_memory(target, breakpoint->address, 2, breakpoint->length / 2, buff);
 
@@ -1047,7 +1047,7 @@ static int riscv_run_algorithm(struct target *target, int num_mem_params,
 
        /* Disable Interrupts before attempting to run the algorithm. */
        uint64_t current_mstatus;
-       uint8_t mstatus_bytes[8];
+       uint8_t mstatus_bytes[8] = { 0 };
 
        LOG_DEBUG("Disabling Interrupts");
        struct reg *reg_mstatus = register_get_by_name(target->reg_cache,
@@ -1103,7 +1103,7 @@ static int riscv_run_algorithm(struct target *target, int num_mem_params,
        reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
 
        /* Restore registers */
-       uint8_t buf[8];
+       uint8_t buf[8] = { 0 };
        buf_set_u64(buf, 0, info->xlen[0], saved_pc);
        if (reg_pc->type->set(reg_pc, buf) != ERROR_OK)
                return ERROR_FAIL;
index 3ef8922b529eb568f8e5857295f8ac6f6b05c40d..e57996585ed2a33e6864f78082653fbd42cbad0f 100644 (file)
@@ -129,7 +129,7 @@ static const struct xscale_reg xscale_reg_arch_info[] = {
 /* convenience wrapper to access XScale specific registers */
 static int xscale_set_reg_u32(struct reg *reg, uint32_t value)
 {
-       uint8_t buf[4];
+       uint8_t buf[4] = { 0 };
 
        buf_set_u32(buf, 0, 32, value);
 
@@ -154,7 +154,7 @@ static int xscale_jtag_set_instr(struct jtag_tap *tap, uint32_t new_instr, tap_s
 
        if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
                struct scan_field field;
-               uint8_t scratch[4];
+               uint8_t scratch[4] = { 0 };
 
                memset(&field, 0, sizeof field);
                field.num_bits = tap->ir_length;
@@ -514,7 +514,7 @@ static int xscale_send(struct target *target, const uint8_t *buffer, int count,
                TAP_IDLE);
 
        static const uint8_t t0;
-       uint8_t t1[4];
+       uint8_t t1[4] = { 0 };
        static const uint8_t t2 = 1;
        struct scan_field fields[3] = {
                        { .num_bits = 3, .out_value = &t0 },
@@ -645,8 +645,8 @@ static unsigned int parity(unsigned int v)
 static int xscale_load_ic(struct target *target, uint32_t va, uint32_t buffer[8])
 {
        struct xscale_common *xscale = target_to_xscale(target);
-       uint8_t packet[4];
-       uint8_t cmd;
+       uint8_t packet[4] = { 0 };
+       uint8_t cmd = 0;
        int word;
        struct scan_field fields[2];
 
@@ -699,8 +699,8 @@ static int xscale_load_ic(struct target *target, uint32_t va, uint32_t buffer[8]
 static int xscale_invalidate_ic_line(struct target *target, uint32_t va)
 {
        struct xscale_common *xscale = target_to_xscale(target);
-       uint8_t packet[4];
-       uint8_t cmd;
+       uint8_t packet[4] = { 0 };
+       uint8_t cmd = 0;
        struct scan_field fields[2];
 
        xscale_jtag_set_instr(target->tap,

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)