Fix: Python bindings array access functions write out of bounds
[babeltrace.git] / bindings / python / babeltrace.i.in
index dd2daccd25f7298a2a1b55ece6d96f9bda47a3d0..a2ba374db185067dca24b2a7d597b11a0f0d1ff4 100644 (file)
@@ -54,6 +54,36 @@ typedef unsigned long long uint64_t;
 typedef long long int64_t;
 typedef int bt_intern_str;
 
+/* =================================================================
+               PYTHON-COMPLEMENTS.H
+               ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
+*/
+
+FILE *_bt_file_open(char *file_path, char *mode);
+void _bt_file_close(FILE *fp);
+struct bt_definition **_bt_python_field_listcaller(
+               const struct bt_ctf_event *ctf_event,
+               const struct bt_definition *scope,
+               unsigned int *OUTPUT);
+struct bt_definition *_bt_python_field_one_from_list(
+               struct bt_definition **list, int index);
+struct bt_ctf_event_decl **_bt_python_event_decl_listcaller(
+               int handle_id,
+               struct bt_context *ctx,
+               unsigned int *OUTPUT);
+struct bt_ctf_event_decl *_bt_python_decl_one_from_list(
+               struct bt_ctf_event_decl **list, int index);
+struct bt_ctf_field_decl **_by_python_field_decl_listcaller(
+               struct bt_ctf_event_decl *event_decl,
+               enum bt_ctf_scope scope,
+               unsigned int *OUTPUT);
+struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list(
+               struct bt_ctf_field_decl **list, int index);
+struct definition_array *_bt_python_get_array_from_def(
+               struct bt_definition *field);
+struct definition_sequence *_bt_python_get_sequence_from_def(
+               struct bt_definition *field);
+
 /* =================================================================
                CONTEXT.H, CONTEXT-INTERNAL.H
                ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
@@ -477,7 +507,7 @@ class TraceHandle(object):
                raise NotImplementedError("TraceHandle cannot be instantiated")
 
        def __repr__(self):
-               return "Babeltrace TraceHandle: trace_id('{}')".format(self._id)
+               return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id)
 
        def get_id(self):
                """Return the TraceHandle id."""
@@ -560,12 +590,16 @@ struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter);
 %rename("_bt_ctf_get_int_byte_order") bt_ctf_get_int_byte_order(
                const struct bt_declaration *field);
 %rename("_bt_ctf_get_int_len") bt_ctf_get_int_len(const struct bt_declaration *field);
+%rename("_bt_ctf_get_enum_int") bt_ctf_get_enum_int(const struct bt_definition *field);
+%rename("_bt_ctf_get_enum_str") bt_ctf_get_enum_str(const struct bt_definition *field);
 %rename("_bt_ctf_get_encoding") bt_ctf_get_encoding(const struct bt_declaration *field);
 %rename("_bt_ctf_get_array_len") bt_ctf_get_array_len(const struct bt_declaration *field);
 %rename("_bt_ctf_get_uint64") bt_ctf_get_uint64(const struct bt_definition *field);
 %rename("_bt_ctf_get_int64") bt_ctf_get_int64(const struct bt_definition *field);
 %rename("_bt_ctf_get_char_array") bt_ctf_get_char_array(const struct bt_definition *field);
 %rename("_bt_ctf_get_string") bt_ctf_get_string(const struct bt_definition *field);
+%rename("_bt_ctf_get_float") bt_ctf_get_float(const struct bt_definition *field);
+%rename("_bt_ctf_get_variant") bt_ctf_get_variant(const struct bt_definition *field);
 %rename("_bt_ctf_field_get_error") bt_ctf_field_get_error(void);
 %rename("_bt_ctf_get_decl_event_name") bt_ctf_get_decl_event_name(const struct
                bt_ctf_event_decl *event);
@@ -573,6 +607,11 @@ struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter);
                const struct bt_ctf_field_decl *field);
 %rename("_bt_ctf_get_decl_from_def") bt_ctf_get_decl_from_def(
                const struct bt_definition *field);
+%rename("_bt_array_index") bt_array_index(struct definition_array *array, uint64_t i);
+%rename("_bt_sequence_len")  bt_sequence_len(struct definition_sequence *sequence);
+%rename("_bt_sequence_index") bt_sequence_index(struct definition_sequence *sequence, uint64_t i);
+%rename("_bt_ctf_get_struct_field_count") bt_ctf_get_struct_field_count(const struct bt_definition *structure);
+%rename("_bt_ctf_get_struct_field_index") bt_ctf_get_struct_field_index(const struct bt_definition *structure, uint64_t i);
 
 const struct bt_definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
                enum bt_ctf_scope scope);
@@ -591,16 +630,25 @@ int bt_ctf_get_int_signedness(const struct bt_declaration *field);
 int bt_ctf_get_int_base(const struct bt_declaration *field);
 int bt_ctf_get_int_byte_order(const struct bt_declaration *field);
 ssize_t bt_ctf_get_int_len(const struct bt_declaration *field);
+const struct bt_definition *bt_ctf_get_enum_int(const struct bt_definition *field);
+const char *bt_ctf_get_enum_str(const struct bt_definition *field);
 enum ctf_string_encoding bt_ctf_get_encoding(const struct bt_declaration *field);
 int bt_ctf_get_array_len(const struct bt_declaration *field);
+struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i);
 uint64_t bt_ctf_get_uint64(const struct bt_definition *field);
 int64_t bt_ctf_get_int64(const struct bt_definition *field);
 char *bt_ctf_get_char_array(const struct bt_definition *field);
 char *bt_ctf_get_string(const struct bt_definition *field);
+double bt_ctf_get_float(const struct bt_definition *field);
+const struct bt_definition *bt_ctf_get_variant(const struct bt_definition *field);
 int bt_ctf_field_get_error(void);
 const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event);
 const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field);
 const struct bt_declaration *bt_ctf_get_decl_from_def(const struct bt_definition *field);
+uint64_t bt_sequence_len(struct definition_sequence *sequence);
+struct bt_definition *bt_sequence_index(struct definition_sequence *sequence, uint64_t i);
+uint64_t bt_ctf_get_struct_field_count(const struct bt_definition *structure);
+const struct bt_definition *bt_ctf_get_struct_field_index(const struct bt_definition *structure, uint64_t i);
 
 %pythoncode%{
 
@@ -622,6 +670,15 @@ class ctf:
                SEQUENCE = 9
                NR_CTF_TYPES = 10
 
+               def get_type_id_name(id):
+                       name = "UNKNOWN"
+                       constants = [attr for attr in dir(ctf.type_id) if not callable(getattr(ctf.type_id, attr)) and not attr.startswith("__")]
+                       for attr in constants:
+                               if getattr(ctf.type_id, attr) == id:
+                                       name = attr
+                                       break
+                       return name
+
        class scope:
                TRACE_PACKET_HEADER = 0
                STREAM_PACKET_CONTEXT = 1
@@ -744,23 +801,52 @@ class ctf:
                        """
                        return _bt_ctf_get_timestamp(self._e)
 
-               def get_field(self, scope, field):
-                       """Return the definition of a specific field."""
+               def get_field_with_scope(self, scope, field):
+                       """
+                       Return the definition of a specific field.
+                       Return None on error.
+                       """
                        evDef = ctf.Definition.__new__(ctf.Definition)
                        try:
                                evDef._d = _bt_ctf_get_field(self._e, scope._d, field)
                        except AttributeError:
                                raise TypeError("in get_field, argument 2 must be a "
                                        "Definition (scope) instance")
+                       if evDef._d is None:
+                               return None
+                       evDef._s = scope
                        return evDef
 
-               def get_field_list(self, scope):
+               def get_field(self, field):
+                       """
+                       Return the definition of fields by a name
+                       Return None on error
+                       """
+                       eventScope = self.get_top_level_scope(ctf.scope.EVENT_FIELDS)
+                       streamScope = self.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT)
+                       fields_by_name = []
+
+                       if eventScope is not None:
+                               evDef = self.get_field_with_scope(eventScope, field)
+                               if evDef is not None:
+                                       fields_by_name.append(evDef)
+
+                       if streamScope is not None:
+                               evDef = self.get_field_with_scope(streamScope, field)
+                               if evDef is not None:
+                                       fields_by_name.append(evDef);
+
+                       if not fields_by_name:
+                               return None
+                       return fields_by_name
+
+               def get_field_list_with_scope(self, scope):
                        """
-                       Return a list of Definitions
+                       Return a list of Definitions associated with the scope
                        Return None on error.
                        """
                        try:
-                               field_lc = _bt_python_field_listcaller(self._e, scope._d)
+                               field_lc, count = _bt_python_field_listcaller(self._e, scope._d)
                        except AttributeError:
                                raise TypeError("in get_field_list, argument 2 must be a "
                                        "Definition (scope) instance")
@@ -769,18 +855,32 @@ class ctf:
                                return None
 
                        def_list = []
-                       i = 0
-                       while True:
+                       for i in range(count):
                                tmp = ctf.Definition.__new__(ctf.Definition)
                                tmp._d = _bt_python_field_one_from_list(field_lc, i)
+                               tmp._s = scope
+                               def_list.append(tmp)
 
-                               if tmp._d is None:
-                                       #Last item of list is None, assured in
-                                       #_bt_python_field_listcaller
-                                       break
+                       return def_list
 
-                               def_list.append(tmp)
-                               i += 1
+               def get_field_list(self):
+                       """Return a list of Definitions or None on error."""
+                       eventScope = self.get_top_level_scope(ctf.scope.EVENT_FIELDS)
+                       streamScope = self.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT)
+
+                       def_list = []
+                       if eventScope is not None:
+                               event_field_list = self.get_field_list_with_scope(eventScope)
+                               if event_field_list is not None:
+                                       def_list = event_field_list
+
+                       if streamScope is not None:
+                               event_field_list = self.get_field_list_with_scope(streamScope)
+                               if event_field_list is not None:
+                                       def_list.extend(event_field_list)
+
+                       if not def_list:
+                               return None
                        return def_list
 
                def get_index(self, field, index):
@@ -824,6 +924,12 @@ class ctf:
                        else:
                                return ctx
 
+       class FieldError(Exception):
+               def __init__(self, value):
+                       self.value = value
+
+               def __str__(self):
+                       return repr(self.value)
 
        class Definition(object):
                """Definition class.  Do not instantiate."""
@@ -832,7 +938,7 @@ class ctf:
                        raise NotImplementedError("ctf.Definition cannot be instantiated")
 
                def __repr__(self):
-                       return "Babeltrace Definition: name('{}'), type({})".format(
+                       return "Babeltrace Definition: name('{0}'), type({1})".format(
                                self.field_name(), self.field_type())
 
                def field_name(self):
@@ -866,7 +972,14 @@ class ctf:
                        Return the size, in bits, of an int or a negative
                        value on error.
                        """
-                       return _bt_ctf_get_int_len(self._d)
+                       return _bt_ctf_get_int_len(_bt_ctf_get_decl_from_def(self._d))
+
+               def get_enum_str(self):
+                       """
+                       Return the string matching the current enumeration.
+                       Return None on error.
+                       """
+                       return _bt_ctf_get_enum_str(self._d)
 
                def get_encoding(self):
                        """
@@ -882,6 +995,42 @@ class ctf:
                        """
                        return _bt_ctf_get_array_len(_bt_ctf_get_decl_from_def(self._d))
 
+               def get_array_element_at(self, index):
+                       """
+                       Return the array's element at position index.
+                       Return None on error
+                       """
+                       array = _bt_python_get_array_from_def(self._d)
+                       if array is None:
+                               return None
+
+                       element = ctf.Definition.__new__(ctf.Definition)
+                       element._d =  _bt_array_index(array, index)
+                       if element._d is None:
+                               return None
+                       return element
+
+               def get_sequence_len(self):
+                       """
+                       Return the len of a sequence or a negative
+                       value on error.
+                       """
+                       seq = _bt_python_get_sequence_from_def(self._d)
+                       return _bt_sequence_len(seq)
+
+               def get_sequence_element_at(self, index):
+                       """
+                       Return the sequence's element at position index,
+                       otherwise return None
+                       """
+                       seq = _bt_python_get_sequence_from_def(self._d)
+                       if seq is not None:
+                               element = ctf.Definition.__new__(ctf.Definition)
+                               element._d = _bt_sequence_index(seq, index)
+                               if element._d is not None:
+                                       return element
+                       return None
+
                def get_uint64(self):
                        """
                        Return the value associated with the field.
@@ -918,6 +1067,88 @@ class ctf:
                        """
                        return _bt_ctf_get_string(self._d)
 
+               def get_float(self):
+                       """
+                       Return the value associated with the field.
+                       If the field does not exist or is not of the type requested,
+                       the value returned is undefined. To check if an error occured,
+                       use the ctf.field_error() function after accessing a field.
+                       """
+                       return _bt_ctf_get_float(self._d)
+
+               def get_variant(self):
+                       """
+                       Return the variant's selected field.
+                       If the field does not exist or is not of the type requested,
+                       the value returned is undefined. To check if an error occured,
+                       use the ctf.field_error() function after accessing a field.
+                       """
+                       return _bt_ctf_get_variant(self._d)
+
+               def get_struct_field_count(self):
+                       """
+                       Return the number of fields contained in the structure.
+                       If the field does not exist or is not of the type requested,
+                       the value returned is undefined.
+                       """
+                       return _bt_ctf_get_struct_field_count(self._d)
+
+               def get_struct_field_at(self, i):
+                       """
+                       Return the structure's field at position i.
+                       If the field does not exist or is not of the type requested,
+                       the value returned is undefined. To check if an error occured,
+                       use the ctf.field_error() function after accessing a field.
+                       """
+                       return _bt_ctf_get_struct_field_index(self._d, i)
+
+               def get_value(self):
+                       """
+                       Return the value associated with the field according to its type.
+                       Return None on error.
+                       """
+                       id = self.field_type()
+                       value = None
+                       if id == ctf.type_id.STRING:
+                               value = self.get_str()
+                       elif id == ctf.type_id.ARRAY:
+                               value = []
+                               for i in range(self.get_array_len()):
+                                       element = self.get_array_element_at(i)
+                                       value.append(element.get_value())
+                       elif id == ctf.type_id.INTEGER:
+                               if self.get_int_signedness() == 0:
+                                       value = self.get_uint64()
+                               else:
+                                       value = self.get_int64()
+                       elif id == ctf.type_id.ENUM:
+                               value = self.get_enum_str()
+                       elif id == ctf.type_id.SEQUENCE:
+                               seq_len = self.get_sequence_len()
+                               value = []
+                               for i in range(seq_len):
+                                       evDef = self.get_sequence_element_at(i)
+                                       value.append(evDef.get_value())
+                       elif id == ctf.type_id.FLOAT:
+                               value = self.get_float()
+                       elif id == ctf.type_id.VARIANT:
+                               variant = ctf.Definition.__new__(ctf.Definition)
+                               variant._d = self.get_variant();
+                               value = variant.get_value()
+                       elif id == ctf.type_id.STRUCT:
+                               value = {}
+                               for i in range(self.get_struct_field_count()):
+                                       member = ctf.Definition.__new__(ctf.Definition)
+                                       member._d = self.get_struct_field_at(i);
+                                       value[member.field_name()] = member.get_value()
+
+                       if ctf.field_error():
+                               raise ctf.FieldError("Error occured while accessing field {} of type {}".format(self.field_name(), ctf.type_id.get_type_id_name(self.field_type())))
+                       return value
+
+               def get_scope(self):
+                       """Return the scope of a field or None on error."""
+                       return self._s
 
        class EventDecl(object):
                """Event declaration class.  Do not instantiate."""
@@ -926,7 +1157,7 @@ class ctf:
                        raise NotImplementedError("ctf.EventDecl cannot be instantiated")
 
                def __repr__(self):
-                       return "Babeltrace EventDecl: name {}".format(self.get_name())
+                       return "Babeltrace EventDecl: name {0}".format(self.get_name())
 
                def get_name(self):
                        """Return the name of the event or None on error"""
@@ -965,7 +1196,7 @@ class ctf:
                        raise NotImplementedError("ctf.FieldDecl cannot be instantiated")
 
                def __repr__(self):
-                       return "Babeltrace FieldDecl: name {}".format(self.get_name())
+                       return "Babeltrace FieldDecl: name {0}".format(self.get_name())
 
                def get_name(self):
                        """Return the name of a FieldDecl or None on error"""
@@ -993,7 +1224,7 @@ class ctf:
                                raise TypeError("in get_event_decl_list, "
                                        "argument 1 must be a TraceHandle instance")
                try:
-                       ptr_list = _bt_python_event_decl_listcaller(handle_id, context._c)
+                       ptr_list, count = _bt_python_event_decl_listcaller(handle_id, context._c)
                except AttributeError:
                                raise TypeError("in get_event_decl_list, "
                                        "argument 2 must be a Context instance")
@@ -1002,17 +1233,11 @@ class ctf:
                        return None
 
                decl_list = []
-               i = 0
-               while True:
+               for i in range(count):
                        tmp = ctf.EventDecl.__new__(ctf.EventDecl)
                        tmp._d =  _bt_python_decl_one_from_list(ptr_list, i)
-
-                       if tmp._d is None:
-                               #Last item of list is None
-                               break
-
                        decl_list.append(tmp)
-                       i += 1
+
                return decl_list
 
 %}
@@ -1025,8 +1250,6 @@ class ctf:
 //                        python-complements.h
 // =================================================================
 
-%include python-complements.c
-
 %pythoncode %{
 
 class File(object):
@@ -1085,7 +1308,7 @@ class File(object):
                        stat = 'opened'
                else:
                        stat = 'closed'
-               return "{} babeltrace File; file_path('{}'), mode('{}')".format(
+               return "{0} babeltrace File; file_path('{1}'), mode('{2}')".format(
                        stat, self._file_path, self._mode)
 
        def close(self):
This page took 0.028897 seconds and 4 git commands to generate.