* [edk2-devel] [PATCH v2] RedfishPkg/RedfishCrtLib: handle floating point number in JSON
@ 2024-01-14 14:13 Nickle Wang via groups.io
2024-01-15 5:17 ` Chang, Abner via groups.io
0 siblings, 1 reply; 2+ messages in thread
From: Nickle Wang via groups.io @ 2024-01-14 14:13 UTC (permalink / raw)
To: devel; +Cc: Abner Chang, Igor Kulchytskyy, Nick Ramirez
When the value type is defined as number in Redfish schema, floating
point number is allowed. RedfishCrtLib raises assert without handling
this case now. Follow the way in EDK2 to call AsciiStrDecimalToUintnS
and handle the floating point number. Only the integer value is
returned.
Signed-off-by: Nickle Wang <nicklew@nvidia.com>
Cc: Abner Chang <abner.chang@amd.com>
Cc: Igor Kulchytskyy <igork@ami.com>
Cc: Nick Ramirez <nramirez@nvidia.com>
---
RedfishPkg/Library/JsonLib/load.c | 5 +--
.../RedfishCrtLib/RedfishCrtLib.c | 32 ++++++++++++++++---
2 files changed, 30 insertions(+), 7 deletions(-)
diff --git a/RedfishPkg/Library/JsonLib/load.c b/RedfishPkg/Library/JsonLib/load.c
index 958c3ea8c5..1a3b4a6e6a 100644
--- a/RedfishPkg/Library/JsonLib/load.c
+++ b/RedfishPkg/Library/JsonLib/load.c
@@ -5,6 +5,7 @@
* it under the terms of the MIT license. See LICENSE for details.
(C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+ Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent AND MIT
*/
@@ -717,8 +718,8 @@ lex_scan_number (
goto out;
}
- lex->token = TOKEN_REAL;
- lex->value.real = doubleval;
+ lex->token = TOKEN_INTEGER;
+ lex->value.integer = doubleval;
return 0;
out:
diff --git a/RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c b/RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c
index 57a997f351..902dd0b3a0 100644
--- a/RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c
+++ b/RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c
@@ -4,6 +4,7 @@
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
(C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+ Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -519,7 +520,7 @@ strtoull (
}
/**
- edk2 Jansson port does not support doubles, simply return 0.
+ edk2 Jansson port does not support doubles, simply return integer part.
These conversion functions convert the initial portion of the string
pointed to by nptr to double, float, and long double representation,
@@ -540,7 +541,7 @@ strtoull (
the return value), and ERANGE is stored in errno. If the correct value
would cause underflow, zero is returned and ERANGE is stored in errno.
- @return Return 0.
+ @return Integer part of decimal number.
**/
double
strtod (
@@ -548,9 +549,30 @@ strtod (
char **__restrict endptr
)
{
- DEBUG ((DEBUG_ERROR, "We don't supprot double type on edk2 yet!"));
- ASSERT (FALSE);
- return (double)0;
+ UINTN Data;
+ UINTN StrLen;
+
+ Data = 0;
+ StrLen = 0;
+
+ if (nptr == NULL) {
+ return (double)0;
+ }
+
+ AsciiStrDecimalToUintnS (nptr, NULL, &Data);
+ DEBUG ((DEBUG_WARN, "%a: \"%a\" We don't support double type on edk2 yet. Only integer part is returned: %d\n", __func__, nptr, Data));
+
+ //
+ // Force endptr to the last position of nptr because caller may
+ // check endptr and raise assertion. We don't support floating
+ // number in edk2 so this prevents unecessary assertion from happening.
+ //
+ if (endptr != NULL) {
+ StrLen = AsciiStrLen (nptr);
+ *endptr = (char *__restrict)nptr + StrLen;
+ }
+
+ return (double)Data;
}
static UINT8 BitMask[] = {
--
2.34.1
-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113780): https://edk2.groups.io/g/devel/message/113780
Mute This Topic: https://groups.io/mt/103718898/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [edk2-devel] [PATCH v2] RedfishPkg/RedfishCrtLib: handle floating point number in JSON
2024-01-14 14:13 [edk2-devel] [PATCH v2] RedfishPkg/RedfishCrtLib: handle floating point number in JSON Nickle Wang via groups.io
@ 2024-01-15 5:17 ` Chang, Abner via groups.io
0 siblings, 0 replies; 2+ messages in thread
From: Chang, Abner via groups.io @ 2024-01-15 5:17 UTC (permalink / raw)
To: Nickle Wang, devel@edk2.groups.io; +Cc: Igor Kulchytskyy, Nick Ramirez
[AMD Official Use Only - General]
Reviewed-by: Abner Chang <abner.chang@amd.com>
> -----Original Message-----
> From: Nickle Wang <nicklew@nvidia.com>
> Sent: Sunday, January 14, 2024 10:14 PM
> To: devel@edk2.groups.io
> Cc: Chang, Abner <Abner.Chang@amd.com>; Igor Kulchytskyy
> <igork@ami.com>; Nick Ramirez <nramirez@nvidia.com>
> Subject: [PATCH v2] RedfishPkg/RedfishCrtLib: handle floating point number
> in JSON
>
> Caution: This message originated from an External Source. Use proper caution
> when opening attachments, clicking links, or responding.
>
>
> When the value type is defined as number in Redfish schema, floating
> point number is allowed. RedfishCrtLib raises assert without handling
> this case now. Follow the way in EDK2 to call AsciiStrDecimalToUintnS
> and handle the floating point number. Only the integer value is
> returned.
>
> Signed-off-by: Nickle Wang <nicklew@nvidia.com>
> Cc: Abner Chang <abner.chang@amd.com>
> Cc: Igor Kulchytskyy <igork@ami.com>
> Cc: Nick Ramirez <nramirez@nvidia.com>
> ---
> RedfishPkg/Library/JsonLib/load.c | 5 +--
> .../RedfishCrtLib/RedfishCrtLib.c | 32 ++++++++++++++++---
> 2 files changed, 30 insertions(+), 7 deletions(-)
>
> diff --git a/RedfishPkg/Library/JsonLib/load.c
> b/RedfishPkg/Library/JsonLib/load.c
> index 958c3ea8c5..1a3b4a6e6a 100644
> --- a/RedfishPkg/Library/JsonLib/load.c
> +++ b/RedfishPkg/Library/JsonLib/load.c
> @@ -5,6 +5,7 @@
> * it under the terms of the MIT license. See LICENSE for details.
>
> (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
> + Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
>
> SPDX-License-Identifier: BSD-2-Clause-Patent AND MIT
> */
> @@ -717,8 +718,8 @@ lex_scan_number (
> goto out;
> }
>
> - lex->token = TOKEN_REAL;
> - lex->value.real = doubleval;
> + lex->token = TOKEN_INTEGER;
> + lex->value.integer = doubleval;
> return 0;
>
> out:
> diff --git a/RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c
> b/RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c
> index 57a997f351..902dd0b3a0 100644
> --- a/RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c
> +++ b/RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c
> @@ -4,6 +4,7 @@
>
> Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
> + Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights
> reserved.
>
> SPDX-License-Identifier: BSD-2-Clause-Patent
>
> @@ -519,7 +520,7 @@ strtoull (
> }
>
> /**
> - edk2 Jansson port does not support doubles, simply return 0.
> + edk2 Jansson port does not support doubles, simply return integer part.
>
> These conversion functions convert the initial portion of the string
> pointed to by nptr to double, float, and long double representation,
> @@ -540,7 +541,7 @@ strtoull (
> the return value), and ERANGE is stored in errno. If the correct value
> would cause underflow, zero is returned and ERANGE is stored in errno.
>
> - @return Return 0.
> + @return Integer part of decimal number.
> **/
> double
> strtod (
> @@ -548,9 +549,30 @@ strtod (
> char **__restrict endptr
> )
> {
> - DEBUG ((DEBUG_ERROR, "We don't supprot double type on edk2 yet!"));
> - ASSERT (FALSE);
> - return (double)0;
> + UINTN Data;
> + UINTN StrLen;
> +
> + Data = 0;
> + StrLen = 0;
> +
> + if (nptr == NULL) {
> + return (double)0;
> + }
> +
> + AsciiStrDecimalToUintnS (nptr, NULL, &Data);
> + DEBUG ((DEBUG_WARN, "%a: \"%a\" We don't support double type on
> edk2 yet. Only integer part is returned: %d\n", __func__, nptr, Data));
> +
> + //
> + // Force endptr to the last position of nptr because caller may
> + // check endptr and raise assertion. We don't support floating
> + // number in edk2 so this prevents unecessary assertion from happening.
> + //
> + if (endptr != NULL) {
> + StrLen = AsciiStrLen (nptr);
> + *endptr = (char *__restrict)nptr + StrLen;
> + }
> +
> + return (double)Data;
> }
>
> static UINT8 BitMask[] = {
> --
> 2.34.1
-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113793): https://edk2.groups.io/g/devel/message/113793
Mute This Topic: https://groups.io/mt/103718898/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-01-16 6:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-14 14:13 [edk2-devel] [PATCH v2] RedfishPkg/RedfishCrtLib: handle floating point number in JSON Nickle Wang via groups.io
2024-01-15 5:17 ` Chang, Abner via groups.io
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox