[gjs: 1/4] byteArray: Add checks that object is a Uint8Array
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 1/4] byteArray: Add checks that object is a Uint8Array
- Date: Tue, 13 Nov 2018 02:12:19 +0000 (UTC)
commit 8e6ed6ccc8769bf8782b579463ecc7692cca9c77
Author: Philip Chimento <philip chimento gmail com>
Date: Mon Nov 12 17:37:08 2018 -0500
byteArray: Add checks that object is a Uint8Array
This avoids a crash when the wrong object is passed to the functions.
Closes #219.
gjs/byteArray.cpp | 12 ++++++++++++
installed-tests/js/testByteArray.js | 6 ++++++
2 files changed, 18 insertions(+)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 6130eb65..825e1bf8 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -50,6 +50,12 @@ static void bytes_unref_arraybuffer(void* contents, void* user_data) {
GJS_JSAPI_RETURN_CONVENTION
static bool to_string_impl(JSContext* context, JS::HandleObject byte_array,
const char* encoding, JS::MutableHandleValue rval) {
+ if (!JS_IsUint8Array(byte_array)) {
+ gjs_throw(context,
+ "Argument to ByteArray.toString() must be a Uint8Array");
+ return false;
+ }
+
bool encoding_is_utf8;
uint8_t* data;
@@ -161,6 +167,12 @@ to_gbytes_func(JSContext *context,
"byteArray", &byte_array))
return false;
+ if (!JS_IsUint8Array(byte_array)) {
+ gjs_throw(context,
+ "Argument to ByteArray.toGBytes() must be a Uint8Array");
+ return false;
+ }
+
GBytes* bytes = gjs_byte_array_get_bytes(byte_array);
gbytes_info = g_irepository_find_by_gtype(NULL, G_TYPE_BYTES);
ret_bytes_obj = gjs_boxed_from_c_struct(context, (GIStructInfo*)gbytes_info,
diff --git a/installed-tests/js/testByteArray.js b/installed-tests/js/testByteArray.js
index 3c537603..4bcebf8c 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 non Uint8Array', function () {
+ const a = [97, 98, 99, 100, 0];
+ expect(() => ByteArray.toString(a)).toThrow();
+ expect(() => ByteArray.toGBytes(a)).toThrow();
+ });
+
describe('legacy toString() behavior', function () {
beforeEach(function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]