Skip to content

Commit c913e4d

Browse files
committed
jtag: fix build with configure --enable-verbose
With flag --enable-verbose, configure enables compiling some conditional code that with new gcc triggers an error: error: '%04x' directive output may be truncated writing between 4 and 8 bytes into a region of size 5 [-Werror=format-truncation=] Extend the buffer to contain the full 8 bytes of %04x on a 'int' and change the limit in snprintf. Skip the intermediate buffer 's[4]'. Align the code to the coding style. Change-Id: Ifc8a6e4686555578a7355a1f6049471fd5e31913 Signed-off-by: Antonio Borneo <[email protected]> Reported-by: Karl Hammar <[email protected]> Reported-by: Tommy Murphy <[email protected]> Fixes: https://sourceforge.net/p/openocd/tickets/376/ Reviewed-on: https://review.openocd.org/c/openocd/+/7403 Tested-by: jenkins Reviewed-by: Tomas Vanek <[email protected]>
1 parent 77c7abe commit c913e4d

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

src/jtag/drivers/arm-jtag-ew.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -776,17 +776,12 @@ static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length)
776776

777777
static void armjtagew_debug_buffer(uint8_t *buffer, int length)
778778
{
779-
char line[81];
780-
char s[4];
781-
int i;
782-
int j;
779+
char line[8 + 3 * BYTES_PER_LINE + 1];
783780

784-
for (i = 0; i < length; i += BYTES_PER_LINE) {
785-
snprintf(line, 5, "%04x", i);
786-
for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
787-
snprintf(s, 4, " %02x", buffer[j]);
788-
strcat(line, s);
789-
}
781+
for (int i = 0; i < length; i += BYTES_PER_LINE) {
782+
int n = snprintf(line, 9, "%04x", i);
783+
for (int j = i; j < i + BYTES_PER_LINE && j < length; j++)
784+
n += snprintf(line + n, 4, " %02x", buffer[j]);
790785
LOG_DEBUG("%s", line);
791786

792787
/* Prevent GDB timeout (writing to log might take some time) */

src/jtag/drivers/opendous.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -796,17 +796,12 @@ int opendous_usb_read(struct opendous_jtag *opendous_jtag)
796796

797797
void opendous_debug_buffer(uint8_t *buffer, int length)
798798
{
799-
char line[81];
800-
char s[4];
801-
int i;
802-
int j;
799+
char line[8 + 3 * BYTES_PER_LINE + 1];
803800

804-
for (i = 0; i < length; i += BYTES_PER_LINE) {
805-
snprintf(line, 5, "%04x", i);
806-
for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
807-
snprintf(s, 4, " %02x", buffer[j]);
808-
strcat(line, s);
809-
}
801+
for (int i = 0; i < length; i += BYTES_PER_LINE) {
802+
int n = snprintf(line, 9, "%04x", i);
803+
for (int j = i; j < i + BYTES_PER_LINE && j < length; j++)
804+
n += snprintf(line + n, 4, " %02x", buffer[j]);
810805
LOG_DEBUG("%s", line);
811806
}
812807
}

0 commit comments

Comments
 (0)