Add FormatInt64 to convert signed integers in signal-safe manner
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Chase Douglas <chase.douglas@canonical.com>
This commit is contained in:
parent
36c1d92ec0
commit
7f8c39c8b5
|
@ -247,6 +247,7 @@ padding_for_int32(const int bytes)
|
||||||
|
|
||||||
|
|
||||||
extern char **xstrtokenize(const char *str, const char *separators);
|
extern char **xstrtokenize(const char *str, const char *separators);
|
||||||
|
extern void FormatInt64(int64_t num, char *string);
|
||||||
extern void FormatUInt64(uint64_t num, char *string);
|
extern void FormatUInt64(uint64_t num, char *string);
|
||||||
extern void FormatUInt64Hex(uint64_t num, char *string);
|
extern void FormatUInt64Hex(uint64_t num, char *string);
|
||||||
|
|
||||||
|
|
14
os/utils.c
14
os/utils.c
|
@ -1924,6 +1924,20 @@ xstrtokenize(const char *str, const char *separators)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Format a signed number into a string in a signal safe manner. The string
|
||||||
|
* should be at least 21 characters in order to handle all int64_t values.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
FormatInt64(int64_t num, char *string)
|
||||||
|
{
|
||||||
|
if (num < 0) {
|
||||||
|
string[0] = '-';
|
||||||
|
num *= -1;
|
||||||
|
string++;
|
||||||
|
}
|
||||||
|
FormatUInt64(num, string);
|
||||||
|
}
|
||||||
|
|
||||||
/* Format a number into a string in a signal safe manner. The string should be
|
/* Format a number into a string in a signal safe manner. The string should be
|
||||||
* at least 21 characters in order to handle all uint64_t values. */
|
* at least 21 characters in order to handle all uint64_t values. */
|
||||||
void
|
void
|
||||||
|
|
|
@ -36,6 +36,26 @@ struct number_format_test {
|
||||||
char hex_string[17];
|
char hex_string[17];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct signed_number_format_test {
|
||||||
|
int64_t number;
|
||||||
|
char string[21];
|
||||||
|
};
|
||||||
|
|
||||||
|
static Bool
|
||||||
|
check_signed_number_format_test(const struct signed_number_format_test *test)
|
||||||
|
{
|
||||||
|
char string[21];
|
||||||
|
|
||||||
|
FormatInt64(test->number, string);
|
||||||
|
if(strncmp(string, test->string, 21) != 0) {
|
||||||
|
fprintf(stderr, "Failed to convert %jd to decimal string (%s vs %s)\n",
|
||||||
|
test->number, test->string, string);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
static Bool
|
static Bool
|
||||||
check_number_format_test(const struct number_format_test *test)
|
check_number_format_test(const struct number_format_test *test)
|
||||||
{
|
{
|
||||||
|
@ -62,7 +82,7 @@ static void
|
||||||
number_formatting(void)
|
number_formatting(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct number_format_test tests[] = {
|
struct number_format_test unsigned_tests[] = {
|
||||||
{ /* Zero */
|
{ /* Zero */
|
||||||
0,
|
0,
|
||||||
"0",
|
"0",
|
||||||
|
@ -100,8 +120,62 @@ number_formatting(void)
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
|
struct signed_number_format_test signed_tests[] = {
|
||||||
assert(check_number_format_test(tests + i));
|
{ /* Zero */
|
||||||
|
0,
|
||||||
|
"0",
|
||||||
|
},
|
||||||
|
{ /* Single digit number */
|
||||||
|
5,
|
||||||
|
"5",
|
||||||
|
},
|
||||||
|
{ /* Two digit decimal number */
|
||||||
|
12,
|
||||||
|
"12",
|
||||||
|
},
|
||||||
|
{ /* Two digit hex number */
|
||||||
|
37,
|
||||||
|
"37",
|
||||||
|
},
|
||||||
|
{ /* Large < 32 bit number */
|
||||||
|
0xC90B2,
|
||||||
|
"823474",
|
||||||
|
},
|
||||||
|
{ /* Large > 32 bit number */
|
||||||
|
0x15D027BF211B37A,
|
||||||
|
"98237498237498234",
|
||||||
|
},
|
||||||
|
{ /* Maximum 64-bit signed number */
|
||||||
|
0x7FFFFFFFFFFFFFFF,
|
||||||
|
"9223372036854775807",
|
||||||
|
},
|
||||||
|
{ /* Single digit number */
|
||||||
|
-1,
|
||||||
|
"-1",
|
||||||
|
},
|
||||||
|
{ /* Two digit decimal number */
|
||||||
|
-12,
|
||||||
|
"-12",
|
||||||
|
},
|
||||||
|
{ /* Large < 32 bit number */
|
||||||
|
-0xC90B2,
|
||||||
|
"-823474",
|
||||||
|
},
|
||||||
|
{ /* Large > 32 bit number */
|
||||||
|
-0x15D027BF211B37A,
|
||||||
|
"-98237498237498234",
|
||||||
|
},
|
||||||
|
{ /* Maximum 64-bit number */
|
||||||
|
-0x7FFFFFFFFFFFFFFF,
|
||||||
|
"-9223372036854775807",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
for (i = 0; i < sizeof(unsigned_tests) / sizeof(unsigned_tests[0]); i++)
|
||||||
|
assert(check_number_format_test(unsigned_tests + i));
|
||||||
|
|
||||||
|
for (i = 0; i < sizeof(unsigned_tests) / sizeof(signed_tests[0]); i++)
|
||||||
|
assert(check_signed_number_format_test(signed_tests + i));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void logging_format(void)
|
static void logging_format(void)
|
||||||
|
|
Loading…
Reference in New Issue