[pygobject] More compiler warning fixes for Windows/32bit #191



commit 25a5b066467523547d3b3c1947ae3722c3fc38d5
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Thu Apr 5 16:16:21 2018 +0200

    More compiler warning fixes for Windows/32bit #191

 gi/gimodule.c             | 20 ++++++++------------
 gi/pygi-argument.c        | 25 ++++++++++++++++---------
 gi/pygi-array.c           | 24 ++++++++++++++++--------
 gi/pygi-basictype.c       |  4 ++--
 gi/pygi-closure.c         | 30 +++++++++++++++---------------
 gi/pygi-enum-marshal.c    | 18 +++++++++++++++---
 gi/pygi-marshal-cleanup.c |  4 ++--
 gi/pygi-type.c            | 10 ++++++----
 gi/pygi-util.c            | 14 ++++++++++++++
 gi/pygi-util.h            |  2 ++
 gi/pygi-value.c           | 23 +++++++++++++++--------
 11 files changed, 111 insertions(+), 63 deletions(-)
---
diff --git a/gi/gimodule.c b/gi/gimodule.c
index 957184ff..f42ad2b1 100644
--- a/gi/gimodule.c
+++ b/gi/gimodule.c
@@ -763,12 +763,10 @@ create_signal (GType instance_type, const gchar *signal_name, PyObject *tuple)
     }
     py_n_params = PySequence_Length(py_param_types);
     if (py_n_params < 0)
