From f3bc3cd97ebdc13dbdb5d5cd5554a7060a04a8bc Mon Sep 17 00:00:00 2001 From: Sourav Moitra Date: Wed, 1 Jan 2025 00:02:56 +0530 Subject: [PATCH] Jansson has no support for uint64 and hence we need to convert double to uint64 Since there is no clean way to do this Signed-off-by: Sourav Moitra --- src/ocispec/json_common.c | 22 ++++++++++++++++++---- src/ocispec/json_common.h | 2 ++ tests/data/config.json | 7 ++++++- tests/test-1.c | 2 +- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/ocispec/json_common.c b/src/ocispec/json_common.c index 83ed7a41..c9873989 100644 --- a/src/ocispec/json_common.c +++ b/src/ocispec/json_common.c @@ -141,10 +141,24 @@ json_double_to_uint (double d, unsigned int *converted) int json_double_to_uint64 (double d, uint64_t *converted) { - unsigned long long int ull; - ull = (unsigned long long int) d; - *converted = (uint64_t) ull; - return 0; + // Check for special cases: NaN, infinity, and values outside the representable range of uint64_t + if (isnan(d) || isinf(d) || d < 0 || d > UINT64_MAX) { + // Handle the error case as needed (e.g., return a specific error code) + return -1; // Or another appropriate value + } + + // Safely convert double to uint64_t by checking for potential overflows + if (d >= 4294967296.0) { // Check if value is greater than or equal to 2^32 + // TODO: Better solution for converting double to uint64 + *converted = 0; + char string[20]; + sprintf(string, "%0.f", d); + *converted = strtoull(string, NULL, 10); + } else { + // Handle smaller values (less than 2^32) + *converted = (uint64_t) d; + } + return 0; } /* diff --git a/src/ocispec/json_common.h b/src/ocispec/json_common.h index 0d4179d2..6c16e917 100644 --- a/src/ocispec/json_common.h +++ b/src/ocispec/json_common.h @@ -6,7 +6,9 @@ #include #include #include +#include #include +#include #ifdef __cplusplus extern "C" { diff --git a/tests/data/config.json b/tests/data/config.json index 33be19b9..3be40dda 100644 --- a/tests/data/config.json +++ b/tests/data/config.json @@ -51,10 +51,15 @@ }, "rlimits": [ { - "type": "RLIMIT_NOFILE", + "type": "RLIMIT_CORE", "hard": 18446744073709551615, "soft": 18446744073709551615 }, + { + "type": "RLIMIT_NOFILE", + "hard": 1024, + "soft": 1024 + }, { "type": "RLIMIT_NPROC", "hard": 1048576, diff --git a/tests/test-1.c b/tests/test-1.c index cd85ba6d..e7af632c 100644 --- a/tests/test-1.c +++ b/tests/test-1.c @@ -59,7 +59,7 @@ main () exit (6); if (strcmp (container->process->args[0], "ARGS1") && strcmp (container->process->args[0], container_gen->process->args[0])) exit (61); - if (container->process->rlimits[0]->hard == hard_limit && container_gen->process->rlimits[0]->hard == container->process->rlimits[0]->hard) + if (container->process->rlimits[0]->hard != hard_limit) exit (63); if (strcmp (container->mounts[0]->destination, "/proc") && strcmp (container->mounts[0]->destination, container_gen->mounts[0]->destination)) exit (62);