diff --git a/src/flb_utils.c b/src/flb_utils.c index 87637f1331d..2f445980d84 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -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; @@ -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; @@ -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; @@ -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) diff --git a/tests/internal/utils.c b/tests/internal/utils.c index beec9099ecc..bcb9053d86f 100644 --- a/tests/internal/utils.c +++ b/tests/internal/utils.c @@ -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 }, @@ -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 } };