[prevent][42870] Fix for null pointer dereference
[platform/upstream/at-spi2-core.git] / test / at_spi2_tool.c
index cdbc141..63a3adb 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdbool.h>
 #include <gio/gio.h>
 #include <assert.h>
+#include <stdint.h>
 
 #define ERROR_STATE -1
 #define SAFE_BUFFER_SIZE 2048
@@ -240,7 +241,10 @@ static char *_get_states(AtspiAccessible *node, int length_limit)
 {
        AtspiStateSet *node_state_set = atspi_accessible_get_state_set(node);
        GArray *states = atspi_state_set_get_states(node_state_set);
-       if (!states) return NULL;
+       if (!states) {
+               g_clear_object(&node_state_set);
+               return NULL;
+       }
        g_array_sort(states, _int_sort_function);
 
        AtspiStateType state_type;
@@ -258,7 +262,7 @@ static char *_get_states(AtspiAccessible *node, int length_limit)
        }
 
        g_array_free(states, 0);
-       g_object_unref(node_state_set);
+       g_clear_object(&node_state_set);
 
        _truncate_string(state_string, length_limit);
 
@@ -303,6 +307,9 @@ static char *_get_info(AtspiAccessible *node, int length_limit, bool *attributes
        char *node_name = atspi_accessible_get_name(node, NULL);
        char *node_role_name = atspi_accessible_get_role_name(node, NULL);
        char *unique_id = atspi_accessible_get_unique_id(node, NULL);
+       char *path = atspi_accessible_get_path(node, NULL);
+       unsigned long long eo_ptr = 0;
+       sscanf(path, "%llu", &eo_ptr);
 
        char *attributes = _get_attributes(node, length_limit, attributes_are_too_long);
        Box_Size *box_size = _get_box_size(node);
@@ -312,14 +319,14 @@ static char *_get_info(AtspiAccessible *node, int length_limit, bool *attributes
        bool current_node_has_relations = (relations && relations->len);
 
        char result[SAFE_BUFFER_SIZE];
-       int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%s]]",
-                                               unique_id,
+       int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s(%p)],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%s]]",
+                                               unique_id, (uintptr_t)eo_ptr,
                                                node_role_name,
                                                attributes,
-                                               box_size->x,
-                                               box_size->y,
-                                               box_size->width,
-                                               box_size->height,
+                                               box_size ? box_size->x : "nil",
+                                               box_size ? box_size->y : "nil",
+                                               box_size ? box_size->width : "nil",
+                                               box_size ? box_size->height : "nil",
                                                node_name,
                                                states,
                                                current_node_has_relations ? "*" : "");
@@ -333,6 +340,7 @@ static char *_get_info(AtspiAccessible *node, int length_limit, bool *attributes
        free(node_name);
        free(node_role_name);
        free(unique_id);
+       free(path);
        free(attributes);
        if (box_size) {
                free(box_size->width);
@@ -340,7 +348,8 @@ static char *_get_info(AtspiAccessible *node, int length_limit, bool *attributes
                free(box_size);
        }
        free(states);
-       g_array_free(relations, TRUE);
+       if (relations)
+               g_array_free(relations, TRUE);
 
        return g_strdup(result);
 }
@@ -494,7 +503,8 @@ static void _print_relations_for_object(AtspiAccessible *node) {
 
        printf("\n");
        _print_horizontal_line_in_relations_table();
-       g_array_free(relations, TRUE);
+       if (relations)
+               g_array_free(relations, TRUE);
 }
 
 typedef void (*print_information_about_object_function)(AtspiAccessible *);