Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH v1 1/1] sql: Fix DEFAULTs AST memory leak on alter
@ 2018-12-25 12:00 Kirill Shcherbatov
  2018-12-25 12:37 ` [tarantool-patches] " Kirill Shcherbatov
  2018-12-25 13:29 ` Kirill Yukhin
  0 siblings, 2 replies; 4+ messages in thread
From: Kirill Shcherbatov @ 2018-12-25 12:00 UTC (permalink / raw)
  To: tarantool-patches, v.shpilevoy; +Cc: Kirill Shcherbatov

The space_def_destroy_fields routine is used in make_scoped_guard
on alter:space_def_new_from_tuple always pass extern_alloc=true
for sql_expr_delete routine. It shouldn't as the first-time
allocated AST (field_def_decode) is not external-allocated.
Introduced a new flag 'extern_alloc' for space_def_destroy_fields
routine.

Closes #3908

http://github.com/tarantool/tarantool/tree/kshch/gh-3908-fix-ast-alter-memleak
https://github.com/tarantool/tarantool/issues/3908

---
 src/box/alter.cc    | 4 ++--
 src/box/space_def.c | 7 ++++---
 src/box/space_def.h | 4 +++-
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/box/alter.cc b/src/box/alter.cc
index 03ba68adc..5ec57590e 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -452,7 +452,7 @@ space_format_decode(const char *data, uint32_t *out_count,
 	 */
 	memset(region_defs, 0, size);
 	auto fields_guard = make_scoped_guard([=] {
-		space_def_destroy_fields(region_defs, count);
+		space_def_destroy_fields(region_defs, count, false);
 	});
 	for (uint32_t i = 0; i < count; ++i) {
 		field_def_decode(&region_defs[i], &data, space_name, name_len,
@@ -515,7 +515,7 @@ space_def_new_from_tuple(struct tuple *tuple, uint32_t errcode,
 	fields = space_format_decode(format, &field_count, name,
 				     name_len, errcode, region);
 	auto fields_guard = make_scoped_guard([=] {
-		space_def_destroy_fields(fields, field_count);
+		space_def_destroy_fields(fields, field_count, false);
 	});
 	if (exact_field_count != 0 &&
 	    exact_field_count < field_count) {
diff --git a/src/box/space_def.c b/src/box/space_def.c
index 69b92c5c1..3516bdd8d 100644
--- a/src/box/space_def.c
+++ b/src/box/space_def.c
@@ -259,12 +259,13 @@ space_def_new(uint32_t id, uint32_t uid, uint32_t exact_field_count,
 
 /** Free a default value's syntax trees of @a defs. */
 void
-space_def_destroy_fields(struct field_def *fields, uint32_t field_count)
+space_def_destroy_fields(struct field_def *fields, uint32_t field_count,
+			 bool extern_alloc)
 {
 	for (uint32_t i = 0; i < field_count; ++i) {
 		if (fields[i].default_value_expr != NULL) {
 			sql_expr_delete(sql_get(), fields[i].default_value_expr,
-					true);
+					extern_alloc);
 		}
 	}
 }
@@ -274,7 +275,7 @@ space_def_delete(struct space_def *def)
 {
 	space_opts_destroy(&def->opts);
 	tuple_dictionary_unref(def->dict);
-	space_def_destroy_fields(def->fields, def->field_count);
+	space_def_destroy_fields(def->fields, def->field_count, true);
 	TRASH(def);
 	free(def);
 }
diff --git a/src/box/space_def.h b/src/box/space_def.h
index 0d1e90233..e99581dab 100644
--- a/src/box/space_def.h
+++ b/src/box/space_def.h
@@ -122,9 +122,11 @@ struct space_def {
  * Free a default value syntax trees of @a defs.
  * @param fields Fields array to destroy.
  * @param field_count Length of @a fields.
+ * @param extern_alloc Fields expression AST allocated externally.
  */
 void
-space_def_destroy_fields(struct field_def *fields, uint32_t field_count);
+space_def_destroy_fields(struct field_def *fields, uint32_t field_count,
+			 bool extern_alloc);
 
 /**
  * Delete the space_def object.
-- 
2.19.2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] sql: Fix DEFAULTs AST memory leak on alter
  2018-12-25 12:00 [tarantool-patches] [PATCH v1 1/1] sql: Fix DEFAULTs AST memory leak on alter Kirill Shcherbatov
@ 2018-12-25 12:37 ` Kirill Shcherbatov
  2018-12-25 12:38   ` Vladislav Shpilevoy
  2018-12-25 13:29 ` Kirill Yukhin
  1 sibling, 1 reply; 4+ messages in thread
From: Kirill Shcherbatov @ 2018-12-25 12:37 UTC (permalink / raw)
  To: tarantool-patches, Vladislav Shpilevoy

Extras: catch another one memleak, better comment

diff --git b/src/box/alter.cc a/src/box/alter.cc
index 5ec57590e..27b06c083 100644
--- b/src/box/alter.cc
+++ a/src/box/alter.cc
@@ -540,6 +540,7 @@ space_def_new_from_tuple(struct tuple *tuple, uint32_t errcode,
                space_def_new_xc(id, uid, exact_field_count, name, name_len,
                                 engine_name, engine_name_len, &opts, fields,
                                 field_count);
+       auto def_guard = make_scoped_guard([=] { space_def_delete(def); });
        if (def->opts.checks != NULL &&
            sql_checks_resolve_space_def_reference(def->opts.checks,
                                                   def) != 0) {
@@ -551,7 +552,6 @@ space_def_new_from_tuple(struct tuple *tuple, uint32_t errcode,
                        diag_raise();
                }
        }
-       auto def_guard = make_scoped_guard([=] { space_def_delete(def); });
        struct engine *engine = engine_find_xc(def->engine_name);
        engine_check_space_def_xc(engine, def);
        def_guard.is_active = false;
diff --git b/src/box/space_def.h a/src/box/space_def.h
index e99581dab..8044f88fd 100644
--- b/src/box/space_def.h
+++ a/src/box/space_def.h
@@ -123,6 +123,9 @@ struct space_def {
  * @param fields Fields array to destroy.
  * @param field_count Length of @a fields.
  * @param extern_alloc Fields expression AST allocated externally.
+ *                     (specify false when sql_expr_delete should
+ *                      release default_value_expr memory,
+ *                      true - when shouldn't)
  */

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] sql: Fix DEFAULTs AST memory leak on alter
  2018-12-25 12:37 ` [tarantool-patches] " Kirill Shcherbatov
@ 2018-12-25 12:38   ` Vladislav Shpilevoy
  0 siblings, 0 replies; 4+ messages in thread
From: Vladislav Shpilevoy @ 2018-12-25 12:38 UTC (permalink / raw)
  To: Kirill Shcherbatov, tarantool-patches, Kirill Yukhin

LGTM.

On 25/12/2018 15:37, Kirill Shcherbatov wrote:
> Extras: catch another one memleak, better comment
> 
> diff --git b/src/box/alter.cc a/src/box/alter.cc
> index 5ec57590e..27b06c083 100644
> --- b/src/box/alter.cc
> +++ a/src/box/alter.cc
> @@ -540,6 +540,7 @@ space_def_new_from_tuple(struct tuple *tuple, uint32_t errcode,
>                  space_def_new_xc(id, uid, exact_field_count, name, name_len,
>                                   engine_name, engine_name_len, &opts, fields,
>                                   field_count);
> +       auto def_guard = make_scoped_guard([=] { space_def_delete(def); });
>          if (def->opts.checks != NULL &&
>              sql_checks_resolve_space_def_reference(def->opts.checks,
>                                                     def) != 0) {
> @@ -551,7 +552,6 @@ space_def_new_from_tuple(struct tuple *tuple, uint32_t errcode,
>                          diag_raise();
>                  }
>          }
> -       auto def_guard = make_scoped_guard([=] { space_def_delete(def); });
>          struct engine *engine = engine_find_xc(def->engine_name);
>          engine_check_space_def_xc(engine, def);
>          def_guard.is_active = false;
> diff --git b/src/box/space_def.h a/src/box/space_def.h
> index e99581dab..8044f88fd 100644
> --- b/src/box/space_def.h
> +++ a/src/box/space_def.h
> @@ -123,6 +123,9 @@ struct space_def {
>    * @param fields Fields array to destroy.
>    * @param field_count Length of @a fields.
>    * @param extern_alloc Fields expression AST allocated externally.
> + *                     (specify false when sql_expr_delete should
> + *                      release default_value_expr memory,
> + *                      true - when shouldn't)
>    */
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tarantool-patches] Re: [PATCH v1 1/1] sql: Fix DEFAULTs AST memory leak on alter
  2018-12-25 12:00 [tarantool-patches] [PATCH v1 1/1] sql: Fix DEFAULTs AST memory leak on alter Kirill Shcherbatov
  2018-12-25 12:37 ` [tarantool-patches] " Kirill Shcherbatov
@ 2018-12-25 13:29 ` Kirill Yukhin
  1 sibling, 0 replies; 4+ messages in thread
From: Kirill Yukhin @ 2018-12-25 13:29 UTC (permalink / raw)
  To: tarantool-patches; +Cc: v.shpilevoy, Kirill Shcherbatov

Hello,

On 25 Dec 15:00, Kirill Shcherbatov wrote:
> The space_def_destroy_fields routine is used in make_scoped_guard
> on alter:space_def_new_from_tuple always pass extern_alloc=true
> for sql_expr_delete routine. It shouldn't as the first-time
> allocated AST (field_def_decode) is not external-allocated.
> Introduced a new flag 'extern_alloc' for space_def_destroy_fields
> routine.
> 
> Closes #3908
> 
> http://github.com/tarantool/tarantool/tree/kshch/gh-3908-fix-ast-alter-memleak
> https://github.com/tarantool/tarantool/issues/3908

I've committed your patch into 2.1 branch.

--
Regards, Kirill Yukhin

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-12-25 13:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-25 12:00 [tarantool-patches] [PATCH v1 1/1] sql: Fix DEFAULTs AST memory leak on alter Kirill Shcherbatov
2018-12-25 12:37 ` [tarantool-patches] " Kirill Shcherbatov
2018-12-25 12:38   ` Vladislav Shpilevoy
2018-12-25 13:29 ` Kirill Yukhin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox