Since a local variable decimals is unsigned int, this conditional
if (decimals <= 1)
needs cast as shown below.
if ((int)decimals <= 1)
Otherwise it results in showing some garbage in case of maxlen == 0,
# cat ./test0.c
#include <stdio.h>
#include "lib/num2str.h"
int main(void) {
printf("%s\n", num2str(123, 3, 1, 1, N2S_BYTE));
printf("%s\n", num2str(123, 2, 1, 1, N2S_BYTE));
printf("%s\n", num2str(123, 1, 1, 1, N2S_BYTE));
printf("%s\n", num2str(123, 0, 1, 1, N2S_BYTE));
return 0;
}
# gcc -Wall -g -DCONFIG_STATIC_ASSERT ./test0.c ./lib/num2str.c
# ./a.out
123B
0KiB
0KiB
0.0?$-JB
# ./a.out
123B
0KiB
0KiB
0.0#;bB
# ./a.out
123B
0KiB
0KiB
0.0|B
whereas with this commit it prints "0B".
# gcc -Wall -g -DCONFIG_STATIC_ASSERT ./test0.c ./lib/num2str.c
# ./a.out
123B
0KiB
0KiB
0B
Signed-off-by: Tomohiro Kusumi <tkusumi@tuxera.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
sprintf(tmp, "%llu", (unsigned long long) num);
decimals = maxlen - strlen(tmp);
- if (decimals <= 1) {
+ if ((int)decimals <= 1) {
if (carry)
num++;
goto done;