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

nickva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb-jiffy.git

commit c3dce8afc2708e288caf2ed22a8387030fc136fe
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Mon Apr 13 12:21:48 2026 -0400

    Skip setting buffer to 0 for strtod and strtol
    
    Just terminate with 0 after copy
---
 c_src/decoder.c                | 10 ++++---
 test/jiffy_03_number_tests.erl | 63 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/c_src/decoder.c b/c_src/decoder.c
index d934100..779f947 100644
--- a/c_src/decoder.c
+++ b/c_src/decoder.c
@@ -635,9 +635,11 @@ parse:
 
     errno = 0;
 
-    if(d->i - start < NUM_BUF_LEN) {
-        memset(nbuf, 0, NUM_BUF_LEN);
-        memcpy(nbuf, &p[start], d->i - start);
+    size_t num_len = d->i - start;
+
+    if(num_len < NUM_BUF_LEN) {
+        memcpy(nbuf, &p[start], num_len);
+        nbuf[num_len] = '\0';
 
         if(has_frac || has_exp) {
             dval = strtod(nbuf, NULL);
@@ -663,7 +665,7 @@ parse:
     }
 
     d->is_partial = 1;
-    *value = enif_make_sub_binary(d->env, d->arg, start, d->i - start);
+    *value = enif_make_sub_binary(d->env, d->arg, start, num_len);
     *value = enif_make_tuple2(d->env, num_type, *value);
     return 1;
 
diff --git a/test/jiffy_03_number_tests.erl b/test/jiffy_03_number_tests.erl
index 2a8fdbb..3c8fb8b 100644
--- a/test/jiffy_03_number_tests.erl
+++ b/test/jiffy_03_number_tests.erl
@@ -73,6 +73,69 @@ cases(ok) ->
             1.0E-36,
             <<"1.0e-36">>
         },
+        % There is an internal num_buffer used for loading values in before
+        % passing them to strtod and strtol so we're testing around that 32
+        % size limit
+        % 30 ones
+        {
+            <<"111111111111111111111111111111">>,
+            111111111111111111111111111111,
+            <<"111111111111111111111111111111">>
+        },
+        % 31 ones
+        {
+            <<"1111111111111111111111111111111">>,
+            1111111111111111111111111111111,
+            <<"1111111111111111111111111111111">>
+        },
+        % 32 ones
+        {
+            <<"11111111111111111111111111111111">>,
+            11111111111111111111111111111111,
+            <<"11111111111111111111111111111111">>
+        },
+        % 33 ones
+        {
+            <<"111111111111111111111111111111111">>,
+            111111111111111111111111111111111,
+            <<"111111111111111111111111111111111">>
+        },
+        % 34 ones
+        {
+            <<"1111111111111111111111111111111111">>,
+            1111111111111111111111111111111111,
+            <<"1111111111111111111111111111111111">>
+        },
+        % 30 frac digits
+        {
+            <<"1.00000000000000000000000000E10">>,
+            1.0e10,
+            <<"1.0e10">>
+        },
+        % 31 frac digits
+        {
+            <<"1.00000000000000000000000000E10">>,
+            1.0e10,
+            <<"1.0e10">>
+        },
+        % 32 frac digits
+        {
+            <<"1.000000000000000000000000000E10">>,
+            1.0e10,
+            <<"1.0e10">>
+        },
+        % 33 frac digits
+        {
+            <<"1.0000000000000000000000000000E10">>,
+            1.0e10,
+            <<"1.0e10">>
+        },
+        % 34 frac digits
+        {
+            <<"1.00000000000000000000000000000E10">>,
+            1.0e10,
+            <<"1.0e10">>
+        },
         {<<"0.75">>, 0.75},
         {<<"2.0123456789">>, 2.0123456789, <<"2.0123456789">>},
         {<<"2.4234324E24">>, 2.4234324E24, <<"2.4234324e24">>},

Reply via email to