Re: [PATCH v2 28/31] Use ui_file_as_string throughout more

Yao Qi
2017-02-23T10:23:05Z

Pedro Alves <palves@redhat.com> writes:

Hi Pedro,

> std::string as member of a struct, I needed to adjust
> allocation/destruction of said struct to use new/delete instead of
> xmalloc/xfree.
>

> diff --git a/gdb/varobj-iter.h b/gdb/varobj-iter.h
> index bdbf661..1e7889b 100644
> --- a/gdb/varobj-iter.h
> +++ b/gdb/varobj-iter.h
> @@ -19,7 +19,7 @@
> typedef struct varobj_item
> {
> /* Name of this item. */
> - char *name;
> + std::string name;
>
> /* Value of this item. */
> struct value *value;
> @@ -67,6 +67,6 @@ struct varobj_iter_ops
> if ((ITER) != NULL) \
> { \
> (ITER)->ops->dtor (ITER); \
> - xfree (ITER); \
> + delete (ITER); \

We don't need to use "delete" for varobj_iter, because you didn't add
std::string member in varobj_iter. This change causes an ASAN error,

-var-create container @ c^M
=================================================================^M
^[[1m^[[31m==12975==ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete) on 0x603000085e40^M
^[[1m^[[0m #0 0x2b8ce74d15d7 in operator delete(void*) (/usr/lib/x86_64-linux-gnu/libasan.so.1+0x555d7)^M
#1 0xc13d3c in update_dynamic_varobj_children /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:786^M
#2 0xc1425f in varobj_get_num_children(varobj*) /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:846^M
#3 0x59ffeb in print_varobj /home/yao/SourceCode/gnu/gdb/git/gdb/mi/mi-cmd-var.c:61^M
#4 0x5a0689 in mi_cmd_var_create(char*, char**, int) /home/yao/SourceCode/gnu/gdb/git/gdb/mi/mi-cmd-var.c:145^M
#5 0x5b5ac9 in mi_cmd_execute /home/yao/SourceCode/gnu/gdb/git/gdb/mi/mi-main.c:2295^M
#6 0x5b3de0 in captured_mi_execute_command /home/yao/SourceCode/gnu/gdb/git/gdb/mi/mi-main.c:1997^M
.....
^M
^[[1m^[[32m0x603000085e40 is located 0 bytes inside of 32-byte region [0x603000085e40,0x603000085e60)^M
^[[1m^[[0m^[[1m^[[35mallocated by thread T0 here:^[[1m^[[0m^M
#0 0x2b8ce74d0862 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.1+0x54862)^M
#1 0x80df7f in xmalloc /home/yao/SourceCode/gnu/gdb/git/gdb/common/common-utils.c:43^M
#2 0x6364ec in py_varobj_iter_new /home/yao/SourceCode/gnu/gdb/git/gdb/python/py-varobj.c:154^M
#3 0x636710 in py_varobj_get_iterator(varobj*, _object*) /home/yao/SourceCode/gnu/gdb/git/gdb/python/py-varobj.c:188^M
#4 0xc13580 in varobj_get_iterator /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:712^M
#5 0xc138fa in update_dynamic_varobj_children /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:750^M
#6 0xc1425f in varobj_get_num_children(varobj*) /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:846^M

reverting the change above fixes the error. We can also C++-fy
varobj_iter, but I'd like to do in another patch. How is the patch below?

> } \
> } while (0)

> diff --git a/gdb/varobj.h b/gdb/varobj.h
> index 6b9a71f..7f4aad2 100644
> --- a/gdb/varobj.h
> +++ b/gdb/varobj.h
> @@ -86,22 +86,22 @@ struct varobj_dynamic;
>
> /* Every variable in the system has a structure of this type defined
> for it. This structure holds all information necessary to manipulate
> - a particular object variable. Members which must be freed are noted. */
> + a particular object variable. */
> struct varobj
> {
> - /* Alloc'd name of the variable for this object. If this variable is a
> + /* Name of the variable for this object. If this variable is a
> child, then this name will be the child's source name.
> (bar, not foo.bar). */
> /* NOTE: This is the "expression". */
> - char *name;
> + std::string name;
>
> - /* Alloc'd expression for this child. Can be used to create a
> - root variable corresponding to this child. */
> - char *path_expr;
> + /* Expression for this child. Can be used to create a root variable
> + corresponding to this child. */
> + std::string path_expr;
>
> - /* The alloc'd name for this variable's object. This is here for
> + /* The name for this variable's object. This is here for
> convenience when constructing this object's children. */
> - char *obj_name;
> + std::string obj_name;
>
> /* Index of this variable in its parent or -1. */
> int index;
> @@ -137,7 +137,7 @@ struct varobj
> int updated;
>
> /* Last print value. */
> - char *print_value;
> + std::string print_value;
>
> /* Is this variable frozen. Frozen variables are never implicitly
> updated by -var-update *
> @@ -170,18 +170,15 @@ struct lang_varobj_ops
> /* The number of children of PARENT. */
> int (*number_of_children) (const struct varobj *parent);
>
> - /* The name (expression) of a root varobj. The returned value must be freed
> - by the caller. */
> - char *(*name_of_variable) (const struct varobj *parent);
> + /* The name (expression) of a root varobj. */
> + std::string (*name_of_variable) (const struct varobj *parent);
>
> - /* The name of the INDEX'th child of PARENT. The returned value must be
> - freed by the caller. */
> - char *(*name_of_child) (const struct varobj *parent, int index);
> + /* The name of the INDEX'th child of PARENT. */
> + std::string (*name_of_child) (const struct varobj *parent, int index);
>
> /* Returns the rooted expression of CHILD, which is a variable
> - obtain that has some parent. The returned value must be freed by the
> - caller. */
> - char *(*path_expr_of_child) (const struct varobj *child);
> + obtain that has some parent. */
> + std::string (*path_expr_of_child) (const struct varobj *child);
>
> /* The ``struct value *'' of the INDEX'th child of PARENT. */
> struct value *(*value_of_child) (const struct varobj *parent, int index);
> @@ -189,10 +186,9 @@ struct lang_varobj_ops
> /* The type of the INDEX'th child of PARENT. */
> struct type *(*type_of_child) (const struct varobj *parent, int index);
>
> - /* The current value of VAR. The returned value must be freed by the
> - caller. */
> - char *(*value_of_variable) (const struct varobj *var,
> - enum varobj_display_formats format);
> + /* The current value of VAR. */
> + std::string (*value_of_variable) (const struct varobj *var,
> + enum varobj_display_formats format);
>
> /* Return non-zero if changes in value of VAR must be detected and
> reported by -var-update. Return zero if -var-update should never

--
Yao (齐尧)
From 71dc64a061cfb1ec57b753e495c02665827c7a01 Mon Sep 17 00:00:00 2001
From: Yao Qi <yao.qi@linaro.org>
Date: Thu, 23 Feb 2017 09:29:48 +0000
Subject: [PATCH] Use xfree rather than delete for varobj_iter

ASAN reports an error,

-var-create container @ c^M
=================================================================^M
^[[1m^[[31m==21639==ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete) on 0x6030000805c0^M
^[[1m^[[0m #0 0x7f2449b01b2a in operator delete(void*) (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99b2a)^M
#1 0xbb601d in update_dynamic_varobj_children ../../binutils-gdb/gdb/varobj.c:794^M
#2 0xbb6556 in varobj_get_num_children(varobj*) ../../binutils-gdb/gdb/varobj.c:854^M
#3 0x580cb4 in print_varobj ../../binutils-gdb/gdb/mi/mi-cmd-var.c:61^M
#4 0x58138b in mi_cmd_var_create(char*, char**, int) ../../binutils-gdb/gdb/mi/mi-cmd-var.c:145^M
#5 0x5967ce in mi_cmd_execute ../../binutils-gdb/gdb/mi/mi-main.c:2301^M
#6 0x594b05 in captured_mi_execute_command ../../binutils-gdb/gdb/mi/mi-main.c:2001
....
^M
^[[1m^[[32m0x6030000805c0 is located 0 bytes inside of 32-byte region [0x6030000805c0,0x6030000805e0)^M
^[[1m^[[0m^[[1m^[[35mallocated by thread T0 here:^[[1m^[[0m^M
#0 0x7f2449b00602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)^M
#1 0x7d1596 in xmalloc ../../binutils-gdb/gdb/common/common-utils.c:43^M
#2 0x604176 in py_varobj_iter_new ../../binutils-gdb/gdb/python/py-varobj.c:159^M
#3 0x6042da in py_varobj_get_iterator(varobj*, _object*) ../../binutils-gdb/gdb/python/py-varobj.c:198^M
#4 0xbb5806 in varobj_get_iterator ../../binutils-gdb/gdb/varobj.c:720^M
#5 0xbb5b9b in update_dynamic_varobj_children ../../binutils-gdb/gdb/varobj.c:758^M

gdb:

2017-02-23 Yao Qi <yao.qi@linaro.org>

* varobj-iter.h (varobj_iter_delete): Call xfree instead of
delete.

diff --git a/gdb/varobj-iter.h b/gdb/varobj-iter.h
index 34182e0..7eed4f1 100644
--- a/gdb/varobj-iter.h
+++ b/gdb/varobj-iter.h
@@ -67,6 +67,6 @@ struct varobj_iter_ops
if ((ITER) != NULL) \
{ \
(ITER)->ops->dtor (ITER); \
- delete (ITER); \
+ xfree (ITER); \
} \
} while (0)