1 /* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include "my_dbug.h"
24 #include "my_loglevel.h"
25 #include "mysql/components/services/log_builtins.h"
26 #include "mysqld_error.h"
27 #include "sql/current_thd.h"
28 #include "sql/log.h"
29 #include "sql/sql_class.h"
30 #include "sql/sql_error.h"
31 
sql_alloc_error_handler(void)32 extern "C" void sql_alloc_error_handler(void) {
33   THD *thd = current_thd;
34   if (thd && !thd->is_error()) {
35     /*
36       This thread is Out Of Memory.
37 
38       An OOM condition is a fatal error. It should not be caught by error
39       handlers in stored procedures.
40 
41       Recording this SQL condition in the condition area could cause more
42       memory allocations, which in turn could raise more OOM conditions,
43       causing recursion in the error handling code itself. As a result,
44       my_error() should not be invoked, and the thread Diagnostics Area is
45       set to an error status directly.
46 
47       Note that Diagnostics_area::set_error_status() is safe, since it does
48       not call any memory allocation routines.
49 
50       The visible result for a client application will be:
51         - a query fails with an ER_OUT_OF_RESOURCES error, returned in the
52           error packet.
53         - SHOW ERROR/SHOW WARNINGS may be empty.
54     */
55     thd->get_stmt_da()->set_error_status(thd, ER_OUT_OF_RESOURCES);
56   }
57 
58   /* Skip writing to the error log to avoid mtr complaints */
59   DBUG_EXECUTE_IF("simulate_out_of_memory", return;);
60 
61   LogErr(ERROR_LEVEL, ER_SERVER_OUT_OF_RESOURCES);
62 }
63