Skip to content

Commit

Permalink
Fixed memory leak with DEFAULT(f) on Geometry field
Browse files Browse the repository at this point in the history
MDEV-21056  Assertion `global_status_var.global_memory_used == 0'
failed upon shutdown after query with DEFAULT on a geometry
field
  • Loading branch information
montywi committed Apr 18, 2020
1 parent a6d3297 commit 48eda61
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
7 changes: 7 additions & 0 deletions mysql-test/r/gis.result
Original file line number Diff line number Diff line change
Expand Up @@ -2283,5 +2283,12 @@ ERROR 22003: Cannot get geometry object from data you send to the GEOMETRY field
drop table t1;
SET timestamp=default;
#
# MDEV-21056 Memory leak after query with DEFAULT on a geometry field
#
CREATE TABLE t1 (f POINT DEFAULT ST_GEOMFROMTEXT('Point(0 0)'));
SELECT ST_GEOMFROMTEXT('Point(1 1)') IN ( DEFAULT( `f` ), ST_GEOMFROMTEXT('Point(2 2)') ) AS x FROM t1;
x
DROP TABLE t1;
#
# End of 10.2 tests
#
8 changes: 8 additions & 0 deletions mysql-test/t/gis.test
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,14 @@ alter table t1 add column i int;
drop table t1;
SET timestamp=default;

--echo #
--echo # MDEV-21056 Memory leak after query with DEFAULT on a geometry field
--echo #

CREATE TABLE t1 (f POINT DEFAULT ST_GEOMFROMTEXT('Point(0 0)'));
SELECT ST_GEOMFROMTEXT('Point(1 1)') IN ( DEFAULT( `f` ), ST_GEOMFROMTEXT('Point(2 2)') ) AS x FROM t1;
DROP TABLE t1;

--echo #
--echo # End of 10.2 tests
--echo #
2 changes: 1 addition & 1 deletion sql/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8857,7 +8857,7 @@ int Field_geom::store(const char *from, uint length, CHARSET_INFO *cs)

my_error(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, MYF(0),
Geometry::ci_collection[geom_type]->m_name.str,
wkt.c_ptr(), db, tab_name, field_name,
wkt.c_ptr_safe(), db, tab_name, field_name,
(ulong) table->in_use->get_stmt_da()->
current_row_for_warning());

Expand Down
8 changes: 8 additions & 0 deletions sql/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8968,8 +8968,10 @@ bool Item_default_value::fix_fields(THD *thd, Item **items)
}
if (!(def_field= (Field*) thd->alloc(field_arg->field->size_of())))
goto error;
cached_field= def_field;
memcpy((void *)def_field, (void *)field_arg->field,
field_arg->field->size_of());
def_field->reset_fields();
// If non-constant default value expression
if (def_field->default_value && def_field->default_value->flags)
{
Expand Down Expand Up @@ -8997,6 +8999,12 @@ bool Item_default_value::fix_fields(THD *thd, Item **items)
return TRUE;
}

void Item_default_value::cleanup()
{
delete cached_field; // Free cached blob data
cached_field= 0;
Item_field::cleanup();
}

void Item_default_value::print(String *str, enum_query_type query_type)
{
Expand Down
8 changes: 5 additions & 3 deletions sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -5358,21 +5358,23 @@ class Item_default_value : public Item_field
void calculate();
public:
Item *arg;
Field *cached_field;
Item_default_value(THD *thd, Name_resolution_context *context_arg)
:Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
(const char *)NULL),
arg(NULL) {}
arg(NULL), cached_field(NULL) {}
Item_default_value(THD *thd, Name_resolution_context *context_arg, Item *a)
:Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
(const char *)NULL),
arg(a) {}
arg(a), cached_field(NULL) {}
Item_default_value(THD *thd, Name_resolution_context *context_arg, Field *a)
:Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
(const char *)NULL),
arg(NULL) {}
arg(NULL),cached_field(NULL) {}
enum Type type() const { return DEFAULT_VALUE_ITEM; }
bool eq(const Item *item, bool binary_cmp) const;
bool fix_fields(THD *, Item **);
void cleanup();
void print(String *str, enum_query_type query_type);
String *val_str(String *str);
double val_real();
Expand Down

0 comments on commit 48eda61

Please sign in to comment.