diff options
author | Holger Berndt <hb@gnome.org> | 2011-10-30 16:36:32 +0100 |
---|---|---|
committer | Holger Berndt <hb@gnome.org> | 2011-11-01 18:50:51 +0100 |
commit | eef35b2df8023ffff2d195ee16c084f5cfcb6ba3 (patch) | |
tree | f7b913de6fca2922f91a5dad6e70ac1779b2c779 | |
parent | 4c1d9f01b8fa6702f73b290180f934250e179caa (diff) | |
download | pygobject-eef35b2df8023ffff2d195ee16c084f5cfcb6ba3.tar.gz |
Fix array termination and size calculation
When creating an array of element type uint8 and setting it directly with
memcpy(), make sure that zero-termination is respected.
When calculating the length of a zero-terminated array of type uint8,
fall back to strlen() instead of g_strv_length().
https://bugzilla.gnome.org/show_bug.cgi?id=662550
-rw-r--r-- | gi/pygi-marshal-from-py.c | 6 | ||||
-rw-r--r-- | gi/pygi-marshal-to-py.c | 8 |
2 files changed, 12 insertions, 2 deletions
diff --git a/gi/pygi-marshal-from-py.c b/gi/pygi-marshal-from-py.c index 0a94ffe1..3b3109c6 100644 --- a/gi/pygi-marshal-from-py.c +++ b/gi/pygi-marshal-from-py.c @@ -794,7 +794,11 @@ _pygi_marshal_from_py_array (PyGIInvokeState *state, if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8 && PYGLIB_PyBytes_Check (py_arg)) { memcpy(array_->data, PYGLIB_PyBytes_AsString (py_arg), length); - + if (sequence_cache->is_zero_terminated) { + /* If array_ has been created with zero_termination, space for the + * terminator is properly allocated, so we're not off-by-one here. */ + array_->data[length] = '\0'; + } goto array_success; } diff --git a/gi/pygi-marshal-to-py.c b/gi/pygi-marshal-to-py.c index 984e7c1c..67c21cb0 100644 --- a/gi/pygi-marshal-to-py.c +++ b/gi/pygi-marshal-to-py.c @@ -266,6 +266,8 @@ _pygi_marshal_to_py_array (PyGIInvokeState *state, array_ = arg->v_pointer; + g_assert(array_ != NULL); + /* GArrays make it easier to iterate over arrays * with different element sizes but requires that * we allocate a GArray if the argument was a C array @@ -275,7 +277,11 @@ _pygi_marshal_to_py_array (PyGIInvokeState *state, if (seq_cache->fixed_size >= 0) { len = seq_cache->fixed_size; } else if (seq_cache->is_zero_terminated) { - len = g_strv_length ((gchar **)arg->v_pointer); + if(seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) { + len = strlen (arg->v_pointer); + } else { + len = g_strv_length ((gchar **)arg->v_pointer); + } } else { GIArgument *len_arg = state->args[seq_cache->len_arg_index]; len = len_arg->v_long; |