Skip to content

Commit

Permalink
RedfishPkg/RedfishCrtLib: handle floating point number in JSON
Browse files Browse the repository at this point in the history
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 <[email protected]>
Cc: Abner Chang <[email protected]>
Cc: Igor Kulchytskyy <[email protected]>
Cc: Nick Ramirez <[email protected]>
Reviewed-by: Abner Chang <[email protected]>
  • Loading branch information
nicklela authored and mergify[bot] committed Jan 15, 2024
1 parent 6a01fb2 commit d65b183
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
5 changes: 3 additions & 2 deletions RedfishPkg/Library/JsonLib/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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:
Expand Down
32 changes: 27 additions & 5 deletions RedfishPkg/PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -540,17 +541,38 @@ 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 (
const char *__restrict nptr,
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[] = {
Expand Down

0 comments on commit d65b183

Please sign in to comment.