[gjs/wip/ptomato/warnings: 8/14] byte array: Don't reinterpret char * as jschar *



commit 48ededab57ef53c0f502d3a2d17ec6eb75cd588b
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Oct 2 15:18:15 2016 -0700

    byte array: Don't reinterpret char * as jschar *
    
    Doing so breaks alignment requirements and may have been the cause of an
    ARM-only crash we saw on ByteArray a couple years ago.
    
    Doing an extra copy seems wasteful, but is the recommended way according
    to an article on the subject [1], and compilers are sometimes able to
    optimize out the copy these days...
    
    [1] http://blog.regehr.org/archives/959

 gjs/byteArray.cpp |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 8d7e71e..8627cc5 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -460,6 +460,7 @@ to_string_func(JSContext *context,
         GError *error;
         JSString *s;
         char *u16_str;
+        jschar *u16_out;
 
         error = NULL;
         u16_str = g_convert(data,
@@ -481,15 +482,16 @@ to_string_func(JSContext *context,
          */
         g_assert((bytes_written % 2) == 0);
 
-        s = JS_NewUCStringCopyN(context,
-                                (jschar*) u16_str,
-                                bytes_written / 2);
+        u16_out = g_new(jschar, bytes_written / 2);
+        memcpy(u16_out, u16_str, bytes_written);
+        s = JS_NewUCStringCopyN(context, u16_out, bytes_written / 2);
         if (s != NULL) {
             ok = true;
             argv.rval().setString(s);
         }
 
         g_free(u16_str);
+        g_free(u16_out);
         return ok;
     }
 }


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