1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

MDEV-24507: Server Crash using UDF in WHERE clause of VIEW

These changes are submitted under the BSD 3-clause License.

The original ticket describes a server crash when using a UDF in the WHERE clause of a view.  The crash also happens when using a UDF in the WHERE clause of a SELECT that uses a sub-query in the FROM clause.

When the UDF does not have a _deinit function the server crashes in udf_handler::cleanup (sql/item_func.cc:3467).
When the UDF has both an _init and a _deinit function but _init does not allocate memory for initid->ptr the server crashes in udf_handler::cleanup (sql/item_func.cc:3467).
When the UDF has both an _init and a _deinit function and allocates/deallocates  memory for initid->ptr the server crashes in the memory deallocation of the _deinit function.

The sequence of events seen are:
  1. A UDF, U, is created for the query.
  2. The UDF _init function is called using U->initid.
  3. U is cloned for the sub-query using the [default|implicit] copy constructor, resulting in V.
  4. The UDF _init function is called using V->initid.  U->initid and V->initid are the same value.
  5. The UDF function is called.
  6. The UDF _deinit function is called using U->initid.  If any memory was allocated for initid->ptr it is deallocated here.
  7. udf_handler::cleanup deletes the U->buffers String array.
  8. The UDF _deinit function is called using V->initid.  If any memory was allocated for initid->ptr it was previously deallocated and _deinit crashes the server.
  9. udf_handler::cleanup deletes the V->buffers String array. V->buffers was the same values as U->buffers which was already deallocated.  The server crashes.

The solution is to create a[n explicit] copy constructor for udf_handler which sets not_original to true.  Later, not_original is set back to false (0) after udf_handler::fix_fields has set up a new value for initid->ptr.
This commit is contained in:
Sean Adams
2023-11-21 11:50:55 -08:00
committed by Daniel Black
parent b909b525f4
commit 3281b6b8a3
4 changed files with 113 additions and 0 deletions

View File

@ -147,6 +147,20 @@ class udf_handler :public Sql_alloc
*null_value= (my_bool) (is_null || error);
}
String *val_str(String *str,String *save_str);
udf_handler(const udf_handler &orig)
{
u_d = orig.u_d;
buffers = orig.buffers;
f_args = orig.f_args;
initid = orig.initid;
num_buffer = orig.num_buffer;
error = orig.error;
is_null = orig.is_null;
initialized = orig.initialized;
args = orig.args;
not_original = true;
}
};