[gjs] Avoid type-punning errors under optimization.



commit e2b90de6d281ff4532d82227720f04dba59f8007
Author: C. Scott Ananian <cscott litl com>
Date:   Tue Apr 28 17:13:49 2009 -0400

    Avoid type-punning errors under optimization.
    
    This fixes glibc's "dereferencing type-punned pointer will break
    strict-aliasing rules" warning, because we're not passing a "type-punned"
    pointer into a union type into a function, but instead passing in a pointer
    to a "real" char * variable, and then assigning it to the union type as a
    separate step.
    
    In the second case, we pass in a 'real' void *, and cast to 'char **' in
    a separate step to avoid the warning.
---
 gi/arg.c   |   12 ++++++++++--
 gi/value.c |    5 ++++-
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/gi/arg.c b/gi/arg.c
index 845a704..adb70b1 100644
--- a/gi/arg.c
+++ b/gi/arg.c
@@ -400,7 +400,11 @@ gjs_value_to_g_argument(JSContext      *context,
         if (JSVAL_IS_NULL(value)) {
             arg->v_pointer = NULL;
         } else if (JSVAL_IS_STRING(value)) {
-            if (!gjs_string_to_filename(context, value, (char **)&arg->v_pointer))
+            char *filename_str;
+            if (gjs_string_to_filename(context, value, &filename_str))
+                // doing this as a separate step to avoid type-punning
+                arg->v_pointer = filename_str;
+            else
                 wrong = TRUE;
         } else {
             wrong = TRUE;
@@ -412,7 +416,11 @@ gjs_value_to_g_argument(JSContext      *context,
         if (JSVAL_IS_NULL(value)) {
             arg->v_pointer = NULL;
         } else if (JSVAL_IS_STRING(value)) {
-            if (!gjs_string_to_utf8(context, value, (char **)&arg->v_pointer))
+            char *utf8_str;
+            if (gjs_string_to_utf8(context, value, &utf8_str))
+                // doing this as a separate step to avoid type-punning
+                arg->v_pointer = utf8_str;
+            else
                 wrong = TRUE;
         } else {
             wrong = TRUE;
diff --git a/gi/value.c b/gi/value.c
index 887f8c0..ab80ac9 100644
--- a/gi/value.c
+++ b/gi/value.c
@@ -352,12 +352,15 @@ gjs_value_to_g_value_internal(JSContext    *context,
                           gjs_get_type_name(value));
                 return JS_FALSE;
             } else {
+                void *result;
                 char **strv;
 
                 if (!gjs_array_to_strv (context,
                                         value,
-                                        length, (void**)&strv))
+                                        length, &result))
                     return JS_FALSE;
+                /* cast to strv in a separate step to avoid type-punning */
+                strv = result;
                 g_value_take_boxed (gvalue, strv);
             }
         } else {



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