1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 14:33:52 +01:00

Merge pull request #8224 from OTAkeys/pr/fix_fmt_u32_dec

sys/fmt: fix overflow in fmt_u32_dec()
This commit is contained in:
Joakim Nohlgård 2017-12-08 13:33:32 +01:00 committed by GitHub
commit ea4d5e6506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -164,8 +164,14 @@ size_t fmt_u32_dec(char *out, uint32_t val)
size_t len = 1;
/* count needed characters */
for (uint32_t tmp = 10; tmp <= val; len++) {
tmp *= 10;
/* avoid multiply overflow: uint32_t max len = 10 digits */
if (val >= 1000000000ul) {
len = 10;
}
else {
for (uint32_t tmp = 10; tmp <= val; len++) {
tmp *= 10;
}
}
if (out) {

View File

@ -81,7 +81,7 @@ static void test_fmt_u64_hex(void)
static void test_fmt_u32_dec(void)
{
char out[9] = "--------";
char out[11] = "----------";
uint32_t val = 12345678;
uint8_t chars = 0;
@ -89,6 +89,13 @@ static void test_fmt_u32_dec(void)
TEST_ASSERT_EQUAL_INT(8, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("12345678", (char *) out);
memset(out, '-', sizeof(out));
val = 1234567890;
chars = fmt_u32_dec(out, val);
TEST_ASSERT_EQUAL_INT(10, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("1234567890", (char *) out);
}
static void test_fmt_u16_dec(void)