[gjs/mozjs91: 67/87] Typed array functions expect size_t instead of uint32_t




commit 9ddf6c113629663f1b5103ddde89f92e7c3a1196
Author: Evan Welsh <contact evanwelsh com>
Date:   Fri Oct 8 15:17:51 2021 -0700

    Typed array functions expect size_t instead of uint32_t
    
    See https://bugzilla.mozilla.org/show_bug.cgi?id=1674777
    
    (Rebased on current text-encoding.cpp by Philip; changed other functions
    to expect size_t accordingly)

 gjs/byteArray.cpp     |  2 +-
 gjs/text-encoding.cpp | 20 ++++++++++++--------
 2 files changed, 13 insertions(+), 9 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 40a9d243..9853ba6d 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -190,7 +190,7 @@ JSObject* gjs_byte_array_from_byte_array(JSContext* cx, GByteArray* array) {
 
 GBytes* gjs_byte_array_get_bytes(JSObject* obj) {
     bool is_shared_memory;
-    uint32_t len;
+    size_t len;
     uint8_t* data;
 
     js::GetUint8ArrayLengthAndData(obj, &len, &is_shared_memory, &data);
diff --git a/gjs/text-encoding.cpp b/gjs/text-encoding.cpp
index f6b1bbbf..dbe61b41 100644
--- a/gjs/text-encoding.cpp
+++ b/gjs/text-encoding.cpp
@@ -191,7 +191,7 @@ static JSString* gjs_lossy_decode_from_uint8array_slow(
 GJS_JSAPI_RETURN_CONVENTION
 static JSString* gjs_decode_from_uint8array_slow(JSContext* cx,
                                                  const uint8_t* input,
-                                                 uint32_t input_len,
+                                                 size_t input_len,
                                                  const char* encoding,
                                                  bool fatal) {
     // If the decoding is not fatal we use the lossy decoder.
@@ -199,6 +199,12 @@ static JSString* gjs_decode_from_uint8array_slow(JSContext* cx,
         return gjs_lossy_decode_from_uint8array_slow(cx, input, input_len,
                                                      encoding);
 
+    // g_convert only handles up to SSIZE_MAX bytes, but we may have SIZE_MAX
+    if (G_UNLIKELY(input_len > SSIZE_MAX)) {
+        gjs_throw(cx, "Array too big to decode: %zu bytes", input_len);
+        return nullptr;
+    }
+
     size_t bytes_written, bytes_read;
     GError* error = nullptr;
 
@@ -236,14 +242,13 @@ static JSString* gjs_decode_from_uint8array_slow(JSContext* cx,
 }
 
 // Finds the length of a given data array, stopping at the first 0 byte.
-template <class T, class L>
-[[nodiscard]] static L zero_terminated_length(const T* data, L len) {
+template <class T>
+[[nodiscard]] static size_t zero_terminated_length(const T* data, size_t len) {
     if (!data || len == 0)
         return 0;
 
     const T* start = data;
-    auto* found = static_cast<const T*>(
-        std::memchr(start, '\0', static_cast<size_t>(len)));
+    auto* found = static_cast<const T*>(std::memchr(start, '\0', len));
 
     // If a null byte was not found, return the passed length.
     if (!found)
@@ -265,8 +270,7 @@ JSString* gjs_decode_from_uint8array(JSContext* cx, JS::HandleObject byte_array,
     }
 
     uint8_t* data;
-    // len should be size_t but SpiderMonkey defines it differently in mozjs78
-    uint32_t len;
+    size_t len;
     bool is_shared_memory;
     js::GetUint8ArrayLengthAndData(byte_array, &len, &is_shared_memory, &data);
 
@@ -322,7 +326,7 @@ JSString* gjs_decode_from_uint8array(JSContext* cx, JS::HandleObject byte_array,
     }
 
     uint8_t* current_data;
-    uint32_t current_len;
+    size_t current_len;
     bool ignore_val;
 
     // If a garbage collection occurs between when we call


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]