From 65e5b0ab79a009216dbe4cc6338065c0185c661e Mon Sep 17 00:00:00 2001 From: Robert Manner Date: Tue, 8 Nov 2022 14:47:40 +0100 Subject: [PATCH] roken/parse_bytes: fix test for >= terabyte units on 32 bit systems On 32 bit systems, sizeof(ssize_t) and sizeof(unsigned long aka UL) is 32 bits which is not able to hold the value of a terabyte. --- lib/asn1/check-gen.c | 4 ++-- lib/roken/parse_bytes-test.c | 6 +++--- lib/roken/parse_bytes.c | 18 +++++++++--------- lib/roken/parse_bytes.h | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/asn1/check-gen.c b/lib/asn1/check-gen.c index bba5b9db2..098f781da 100644 --- a/lib/asn1/check-gen.c +++ b/lib/asn1/check-gen.c @@ -2162,9 +2162,9 @@ test_default(void) }; TESTDefault values[] = { - { "Heimdal", 8, 9223372036854775807, 1 }, + { "Heimdal", 8, 9223372036854775807LL, 1 }, { "heimdal", 7, 2147483647, 0 }, - { "Heimdal", 7, 9223372036854775807, 0 }, + { "Heimdal", 7, 9223372036854775807LL, 0 }, { "heimdal", 8, 2147483647, 1 }, }; int i, ret; diff --git a/lib/roken/parse_bytes-test.c b/lib/roken/parse_bytes-test.c index f7697e65d..8f3831cbb 100644 --- a/lib/roken/parse_bytes-test.c +++ b/lib/roken/parse_bytes-test.c @@ -38,7 +38,7 @@ static struct testcase { int canonicalp; - ssize_t val; + int64_t val; const char *def_unit; const char *str; } tests[] = { @@ -52,7 +52,7 @@ static struct testcase { {1, 1024 * 1024, NULL, "1 megabyte"}, {0, 1025, NULL, "1 kilobyte 1"}, {1, 1025, NULL, "1 kilobyte 1 byte"}, - {1, 1024UL * 1024 * 1024 * 1024, NULL, "1 terabyte"}, + {1, 1024ULL * 1024 * 1024 * 1024, NULL, "1 terabyte"}, }; int @@ -63,7 +63,7 @@ main(int argc, char **argv) for (i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) { char buf[256]; - ssize_t val = parse_bytes (tests[i].str, tests[i].def_unit); + int64_t val = parse_bytes (tests[i].str, tests[i].def_unit); if (val != tests[i].val) { printf ("parse_bytes (%s, %s) = %lld != %lld\n", diff --git a/lib/roken/parse_bytes.c b/lib/roken/parse_bytes.c index 006a3524b..01ad55308 100644 --- a/lib/roken/parse_bytes.c +++ b/lib/roken/parse_bytes.c @@ -37,10 +37,10 @@ #include "parse_bytes.h" static struct units bytes_units[] = { - { "petabyte", 1024UL * 1024 * 1024 * 1024 * 1024 }, - { "PB", 1024UL * 1024 * 1024 * 1024 * 1024 }, - { "terabyte", 1024UL * 1024 * 1024 * 1024 }, - { "TB", 1024UL * 1024 * 1024 * 1024 }, + { "petabyte", 1024ULL * 1024 * 1024 * 1024 * 1024 }, + { "PB", 1024ULL * 1024 * 1024 * 1024 * 1024 }, + { "terabyte", 1024ULL * 1024 * 1024 * 1024 }, + { "TB", 1024ULL * 1024 * 1024 * 1024 }, { "gigabyte", 1024 * 1024 * 1024 }, { "gbyte", 1024 * 1024 * 1024 }, { "GB", 1024 * 1024 * 1024 }, @@ -54,28 +54,28 @@ static struct units bytes_units[] = { }; static struct units bytes_short_units[] = { - { "PB", 1024UL * 1024 * 1024 * 1024 * 1024 }, - { "TB", 1024UL * 1024 * 1024 * 1024 }, + { "PB", 1024ULL * 1024 * 1024 * 1024 * 1024 }, + { "TB", 1024ULL * 1024 * 1024 * 1024 }, { "GB", 1024 * 1024 * 1024 }, { "MB", 1024 * 1024 }, { "KB", 1024 }, { NULL, 0 } }; -ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL +ROKEN_LIB_FUNCTION int64_t ROKEN_LIB_CALL parse_bytes(const char *s, const char *def_unit) { return parse_units (s, bytes_units, def_unit); } ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL -unparse_bytes(ssize_t t, char *s, size_t len) +unparse_bytes(int64_t t, char *s, size_t len) { return unparse_units (t, bytes_units, s, len); } ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL -unparse_bytes_short (ssize_t t, char *s, size_t len) +unparse_bytes_short (int64_t t, char *s, size_t len) { return unparse_units_approx (t, bytes_short_units, s, len); } diff --git a/lib/roken/parse_bytes.h b/lib/roken/parse_bytes.h index b7eea2b0d..5148fc603 100644 --- a/lib/roken/parse_bytes.h +++ b/lib/roken/parse_bytes.h @@ -38,13 +38,13 @@ #include -ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL +ROKEN_LIB_FUNCTION int64_t ROKEN_LIB_CALL parse_bytes(const char *s, const char *def_unit); ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL -unparse_bytes(ssize_t t, char *s, size_t len); +unparse_bytes(int64_t t, char *s, size_t len); ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL -unparse_bytes_short(ssize_t t, char *s, size_t len); +unparse_bytes_short(int64_t t, char *s, size_t len); #endif /* __PARSE_BYTES_H__ */