Skip to content

Commit

Permalink
Avoid memcpy_s() in putinthex
Browse files Browse the repository at this point in the history
- Use a define for size of buffer.
- Avoid using memcpy_s() or memcpy_s(). Just set directly in the
  buffer.
  • Loading branch information
mchack-work committed Mar 5, 2025
1 parent be62731 commit 554f3b5
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions libcommon/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,23 +261,26 @@ void puthex(enum ioend dest, const uint8_t c)
write(dest, hexbuf, 2);
}

// Size of of a maximum integer in hex text format
#define INTBUFSIZE 10

void putinthex(enum ioend dest, const uint32_t n)
{
uint8_t buf[10];
uint8_t hexbuf[2];
uint8_t buf[INTBUFSIZE] = {0};
uint8_t hexbuf[2] = {0};
uint8_t *intbuf = (uint8_t *)&n;
int j = 0;

buf[0] = '0';
buf[1] = 'x';
buf[j++] = '0';
buf[j++] = 'x';

int j = 2;
for (int i = 3; i > -1; i--) {
hex(hexbuf, intbuf[i]);
memcpy_s(&buf[j], 10 - j, hexbuf, 2);
j += 2;
buf[j++] = hexbuf[0];
buf[j++] = hexbuf[1];
}

write(dest, buf, 10);
write(dest, buf, INTBUFSIZE);
}

void puts(enum ioend dest, const char *s)
Expand Down

0 comments on commit 554f3b5

Please sign in to comment.