Skip to content

Commit 20fcd07

Browse files
committed
jtag/tcl: fix incorrect memcpy in jim_newtap_expected_id
Found by clang static checker. On the very first call of jim_newtap_expected_id() pTap->expected_ids and expected_len are null, and there's nothing to copy. This patch changes this cryptic code to use realloc() instead. Change-Id: Ic0b5140d08257a906f15b55a2ae64db7bc06d5f1 Signed-off-by: Paul Fertser <fercerpav@gmail.com> Reviewed-on: http://openocd.zylin.com/2562 Reviewed-by: Stian Skjelstad <stian@nixia.no> Tested-by: jenkins
1 parent 2175bb1 commit 20fcd07

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/jtag/tcl.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,20 +434,15 @@ static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
434434
return e;
435435
}
436436

437-
unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
438-
uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
439-
if (new_expected_ids == NULL) {
437+
uint32_t *p = realloc(pTap->expected_ids,
438+
(pTap->expected_ids_cnt + 1) * sizeof(uint32_t));
439+
if (!p) {
440440
Jim_SetResultFormatted(goi->interp, "no memory");
441441
return JIM_ERR;
442442
}
443443

444-
memcpy(new_expected_ids, pTap->expected_ids, expected_len);
445-
446-
new_expected_ids[pTap->expected_ids_cnt] = w;
447-
448-
free(pTap->expected_ids);
449-
pTap->expected_ids = new_expected_ids;
450-
pTap->expected_ids_cnt++;
444+
pTap->expected_ids = p;
445+
pTap->expected_ids[pTap->expected_ids_cnt++] = w;
451446

452447
return JIM_OK;
453448
}

0 commit comments

Comments
 (0)