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 66a6d9940673bfa94fcfb980e5510f9e6beea316
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Sun Mar 22 01:14:24 2026 -0400

    Fix -Wextra warnings
    
    Compiling with gcc 14 and `-Wextra` flags showed a few instances of invalid
    sign compare (comparing int to unsigned long int for ex). So update some
    variables to match the types.
    
    To fix sign compare warnings, the return type of `utf8_validate()` changed 
from
    an `int` to `size_t`, and the error result is indicated by `0` and not 
`-1`, as
    before. Previously it returned -1, or 1,2,3,4 values only. Since 0 wasn't 
used
    and it's not a valid return, use it as the new error result.
---
 c_src/decoder.c | 10 +++++-----
 c_src/encoder.c |  8 ++++----
 c_src/jiffy.c   | 11 +++++++----
 c_src/jiffy.h   |  2 +-
 c_src/utf8.c    | 26 +++++++++++++-------------
 5 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/c_src/decoder.c b/c_src/decoder.c
index 27d101a..6a1a7fd 100644
--- a/c_src/decoder.c
+++ b/c_src/decoder.c
@@ -58,8 +58,8 @@ typedef struct {
     ERL_NIF_TERM    null_term;
 
     unsigned char*  p;
-    int             i;
-    int             len;
+    size_t          i;
+    size_t          len;
 
     char*           st_data;
     int             st_size;
@@ -193,8 +193,8 @@ dec_string(Decoder* d, ERL_NIF_TERM* value)
     int has_escape = 0;
     int num_escapes = 0;
     int st;
-    int ulen;
-    int ui;
+    size_t ulen;
+    size_t ui;
     int hi;
     int lo;
     char* chrbuf;
@@ -278,7 +278,7 @@ dec_string(Decoder* d, ERL_NIF_TERM* value)
             d->i++;
         } else {
             ulen = utf8_validate(&(d->p[d->i]), d->len - d->i);
-            if(ulen < 0) {
+            if(ulen == 0) {
                 return 0;
             }
             d->i += ulen;
diff --git a/c_src/encoder.c b/c_src/encoder.c
index 03c5500..4a68c74 100644
--- a/c_src/encoder.c
+++ b/c_src/encoder.c
@@ -264,7 +264,7 @@ enc_atom(Encoder* e, ERL_NIF_TERM val)
     unsigned char data[512];
 
     size_t size;
-    int i;
+    size_t i;
 
     if(!enif_get_atom(e->env, val, (char*)data, 512, ERL_NIF_LATIN1)) {
         return 0;
@@ -322,9 +322,9 @@ enc_string(Encoder* e, ERL_NIF_TERM val)
     unsigned char* data;
     size_t size;
     int esc_len;
-    int ulen;
+    size_t ulen;
     int uval;
-    int i;
+    size_t i;
 
     if(!enif_inspect_binary(e->env, val, &bin)) {
         return 0;
@@ -353,7 +353,7 @@ enc_string(Encoder* e, ERL_NIF_TERM val)
         } else if(data[i] >= 0x80) {
             ulen = utf8_validate(&(data[i]), size - i);
 
-            if (ulen < 0) {
+            if (ulen == 0) {
                 return 0;
             } else if (e->uescape) {
                 uval = utf8_to_unicode(&(data[i]), size-i);
diff --git a/c_src/jiffy.c b/c_src/jiffy.c
index dfca7c7..790cf65 100644
--- a/c_src/jiffy.c
+++ b/c_src/jiffy.c
@@ -82,12 +82,15 @@ unload(ErlNifEnv* env, void* priv)
     return;
 }
 
+// {name, arity, fptr, dirty_flag}
+// dirty flag: 0 (default) | ERL_NIF_DIRTY_JOB_{IO|CPU}_BOUND
+//
 static ErlNifFunc funcs[] =
 {
-    {"nif_decode_init", 2, decode_init},
-    {"nif_decode_iter", 5, decode_iter},
-    {"nif_encode_init", 2, encode_init},
-    {"nif_encode_iter", 3, encode_iter}
+    {"nif_decode_init", 2, decode_init, 0},
+    {"nif_decode_iter", 5, decode_iter, 0},
+    {"nif_encode_init", 2, encode_init, 0},
+    {"nif_encode_iter", 3, encode_iter, 0}
 };
 
 ERL_NIF_INIT(jiffy, funcs, &load, &reload, &upgrade, &unload);
diff --git a/c_src/jiffy.h b/c_src/jiffy.h
index db42611..351749f 100644
--- a/c_src/jiffy.h
+++ b/c_src/jiffy.h
@@ -74,7 +74,7 @@ int int_from_hex(const unsigned char* p);
 int int_to_hex(int val, unsigned char* p);
 int utf8_len(int c);
 int utf8_esc_len(int c);
-int utf8_validate(unsigned char* data, size_t size);
+size_t utf8_validate(unsigned char* data, size_t size);
 int utf8_to_unicode(unsigned char* buf, size_t size);
 int unicode_to_utf8(int c, unsigned char* buf);
 int unicode_from_pair(int hi, int lo);
diff --git a/c_src/utf8.c b/c_src/utf8.c
index 1176857..6f8d3bd 100644
--- a/c_src/utf8.c
+++ b/c_src/utf8.c
@@ -111,11 +111,12 @@ utf8_esc_len(int c)
     }
 }
 
-int
+size_t
 utf8_validate(unsigned char* data, size_t size)
 {
-    int ulen = -1;
+    size_t ulen = 0;
     int ui;
+    size_t i;
 
     if((data[0] & 0x80) == 0x00) {
         ulen = 1;
@@ -126,13 +127,13 @@ utf8_validate(unsigned char* data, size_t size)
     } else if((data[0] & 0xF8) == 0xF0) {
         ulen = 4;
     }
-    if(ulen < 0 || ulen > size) {
-        return -1;
+    if(ulen == 0 || ulen > size) {
+        return 0;
     }
 
     // Check each continuation byte.
-    for(ui = 1; ui < ulen; ui++) {
-        if((data[ui] & 0xC0) != 0x80) return -1;
+    for(i = 1; i < ulen; i++) {
+        if((data[i] & 0xC0) != 0x80) return 0;
     }
 
     // Wikipedia says I have to check that a UTF-8 encoding
@@ -149,13 +150,13 @@ utf8_validate(unsigned char* data, size_t size)
     // ulen == 1 passes by definition
     if(ulen == 2) {
         if((data[0] & 0x1E) == 0)
-            return -1;
+            return 0;
     } else if(ulen == 3) {
         if((data[0] & 0x0F) + (data[1] & 0x20) == 0)
-            return -1;
+            return 0;
     } else if(ulen == 4) {
         if((data[0] & 0x07) + (data[1] & 0x30) == 0)
-            return -1;
+            return 0;
     }
 
     // Lastly we need to check some miscellaneous ranges for
@@ -163,11 +164,11 @@ utf8_validate(unsigned char* data, size_t size)
     if(ulen >= 3) {
         ui = utf8_to_unicode(data, ulen);
         if(ui < 0) {
-            return -1;
+            return 0;
         } else if(ui >= 0xD800 && ui <= 0xDFFF) {
-            return -1;
+            return 0;
         } else if(ui > 0x10FFFF) {
-            return -1;
+            return 0;
         }
     }
 
@@ -269,4 +270,3 @@ unicode_uescape(int val, unsigned char* p)
     }
     return -1;
 }
-

Reply via email to