You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ha...@apache.org on 2022/08/29 20:54:24 UTC

[incubator-nuttx] 01/01: libc: Port strtod fixes to strtof, strtold and improve comments

This is an automated email from the ASF dual-hosted git repository.

hartmannathan pushed a commit to branch strtod-fixes
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 1d803dd7a81a09097cd0dee66ef396ad55452457
Author: Nathan Hartman <59...@users.noreply.github.com>
AuthorDate: Mon Aug 29 16:53:36 2022 -0400

    libc: Port strtod fixes to strtof, strtold and improve comments
    
    * libs/libc/stdlib/lib_strtod.c:
      (strtod): Add a note about limitations of this implementation
       as compared to POSIX in the function's docstring. Also fix a
       typo.
    
    * libs/libc/stdlib/lib_strtof.c:
      (strtof): Port the changes made to strtod in PR-6952 (commit
       c83985c5ce7) and add same note as above to docstring.
    
    * libs/libc/stdlib/lib_strtold.c:
      (strtold): Same changes as strtof.
---
 libs/libc/stdlib/lib_strtod.c  |  6 +++++-
 libs/libc/stdlib/lib_strtof.c  | 15 ++++++++++++++-
 libs/libc/stdlib/lib_strtold.c | 15 ++++++++++++++-
 3 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/libs/libc/stdlib/lib_strtod.c b/libs/libc/stdlib/lib_strtod.c
index 358eb5aec2..fe8e48ff4b 100644
--- a/libs/libc/stdlib/lib_strtod.c
+++ b/libs/libc/stdlib/lib_strtod.c
@@ -79,12 +79,16 @@ static inline int is_real(double x)
  * Public Functions
  ****************************************************************************/
 
-/***************************************************(************************
+/****************************************************************************
  * Name: strtod
  *
  * Description:
  *   Convert a string to a double value
  *
+ *   NOTE: This implementation is limited as compared to POSIX:
+ *   - Hexadecimal input is not supported
+ *   - INF, INFINITY, NAN, and NAN(...) are not supported
+ *
  ****************************************************************************/
 
 double strtod(FAR const char *str, FAR char **endptr)
diff --git a/libs/libc/stdlib/lib_strtof.c b/libs/libc/stdlib/lib_strtof.c
index 246a5183ad..176a5c5333 100644
--- a/libs/libc/stdlib/lib_strtof.c
+++ b/libs/libc/stdlib/lib_strtof.c
@@ -86,12 +86,16 @@ static inline int is_real(float x)
  * Public Functions
  ****************************************************************************/
 
-/***************************************************(************************
+/****************************************************************************
  * Name: strtof
  *
  * Description:
  *   Convert a string to a float value
  *
+ *   NOTE: This implementation is limited as compared to POSIX:
+ *   - Hexadecimal input is not supported
+ *   - INF, INFINITY, NAN, and NAN(...) are not supported
+ *
  ****************************************************************************/
 
 float strtof(FAR const char *str, FAR char **endptr)
@@ -173,6 +177,7 @@ float strtof(FAR const char *str, FAR char **endptr)
     {
       set_errno(ERANGE);
       number = 0.0F;
+      p = (FAR char *)str;
       goto errout;
     }
 
@@ -208,6 +213,14 @@ float strtof(FAR const char *str, FAR char **endptr)
 
       /* Process string of digits */
 
+      if (!isdigit(*p))
+        {
+          set_errno(ERANGE);
+          number = 0.0F;
+          p = (FAR char *)str;
+          goto errout;
+        }
+
       n = 0;
       while (isdigit(*p))
         {
diff --git a/libs/libc/stdlib/lib_strtold.c b/libs/libc/stdlib/lib_strtold.c
index 322685f699..cd569561d2 100644
--- a/libs/libc/stdlib/lib_strtold.c
+++ b/libs/libc/stdlib/lib_strtold.c
@@ -79,12 +79,16 @@ static inline int is_real(long double x)
  * Public Functions
  ****************************************************************************/
 
-/***************************************************(************************
+/****************************************************************************
  * Name: strtold
  *
  * Description:
  *   Convert a string to a long double value
  *
+ *   NOTE: This implementation is limited as compared to POSIX:
+ *   - Hexadecimal input is not supported
+ *   - INF, INFINITY, NAN, and NAN(...) are not supported
+ *
  ****************************************************************************/
 
 long double strtold(FAR const char *str, FAR char **endptr)
@@ -160,6 +164,7 @@ long double strtold(FAR const char *str, FAR char **endptr)
     {
       set_errno(ERANGE);
       number = 0.0L;
+      p = (FAR char *)str;
       goto errout;
     }
 
@@ -195,6 +200,14 @@ long double strtold(FAR const char *str, FAR char **endptr)
 
       /* Process string of digits */
 
+      if (!isdigit(*p))
+        {
+          set_errno(ERANGE);
+          number = 0.0L;
+          p = (FAR char *)str;
+          goto errout;
+        }
+
       n = 0;
       while (isdigit(*p))
         {