Skip to content

Commit

Permalink
utils: floating-point number support in size-to-bytes conversion (#8767)
Browse files Browse the repository at this point in the history

---------

Signed-off-by: Meet <meetp0878@gmail.com>
  • Loading branch information
Meetp369 authored Jun 25, 2024
1 parent d4735ac commit 456c305
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ int64_t flb_utils_size_to_bytes(const char *size)
int i;
int len;
int plen = 0;
int64_t val;
double val;
char c;
char tmp[3] = {0};
int64_t KB = 1000;
Expand All @@ -538,7 +538,7 @@ int64_t flb_utils_size_to_bytes(const char *size)
}

len = strlen(size);
val = atoll(size);
val = atof(size);

if (len == 0) {
return -1;
Expand All @@ -554,7 +554,7 @@ int64_t flb_utils_size_to_bytes(const char *size)
}

if (plen == 0) {
return val;
return (int64_t)val;
}
else if (plen > 2) {
return -1;
Expand All @@ -577,27 +577,27 @@ int64_t flb_utils_size_to_bytes(const char *size)
{
return -1;
}
return (val * KB);
return (int64_t)(val * KB);
}
else if (tmp[0] == 'M') {
/* set upper bound (2**64/MB)/2 to avoid overflows */
if (val >= 9223372036854 || val <= -9223372036853) {
return -1;
}
return (val * MB);
return (int64_t)(val * MB);
}
else if (tmp[0] == 'G') {
/* set upper bound (2**64/GB)/2 to avoid overflows */
if (val >= 9223372036 || val <= -9223372035) {
return -1;
}
return (val * GB);
return (int64_t)(val * GB);
}
else {
return -1;
}

return val;
return (int64_t)val;
}

int64_t flb_utils_hex2int(char *hex, int len)
Expand Down
37 changes: 37 additions & 0 deletions tests/internal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,42 @@ void test_flb_utils_get_machine_id()
flb_free(id);
}

struct size_to_bytes_check {
char *size; /* size in string */
int64_t ret; /* expected size */
};

struct size_to_bytes_check size_to_bytes_checks[] = {
{"922337.63", 922337},
{"2K",2000},
{"5.7263K", 5726},
{"9223372036854775.23K", -1},
{"1M", 1000000},
{"1.1M", 1100000},
{"3.592M", 3592000},
{"52.752383M", 52752383},
{"9223372036854.42M", -1},
{"492.364G",492364000000},
{"1.2973G", 1297300000},
{"9223372036.78G", -1},
};

void test_size_to_bytes()
{
int i;
int size;
int64_t ret;
struct size_to_bytes_check *u;

size = sizeof(size_to_bytes_checks) / sizeof(struct size_to_bytes_check);
for (i = 0; i < size; i++) {
u = &size_to_bytes_checks[i];

ret = flb_utils_size_to_bytes(u->size);
TEST_CHECK(ret == u->ret);
}
}

TEST_LIST = {
/* JSON maps iteration */
{ "url_split", test_url_split },
Expand All @@ -632,5 +668,6 @@ TEST_LIST = {
{ "test_flb_utils_split_quoted", test_flb_utils_split_quoted},
{ "test_flb_utils_split_quoted_errors", test_flb_utils_split_quoted_errors},
{ "test_flb_utils_get_machine_id", test_flb_utils_get_machine_id },
{ "test_size_to_bytes", test_size_to_bytes },
{ 0 }
};

0 comments on commit 456c305

Please sign in to comment.