[gjs/ewlsh/fix-zero-terminated-string-arrays] gi: Don't zero terminate strings passed for bytes




commit d4b7af85b73cfa325015d4d50395d779d801d500
Author: Evan Welsh <contact evanwelsh com>
Date:   Tue Aug 24 23:58:49 2021 -0700

    gi: Don't zero terminate strings passed for bytes

 gi/arg.cpp                     |  9 +++++----
 installed-tests/js/testGLib.js | 10 ++++++++++
 2 files changed, 15 insertions(+), 4 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 63e8e03c..5804cdd5 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -49,6 +49,7 @@
 #include "gjs/enum-utils.h"
 #include "gjs/jsapi-util.h"
 #include "util/log.h"
+#include "util/misc.h"
 
 GJS_JSAPI_RETURN_CONVENTION static bool gjs_g_arg_release_internal(
     JSContext* cx, GITransfer transfer, GITypeInfo* type_info,
@@ -693,11 +694,11 @@ gjs_string_to_intarray(JSContext       *context,
     switch (element_type) {
         case GI_TYPE_TAG_INT8:
         case GI_TYPE_TAG_UINT8: {
-            JS::UniqueChars result(JS_EncodeStringToUTF8(context, str));
-            if (!result)
+            JS::UniqueChars result;
+            if (!gjs_string_to_utf8_n(context, str, &result, length))
                 return false;
-            *length = strlen(result.get());
-            *arr_p = g_strdup(result.get());
+
+            *arr_p = _gjs_memdup2(result.get(), *length);
             return true;
         }
 
diff --git a/installed-tests/js/testGLib.js b/installed-tests/js/testGLib.js
index bd114292..40a8e98c 100644
--- a/installed-tests/js/testGLib.js
+++ b/installed-tests/js/testGLib.js
@@ -241,4 +241,14 @@ describe('GLib string function overrides', function () {
         expect(GLib.strcanon('1a2b3c4', 'abc', '?')).toEqual('?a?b?c?');
         assertWarnings('strcanon');
     });
+
+    it('GLib.base64_encode', function () {
+        const ascii = 'hello\0world';
+        const base64 = 'aGVsbG8Ad29ybGQ=';
+
+        expect(GLib.base64_encode(ascii)).toBe(base64);
+
+        const encoded = new TextEncoder().encode(ascii);
+        expect(GLib.base64_encode(encoded)).toBe(base64);       
+    });
 });


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