[gjs: 2/4] byteArray: Add 0-length check



commit aa338ae26197827002cd7e1201fc3790630cff4e
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Nov 12 19:32:05 2018 -0500

    byteArray: Add 0-length check
    
    We should not access the data pointer if the length is 0. This was also
    causing a crash.
    
    Closes #219.

 gjs/byteArray.cpp                   | 5 +++++
 installed-tests/js/testByteArray.js | 6 ++++++
 2 files changed, 11 insertions(+)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 825e1bf8..cf3ae255 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -73,6 +73,11 @@ static bool to_string_impl(JSContext* context, JS::HandleObject byte_array,
     bool is_shared_memory;
     js::GetUint8ArrayLengthAndData(byte_array, &len, &is_shared_memory, &data);
 
+    if (len == 0) {
+        rval.setString(JS_GetEmptyString(context));
+        return true;
+    }
+
     if (encoding_is_utf8) {
         /* optimization, avoids iconv overhead and runs
          * libmozjs hardwired utf8-to-utf16
diff --git a/installed-tests/js/testByteArray.js b/installed-tests/js/testByteArray.js
index 4bcebf8c..6432da85 100644
--- a/installed-tests/js/testByteArray.js
+++ b/installed-tests/js/testByteArray.js
@@ -58,6 +58,12 @@ describe('Byte array', function () {
         expect(s).toEqual('ab');
     });
 
+    it('deals gracefully with a 0-length array', function () {
+        const a = new Uint8Array(0);
+        expect(ByteArray.toString(a)).toEqual('');
+        expect(ByteArray.toGBytes(a).get_size()).toEqual(0);
+    });
+
     it('deals gracefully with a non Uint8Array', function () {
         const a = [97, 98, 99, 100, 0];
         expect(() => ByteArray.toString(a)).toThrow();


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