[njs] Avoiding casting NaN value to int64_t in njs.dump().

Dmitry Volyntsev xeioex at nginx.com
Tue Jan 9 17:31:20 UTC 2024


details:   https://hg.nginx.org/njs/rev/c15a6129ade7
branches:  
changeset: 2260:c15a6129ade7
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jan 08 22:20:10 2024 -0800
description:
Avoiding casting NaN value to int64_t in njs.dump().

Found by UndefinedBehaviorSanitizer.

diffstat:

 src/njs_json.c |  10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diffs (41 lines):

diff -r e2c6451435a0 -r c15a6129ade7 src/njs_json.c
--- a/src/njs_json.c	Mon Jan 08 22:19:59 2024 -0800
+++ b/src/njs_json.c	Mon Jan 08 22:20:10 2024 -0800
@@ -1894,7 +1894,7 @@ njs_dump_visited(njs_vm_t *vm, njs_json_
 }
 
 
-njs_inline njs_bool_t
+njs_inline void
 njs_dump_empty(njs_json_stringify_t *stringify, njs_json_state_t *state,
     njs_chb_t *chain, njs_bool_t sep_position)
 {
@@ -1902,7 +1902,7 @@ njs_dump_empty(njs_json_stringify_t *str
     int64_t  diff;
 
     if (!state->array) {
-        return 0;
+        return;
     }
 
     if (sep_position) {
@@ -1919,6 +1919,10 @@ njs_dump_empty(njs_json_stringify_t *str
         }
     }
 
+    if (isnan(prev)) {
+        return;
+    }
+
     if (isnan(key)) {
         key = state->length;
     }
@@ -1947,8 +1951,6 @@ njs_dump_empty(njs_json_stringify_t *str
             njs_json_stringify_indent(stringify, chain, 1);
         }
     }
-
-    return 1;
 }
 
 


More information about the nginx-devel mailing list