Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Forbid GPtrArrays holding non-pointer types
Browse files Browse the repository at this point in the history
It should be safe for bindings to assume that GPtrArrays hold only
pointers (or values as big as it), so there is no need to go through
hoops for converting smaller integers when marshalling.
Libraries that need arrays of integers should use GArray.

https://bugzilla.gnome.org/show_bug.cgi?id=652753
  • Loading branch information
gcampax committed Aug 18, 2011
1 parent e9b0c80 commit 8a4e168
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 35 deletions.
4 changes: 4 additions & 0 deletions giscanner/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ def __init__(self):
GIR_TYPES.extend(BASIC_GIR_TYPES)
GIR_TYPES.extend([TYPE_STRING, TYPE_FILENAME, TYPE_VALIST])

# These are the only basic types that are guaranteed to
# be as big as a pointer (and thus are allowed in GPtrArray)
POINTER_TYPES = [TYPE_ANY, TYPE_INTPTR, TYPE_UINTPTR]

INTROSPECTABLE_BASIC = list(GIR_TYPES)
for v in [TYPE_NONE, TYPE_ANY,
TYPE_LONG_LONG, TYPE_LONG_ULONG,
Expand Down
14 changes: 14 additions & 0 deletions giscanner/maintransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ def _get_position(self, func, param):

return block.position

def _check_array_element_type(self, array, options):
# GPtrArrays are allowed to contain non basic types
# (except enums and flags) or basic types that are
# as big as a gpointer
if array.array_type == ast.Array.GLIB_PTRARRAY and \
((array.element_type in ast.BASIC_GIR_TYPES \
and not array.element_type in ast.POINTER_TYPES) or \
isinstance(array.element_type, ast.Enum) or \
isinstance(array.element_type, ast.Bitfield)):
message.warn("invalid (element-type) for a GPtrArray, "
"must be a pointer", options.position)

def _apply_annotations_array(self, parent, node, options):
array_opt = options.get(OPT_ARRAY)
if array_opt:
Expand Down Expand Up @@ -368,6 +380,7 @@ def _apply_annotations_array(self, parent, node, options):
except ValueError:
# Already warned in annotationparser.py
return
self._check_array_element_type(container_type, options)
node.type = container_type

def _apply_annotations_element_type(self, parent, node, options):
Expand Down Expand Up @@ -409,6 +422,7 @@ def _apply_annotations_element_type(self, parent, node, options):
return
node.type.element_type = self._resolve(element_type_opt.one(),
node.type, node, parent)
self._check_array_element_type(node.type, options)
else:
message.warn_node(parent,
"Unknown container %r for element-type annotation" % (node.type, ))
Expand Down
33 changes: 0 additions & 33 deletions tests/gimarshallingtests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1781,25 +1781,6 @@ gi_marshalling_tests_garray_utf8_full_inout (GArray **array_)
*array_ = result;
}

/**
* gi_marshalling_tests_gptrarray_int_none_return:
* Returns: (element-type gint) (transfer none):
*/
GPtrArray *
gi_marshalling_tests_gptrarray_int_none_return (void)
{
static GPtrArray *parray = NULL;
gint i;

if (parray == NULL) {
parray = g_ptr_array_new ();
for (i = 0; i < 4; i++) {
g_ptr_array_add (parray, GINT_TO_POINTER(i));
}
}

return parray;
}
/**
* gi_marshalling_tests_gptrarray_utf8_none_return:
* Returns: (element-type utf8) (transfer none):
Expand Down Expand Up @@ -1858,20 +1839,6 @@ gi_marshalling_tests_gptrarray_utf8_full_return (void)
return parray;
}

/**
* gi_marshalling_tests_gptrarray_int_none_in:
* @parray_: (element-type gint) (transfer none):
*/
void
gi_marshalling_tests_gptrarray_int_none_in (GPtrArray *parray_)
{
g_assert (parray_->len == 4);
g_assert (g_ptr_array_index (parray_, 0) == GINT_TO_POINTER(0));
g_assert (g_ptr_array_index (parray_, 1) == GINT_TO_POINTER(1));
g_assert (g_ptr_array_index (parray_, 2) == GINT_TO_POINTER(2));
g_assert (g_ptr_array_index (parray_, 3) == GINT_TO_POINTER(3));
}

/**
* gi_marshalling_tests_gptrarray_utf8_none_in:
* @parray_: (element-type utf8) (transfer none):
Expand Down
2 changes: 0 additions & 2 deletions tests/gimarshallingtests.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,10 @@ void gi_marshalling_tests_garray_utf8_container_inout (GArray **array_);
void gi_marshalling_tests_garray_utf8_full_inout (GArray **array_);

/* GPtrArray */
GPtrArray *gi_marshalling_tests_gptrarray_int_none_return (void);
GPtrArray *gi_marshalling_tests_gptrarray_utf8_none_return (void);
GPtrArray *gi_marshalling_tests_gptrarray_utf8_container_return (void);
GPtrArray *gi_marshalling_tests_gptrarray_utf8_full_return (void);

void gi_marshalling_tests_gptrarray_int_none_in (GPtrArray *parray_);
void gi_marshalling_tests_gptrarray_utf8_none_in (GPtrArray *parray_);

void gi_marshalling_tests_gptrarray_utf8_none_out (GPtrArray **parray_);
Expand Down

0 comments on commit 8a4e168

Please sign in to comment.