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



commit 74daa949a3b10f3e7b182f669032435335f315e4
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
    
    https://bugzilla.gnome.org/show_bug.cgi?id=773297

 gjs/byteArray.cpp |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index b522ed5..f62521d 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -453,6 +453,7 @@ to_string_func(JSContext *context,
         GError *error;
         JSString *s;
         char *u16_str;
+        jschar *u16_out;
 
         error = NULL;
         u16_str = g_convert(data,
@@ -474,15 +475,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]