-       return FALSE;
-    if (py_n_params > G_MAXUINT) {
-       PyErr_SetString(PyExc_TypeError, "too many params");
-       return FALSE;
-    }
-    n_params = (guint)py_n_params;
+        return FALSE;
+
+    if (!pygi_guint_from_pyssize (py_n_params, &n_params))
+        return FALSE;
 
     param_types = g_new(GType, n_params);
     for (i = 0; i < n_params; i++) {
@@ -2121,12 +2119,10 @@ pyg_signal_new(PyObject *self, PyObject *args)
 
     py_n_params = PySequence_Length(py_param_types);
     if (py_n_params < 0)
-       return FALSE;
-    if (py_n_params > G_MAXUINT) {
-       PyErr_SetString(PyExc_TypeError, "too many params");
-       return FALSE;
-    }
-    n_params = (guint)py_n_params;
+        return FALSE;
+
+    if (!pygi_guint_from_pyssize (py_n_params, &n_params))
+        return FALSE;
 
     param_types = g_new(GType, n_params);
     for (i = 0; i < n_params; i++) {
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index ae513f82..28bf4120 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -73,10 +73,22 @@ pygi_argument_to_gssize (GIArgument *arg_in,
           *gssize_out = arg_in->v_uint32;
           return TRUE;
       case GI_TYPE_TAG_INT64:
-          *gssize_out = arg_in->v_int64;
+          if (arg_in->v_int64 > G_MAXSSIZE || arg_in->v_int64 < G_MINSSIZE) {
+              PyErr_Format (PyExc_TypeError,
+                            "Unable to marshal %s to gssize",
+                            g_type_tag_to_string(type_tag));
+              return FALSE;
+          }
+          *gssize_out = (gssize)arg_in->v_int64;
           return TRUE;
       case GI_TYPE_TAG_UINT64:
-          *gssize_out = arg_in->v_uint64;
+          if (arg_in->v_uint64 > G_MAXSSIZE) {
+              PyErr_Format (PyExc_TypeError,
+                            "Unable to marshal %s to gssize",
+                            g_type_tag_to_string(type_tag));
+              return FALSE;
+          }
+          *gssize_out = (gssize)arg_in->v_uint64;
           return TRUE;
       default:
           PyErr_Format (PyExc_TypeError,
@@ -361,16 +373,11 @@ _pygi_argument_from_object (PyObject   *object,
             }
 
             py_length = PySequence_Length (object);
-            if (py_length < 0) {
+            if (py_length < 0)
                 break;
-            }
 
-            if (py_length > G_MAXUINT) {
-                PyErr_SetString (PyExc_ValueError, "array too large");
+            if (!pygi_guint_from_pyssize (py_length, &length))
                 break;
-            }
-
-            length = (guint)py_length;
 
             is_zero_terminated = g_type_info_is_zero_terminated (type_info);
             item_type_info = g_type_info_get_param_type (type_info, 0);
diff --git a/gi/pygi-array.c b/gi/pygi-array.c
index 640a4085..6b061c9a 100644
--- a/gi/pygi-array.c
+++ b/gi/pygi-array.c
@@ -162,10 +162,22 @@ gi_argument_to_gsize (GIArgument *arg_in,
           *gsize_out = arg_in->v_uint32;
           return TRUE;
       case GI_TYPE_TAG_INT64:
-          *gsize_out = arg_in->v_int64;
+          if (arg_in->v_uint64 > G_MAXSIZE) {
+              PyErr_Format (PyExc_TypeError,
+                            "Unable to marshal %s to gsize",
+                            g_type_tag_to_string (type_tag));
+              return FALSE;
+          }
+          *gsize_out = (gsize)arg_in->v_int64;
           return TRUE;
       case GI_TYPE_TAG_UINT64:
-          *gsize_out = arg_in->v_uint64;
+          if (arg_in->v_uint64 > G_MAXSIZE) {
+              PyErr_Format (PyExc_TypeError,
+                            "Unable to marshal %s to gsize",
+                            g_type_tag_to_string (type_tag));
+              return FALSE;
+          }
+          *gsize_out = (gsize)arg_in->v_uint64;
           return TRUE;
       default:
           PyErr_Format (PyExc_TypeError,
@@ -211,15 +223,11 @@ _pygi_marshal_from_py_array (PyGIInvokeState   *state,
     if (py_length < 0)
         return FALSE;
 
-    if (py_length > G_MAXUINT) {
-        PyErr_SetString (PyExc_ValueError, "Sequence too large");
+    if (!pygi_guint_from_pyssize (py_length, &length))
         return FALSE;
-    }
-
-    length = (guint)py_length;
 
     if (array_cache->fixed_size >= 0 &&
-            array_cache->fixed_size != length) {
+            (guint)array_cache->fixed_size != length) {
         PyErr_Format (PyExc_ValueError, "Must contain %zd items, not %u",
                       array_cache->fixed_size, length);
 
diff --git a/gi/pygi-basictype.c b/gi/pygi-basictype.c
index d9e4b821..21dcbe60 100644
--- a/gi/pygi-basictype.c
+++ b/gi/pygi-basictype.c
@@ -520,7 +520,7 @@ pygi_gint_to_py (gint value)
 gboolean
 pygi_guint_from_py (PyObject *object, guint *result)
 {
-    long long_value;
+    unsigned long long_value;
     PyObject *number;
 
     number = base_number_checks (object);
@@ -533,7 +533,7 @@ pygi_guint_from_py (PyObject *object, guint *result)
         return FALSE;
 
     if (long_value > G_MAXUINT) {
-        PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %lu",
+        PyErr_Format (PyExc_OverflowError, "%lu not in range %ld to %lu",
                       long_value, (long)0, (unsigned long)G_MAXUINT);
         return FALSE;
     }
diff --git a/gi/pygi-closure.c b/gi/pygi-closure.c
index 21105fc2..0eef41e7 100644
--- a/gi/pygi-closure.c
+++ b/gi/pygi-closure.c
@@ -52,31 +52,31 @@ _pygi_closure_assign_pyobj_to_retval (gpointer retval,
 
     switch (arg_cache->type_tag) {
         case GI_TYPE_TAG_BOOLEAN:
-           *((ffi_sarg *) retval) = arg->v_boolean;
+           *((gboolean *) retval) = arg->v_boolean;
            break;
         case GI_TYPE_TAG_INT8:
-           *((ffi_sarg *) retval) = arg->v_int8;
+           *((gint8 *) retval) = arg->v_int8;
            break;
         case GI_TYPE_TAG_UINT8:
-           *((ffi_arg *) retval) = arg->v_uint8;
+           *((guint8 *) retval) = arg->v_uint8;
            break;
         case GI_TYPE_TAG_INT16:
-           *((ffi_sarg *) retval) = arg->v_int16;
+           *((gint16 *) retval) = arg->v_int16;
            break;
         case GI_TYPE_TAG_UINT16:
-           *((ffi_arg *) retval) = arg->v_uint16;
+           *((guint16 *) retval) = arg->v_uint16;
            break;
         case GI_TYPE_TAG_INT32:
-           *((ffi_sarg *) retval) = arg->v_int32;
+           *((gint32 *) retval) = arg->v_int32;
            break;
         case GI_TYPE_TAG_UINT32:
-           *((ffi_arg *) retval) = arg->v_uint32;
+           *((guint32 *) retval) = arg->v_uint32;
            break;
         case GI_TYPE_TAG_INT64:
-           *((ffi_sarg *) retval) = arg->v_int64;
+           *((gint64 *) retval) = arg->v_int64;
            break;
         case GI_TYPE_TAG_UINT64:
-           *((ffi_arg *) retval) = arg->v_uint64;
+           *((guint64 *) retval) = arg->v_uint64;
            break;
         case GI_TYPE_TAG_FLOAT:
            *((gfloat *) retval) = arg->v_float;
@@ -85,10 +85,10 @@ _pygi_closure_assign_pyobj_to_retval (gpointer retval,
            *((gdouble *) retval) = arg->v_double;
            break;
         case GI_TYPE_TAG_GTYPE:
-           *((ffi_arg *) retval) = arg->v_size;
+           *((gsize *) retval) = arg->v_size;
            break;
         case GI_TYPE_TAG_UNICHAR:
-            *((ffi_arg *) retval) = arg->v_uint32;
+            *((guint32 *) retval) = arg->v_uint32;
             break;
         case GI_TYPE_TAG_INTERFACE:
             {
@@ -98,20 +98,20 @@ _pygi_closure_assign_pyobj_to_retval (gpointer retval,
 
                 switch (g_base_info_get_type (interface_info)) {
                 case GI_INFO_TYPE_ENUM:
-                    *(ffi_sarg *) retval = arg->v_int;
+                    *(gint *) retval = arg->v_int;
                     break;
                 case GI_INFO_TYPE_FLAGS:
-                    *(ffi_arg *) retval = arg->v_uint;
+                    *(guint *) retval = arg->v_uint;
                     break;
                 default:
-                    *(ffi_arg *) retval = (ffi_arg) arg->v_pointer;
+                    *(gpointer *) retval = arg->v_pointer;
                     break;
                 }
 
                 break;
             }
         default:
-            *(ffi_arg *) retval = (ffi_arg) arg->v_pointer;
+            *(gpointer *) retval = arg->v_pointer;
             break;
       }
 }
diff --git a/gi/pygi-enum-marshal.c b/gi/pygi-enum-marshal.c
index 950b33b7..e98d023a 100644
--- a/gi/pygi-enum-marshal.c
+++ b/gi/pygi-enum-marshal.c
@@ -91,10 +91,22 @@ gi_argument_to_c_long (GIArgument *arg_in,
           *c_long_out = arg_in->v_uint32;
           return TRUE;
       case GI_TYPE_TAG_INT64:
-          *c_long_out = arg_in->v_int64;
+          if (arg_in->v_int64 > G_MAXLONG || arg_in->v_int64 < G_MINLONG) {
+              PyErr_Format (PyExc_TypeError,
+                            "Unable to marshal %s to C long",
+                            g_type_tag_to_string(type_tag));
+              return FALSE;
+          }
+          *c_long_out = (glong)arg_in->v_int64;
           return TRUE;
       case GI_TYPE_TAG_UINT64:
-          *c_long_out = arg_in->v_uint64;
+          if (arg_in->v_uint64 > G_MAXLONG) {
+              PyErr_Format (PyExc_TypeError,
+                            "Unable to marshal %s to C long",
+                            g_type_tag_to_string(type_tag));
+              return FALSE;
+          }
+          *c_long_out = (glong)arg_in->v_uint64;
           return TRUE;
       default:
           PyErr_Format (PyExc_TypeError,
@@ -150,7 +162,7 @@ _pygi_marshal_from_py_interface_enum (PyGIInvokeState   *state,
         for (i = 0; i < g_enum_info_get_n_values (iface_cache->interface_info); i++) {
             GIValueInfo *value_info =
                 g_enum_info_get_value (iface_cache->interface_info, i);
-            glong enum_value = g_value_info_get_value (value_info);
+            gint64 enum_value = g_value_info_get_value (value_info);
             g_base_info_unref ( (GIBaseInfo *)value_info);
             if (c_long == enum_value) {
                 is_found = TRUE;
diff --git a/gi/pygi-marshal-cleanup.c b/gi/pygi-marshal-cleanup.c
index 332b08be..98fbe478 100644
--- a/gi/pygi-marshal-cleanup.c
+++ b/gi/pygi-marshal-cleanup.c
@@ -188,7 +188,7 @@ pygi_marshal_cleanup_args_from_py_parameter_fail (PyGIInvokeState   *state,
 
     state->failed = TRUE;
 
-    for (i = 0; i < _pygi_callable_cache_args_len (cache)  && i <= failed_arg_index; i++) {
+    for (i = 0; i < _pygi_callable_cache_args_len (cache)  && i <= (guint)failed_arg_index; i++) {
         PyGIArgCache *arg_cache = _pygi_callable_cache_get_arg (cache, i);
         PyGIMarshalCleanupFunc cleanup_func = arg_cache->from_py_cleanup;
         gpointer cleanup_data = state->args[i].arg_cleanup_data;
@@ -205,7 +205,7 @@ pygi_marshal_cleanup_args_from_py_parameter_fail (PyGIInvokeState   *state,
                           arg_cache,
                           py_arg,
                           cleanup_data,
-                          i < failed_arg_index);
+                          i < (guint)failed_arg_index);
 
         } else if (arg_cache->is_caller_allocates && cleanup_data != NULL) {
             _cleanup_caller_allocates (state,
diff --git a/gi/pygi-type.c b/gi/pygi-type.c
index 8c75c06f..5ff5184a 100644
--- a/gi/pygi-type.c
+++ b/gi/pygi-type.c
@@ -114,7 +114,7 @@ typedef struct {
 PYGLIB_DEFINE_TYPE("gobject.GType", PyGTypeWrapper_Type, PyGTypeWrapper);
 
 static PyObject*
-generic_long_richcompare(long a, long b, int op)
+generic_gsize_richcompare(gsize a, gsize b, int op)
 {
     PyObject *res;
 
@@ -164,7 +164,7 @@ static PyObject*
 pyg_type_wrapper_richcompare(PyObject *self, PyObject *other, int op)
 {
     if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGTypeWrapper_Type)
-        return generic_long_richcompare(((PyGTypeWrapper*)self)->type,
+        return generic_gsize_richcompare(((PyGTypeWrapper*)self)->type,
                                         ((PyGTypeWrapper*)other)->type,
                                         op);
     else {
@@ -997,7 +997,8 @@ pyg_signal_class_closure_marshal(GClosure *closure,
     gchar *method_name, *tmp;
     PyObject *method;
     PyObject *params, *ret;
-    Py_ssize_t i, len;
+    Py_ssize_t py_len;
+    guint i, len;
 
     state = PyGILState_Ensure();
 
@@ -1049,7 +1050,8 @@ pyg_signal_class_closure_marshal(GClosure *closure,
 
     /* Copy boxed values if others ref them, this needs to be done regardless of
        exception status. */
-    len = PyTuple_Size(params);
+    py_len = PyTuple_Size(params);
+    len = (guint)py_len;
     for (i = 0; i < len; i++) {
        PyObject *item = PyTuple_GetItem(params, i);
        if (item != NULL && PyObject_TypeCheck(item, &PyGBoxed_Type)
diff --git a/gi/pygi-util.c b/gi/pygi-util.c
index 734d02fc..ee322238 100644
--- a/gi/pygi-util.c
+++ b/gi/pygi-util.c
@@ -18,6 +18,20 @@
 
 #include "pygi-util.h"
 
+gboolean
+pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result)
+{
+    if (pyval < 0) {
+        PyErr_SetString (PyExc_ValueError, "< 0");
+        return FALSE;
+    } else if (G_MAXUINT < PY_SSIZE_T_MAX && pyval > (Py_ssize_t)G_MAXUINT) {
+        PyErr_SetString (PyExc_ValueError, "too large");
+        return FALSE;
+    }
+    *result = (guint)pyval;
+    return TRUE;
+}
+
 /* Better alternative to PyImport_ImportModule which tries to import from
  * sys.modules first */
 PyObject *
diff --git a/gi/pygi-util.h b/gi/pygi-util.h
index a8b1ee20..592adf65 100644
--- a/gi/pygi-util.h
+++ b/gi/pygi-util.h
@@ -13,6 +13,8 @@ PyObject * pyg_ptr_richcompare(void* a, void *b, int op);
 const gchar * pyg_constant_strip_prefix(const gchar *name, const gchar *strip_prefix);
 PyObject * pygi_import_module (const char *name);
 
+gboolean pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result);
+
 #if PY_VERSION_HEX >= 0x03000000
 
 #define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
diff --git a/gi/pygi-value.c b/gi/pygi-value.c
index 7d1198d0..c4416fc5 100644
--- a/gi/pygi-value.c
+++ b/gi/pygi-value.c
@@ -875,18 +875,25 @@ pyg_param_gvalue_from_pyobject(GValue* value,
 PyObject*
 pyg_param_gvalue_as_pyobject(const GValue* gvalue,
                              gboolean copy_boxed,
-                            const GParamSpec* pspec)
+                             const GParamSpec* pspec)
 {
     if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
-       gunichar u;
-       Py_UNICODE uni_buffer[2] = { 0, 0 };
-
-       u = g_value_get_uint(gvalue);
-       uni_buffer[0] = u;
-       return PyUnicode_FromUnicode(uni_buffer, 1);
+        gunichar u;
+        gchar *encoded;
+        PyObject *retval;
+
+        u = g_value_get_uint (gvalue);
+        encoded = g_ucs4_to_utf8 (&u, 1, NULL, NULL, NULL);
+        if (encoded == NULL) {
+            PyErr_SetString (PyExc_ValueError, "Failed to decode");
+            return NULL;
+        }
+        retval = PyUnicode_FromString (encoded);
+        g_free (encoded);
+        return retval;
     }
     else {
-       return pyg_value_as_pyobject(gvalue, copy_boxed);
+        return pyg_value_as_pyobject(gvalue, copy_boxed);
     }
 }
 


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