From 582f80f5874e357d19bbbc2e5884260e7e11e04e Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Fri, 16 Dec 2016 15:54:58 +0100 Subject: [PATCH] [at_spi2_tool] truncation of states string added; small refactor works Change-Id: I2b3b139ae33d575951a7faa9ff96df6e285f6a1c --- test/at_spi2_tool.c | 105 ++++++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/test/at_spi2_tool.c b/test/at_spi2_tool.c index b8f051f..af7eb97 100644 --- a/test/at_spi2_tool.c +++ b/test/at_spi2_tool.c @@ -236,7 +236,7 @@ static Box_Size *_get_box_size(AtspiAccessible *node) return box_size; } -static char *_get_states(AtspiAccessible *node) +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); @@ -256,13 +256,14 @@ static char *_get_states(AtspiAccessible *node) g_array_free(states, 0); g_object_unref(node_state_set); + _truncate_string(state_string, length_limit); + return state_string; } -static char *_get_attributes(AtspiAccessible *node) +static char *_get_attributes(AtspiAccessible *node, int length_limit) { - GHashTable *attributes = NULL; - attributes = atspi_accessible_get_attributes(node, NULL); + GHashTable *attributes = atspi_accessible_get_attributes(node, NULL); if (!attributes) return NULL; @@ -274,40 +275,41 @@ static char *_get_attributes(AtspiAccessible *node) char *result = NULL; while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) { char attributes_string[ATTR_WIDTH]; - int ret = snprintf(attributes_string, ATTR_WIDTH, "(%s=%s)", (char*)attr_key, (char*)attr_value); - if (ret >= ATTR_WIDTH) { - fprintf(stderr, "_get_attributes: generated string is too long. Buffer overflow"); - attributes_string[ATTR_WIDTH - 1] = '\n'; - } + int ret = snprintf(attributes_string, ATTR_WIDTH, "(%s=%s)", (char *)attr_key, (char *)attr_value); + if (ret >= ATTR_WIDTH) + fprintf(stderr, "\n_get_attributes: generated string is too long. Buffer overflow\n"); + _combine_strings(&result, attributes_string); } g_hash_table_unref(attributes); + _truncate_string(result, length_limit); return result; } -static AtspiAccessible *_get_object_in_relation(AtspiAccessible * source, AtspiRelationType search_type) +static AtspiAccessible *_get_object_in_relation(AtspiAccessible *source, AtspiRelationType search_type) { - GArray *relations; - AtspiAccessible *ret = NULL; - AtspiRelation *relation; - AtspiRelationType type; - int i; - if (source) { - relations = atspi_accessible_get_relation_set(source, NULL); - if (relations) { - for (i = 0; i < relations->len; i++) { - relation = g_array_index(relations, AtspiRelation *, i); - type = atspi_relation_get_relation_type(relation); - - if (type == search_type) { - ret = atspi_relation_get_target(relation, 0); - break; - } - } - g_array_free(relations, TRUE); + if (source == NULL) + return NULL; + + GArray *relations = atspi_accessible_get_relation_set(source, NULL); + + if (relations == NULL) + return NULL; + + AtspiAccessible *ret; + for (int i = 0; i < relations->len; ++i) { + AtspiRelation *relation = g_array_index(relations, AtspiRelation *, i); + AtspiRelationType type = atspi_relation_get_relation_type(relation); + + if (type == search_type) { + ret = atspi_relation_get_target(relation, 0); + break; } } + + g_array_free(relations, TRUE); + return ret; } @@ -319,11 +321,9 @@ static char *_get_info(AtspiAccessible *node, int length_limit) char *node_name = atspi_accessible_get_name(node, NULL); char *node_role_name = atspi_accessible_get_role_name(node, NULL); - char *attributes = _get_attributes(node); - _truncate_string(attributes, length_limit); + char *attributes = _get_attributes(node, length_limit); Box_Size *box_size = _get_box_size(node); - char *states = _get_states(node); - _truncate_string(states, length_limit); + char *states = _get_states(node, length_limit); char result[SAFE_BUFFER_SIZE]; int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%p],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%p,%p]]", @@ -339,10 +339,8 @@ static char *_get_info(AtspiAccessible *node, int length_limit) _get_object_in_relation(node, ATSPI_RELATION_FLOWS_TO), _get_object_in_relation(node, ATSPI_RELATION_FLOWS_FROM)); - if (ret >= SAFE_BUFFER_SIZE) { - fprintf(stderr, "_get_info: generated string is too long. Buffer overflow"); - result[SAFE_BUFFER_SIZE - 1] = '\n'; - } + if (ret >= SAFE_BUFFER_SIZE) + fprintf(stderr, "\n_get_info: generated string is too long. Buffer overflow\n"); free(node_name); free(node_role_name); @@ -438,29 +436,30 @@ static void _atspi_tree_traverse(AtspiAccessible *desktop, const char *app_name, bool app_name_matched = false; for (int i = 0; i < count; i++) { AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL); - if (child) { - char *name = atspi_accessible_get_name(child, NULL); + if (child == NULL) + break; + + char *name = atspi_accessible_get_name(child, NULL); + + if (!dump && !check) + printf("%s\n", name); - if (!dump && !check) { - printf("%s\n", name); - } + if ((check || dump) && name && !strcmp(name, app_name)) { + app_name_matched = true; - if ((check || dump) && name && !strcmp(name, app_name)) { - app_name_matched = true; + _print_module_legend(); - _print_module_legend(); + if (check) + _test_atspi_parent_child_relation(child, desktop, i); - if (check) - _test_atspi_parent_child_relation(child, desktop, i); + _print_atspi_tree_verify_maybe_r(0, child, check, length_limit); - _print_atspi_tree_verify_maybe_r(0, child, check, length_limit); + if (first_match) { + free(name); + break; + } else + printf("\n"); - if (first_match) { - free(name); - break; - } else - printf("\n"); - } free(name); } } -- 2.7.4