Debian Codegraph @

libbinlogevents/src/statement_events.cpp

raw
1 /* Copyright (c) 2014, 2026, Oracle and/or its affiliates.
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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include "statement_events.h"
25 #include <algorithm> // std::min() for Windows
26 #include "event_reader_macros.h"
27
28 namespace binary_log {
29
30 /******************************************************************************
31 Query_event methods
32 ******************************************************************************/
33 /**
34 The simplest constructor that could possibly work. This is used for
35 creating static objects that have a special meaning and are invisible
36 to the log.
37 */
38 Query_event::Query_event(Log_event_type type_arg)
39 : Binary_log_event(type_arg),
40 query(nullptr),
41 db(nullptr),
42 user(nullptr),
43 user_len(0),
44 host(nullptr),
45 host_len(0),
46 db_len(0),
47 q_len(0) {}
48
49 /**
50 The constructor used by MySQL master to create a query event, to be
51 written to the binary log.
52 */
53 Query_event::Query_event(
54 const char *query_arg, const char *catalog_arg, const char *db_arg,
55 uint32_t query_length, unsigned long thread_id_arg,
56 unsigned long long sql_mode_arg, unsigned long auto_increment_increment_arg,
57 unsigned long auto_increment_offset_arg, unsigned int number,
58 unsigned long long table_map_for_update_arg, int errcode)
59 : Binary_log_event(QUERY_EVENT),
60 query(query_arg),
61 db(db_arg),
62 catalog(catalog_arg),
63 user(nullptr),
64 user_len(0),
65 host(nullptr),
66 host_len(0),
67 thread_id(thread_id_arg),
68 db_len(0),
69 error_code(errcode),
70 status_vars_len(0),
71 q_len(query_length),
72 flags2_inited(true),
73 sql_mode_inited(true),
74 charset_inited(true),
75 sql_mode(sql_mode_arg),
76 auto_increment_increment(
77 static_cast<uint16_t>(auto_increment_increment_arg)),
78 auto_increment_offset(static_cast<uint16_t>(auto_increment_offset_arg)),
79 time_zone_len(0),
80 lc_time_names_number(number),
81 charset_database_number(0),
82 table_map_for_update(table_map_for_update_arg),
83 explicit_defaults_ts(TERNARY_UNSET),
84 mts_accessed_dbs(0),
85 ddl_xid(INVALID_XID),
86 default_collation_for_utf8mb4_number(0),
87 sql_require_primary_key(0xff),
88 default_table_encryption(0xff) {}
89
90 /**
91 Utility function for the Query_event constructor.
92 The function copies n bytes from the source string and moves the
93 destination pointer by the number of bytes copied.
94
95 @param dst Pointer to the buffer into which the string is to be copied
96 @param src Source string
97 @param len The number of bytes to be copied
98 */
99 static void copy_str_and_move(Log_event_header::Byte **dst, const char **src,
100 size_t len) {
101 memcpy(*dst, *src, len);
102 *src = reinterpret_cast<const char *>(*dst);
103 (*dst) += len;
104 *(*dst)++ = 0;
105 }
106
107 /**
108 The event occurs when an updating statement is done.
109 */
110 Query_event::Query_event(const char *buf, const Format_description_event *fde,
111 Log_event_type event_type)
112 : Binary_log_event(&buf, fde),
113 query(nullptr),
114 db(nullptr),
115 catalog(nullptr),
116 time_zone_str(nullptr),
117 user(nullptr),
118 user_len(0),
119 host(nullptr),
120 host_len(0),
121 db_len(0),
122 status_vars_len(0),
123 q_len(0),
124 flags2_inited(false),
125 sql_mode_inited(false),
126 charset_inited(false),
127 auto_increment_increment(1),
128 auto_increment_offset(1),
129 time_zone_len(0),
130 catalog_len(0),
131 lc_time_names_number(0),
132 charset_database_number(0),
133 table_map_for_update(0),
134 explicit_defaults_ts(TERNARY_UNSET),
135 mts_accessed_dbs(OVER_MAX_DBS_IN_EVENT_MTS),
136 ddl_xid(INVALID_XID),
137 default_collation_for_utf8mb4_number(0),
138 sql_require_primary_key(0xff),
139 default_table_encryption(0xff) {
140 BAPI_ENTER("Query_event::Query_event(const char*, ...)");
141 READER_TRY_INITIALIZATION;
142 READER_ASSERT_POSITION(fde->common_header_len);
143
144 uint8_t post_header_len = fde->post_header_len[event_type - 1];
145 uint64_t end_variable_part = 0;
146 data_len = READER_CALL(available_to_read);
147 data_len = data_len - post_header_len;
148
149 READER_TRY_SET(thread_id, read<uint32_t>);
150 READER_TRY_SET(query_exec_time, read<uint32_t>);
151 READER_TRY_SET(db_len, read<uint8_t>);
152 READER_TRY_SET(error_code, read<uint16_t>);
153 /*
154 5.0 format starts here.
155 Depending on the format, we may or not have affected/warnings etc
156 The remnant post-header to be parsed has length:
157 */
158 if (post_header_len > QUERY_HEADER_MINIMAL_LEN) {
159 READER_TRY_SET(status_vars_len, read<uint16_t>);
160 /*
161 Check if status variable length is corrupt and will lead to very
162 wrong data. We could be even more strict and require data_len to
163 be even bigger, but this will suffice to catch most corruption
164 errors that can lead to a crash.
165 */
166 if (status_vars_len >
167 std::min<unsigned long>(data_len, MAX_SIZE_LOG_EVENT_STATUS))
168 READER_THROW("Invalid status vars length in Query event");
169 } else
170 /* formats before 5.0 are not supported anymore */
171 READER_THROW("Unsupported Query event format");
172
173 /*
174 We have parsed everything we know in the post header for QUERY_EVENT,
175 the rest of post header is either comes from older version MySQL or
176 dedicated to derived events (e.g. Execute_load_query...)
177 */
178 READER_CALL(go_to, fde->common_header_len + post_header_len);
179
180 data_len = data_len - status_vars_len;
181
182 /* variable-part: the status vars; only in MySQL 5.0 */
183 end_variable_part = READER_CALL(position) + status_vars_len;
184 while (READER_CALL(position) < end_variable_part) {
185 uint8_t variable_type;
186 READER_TRY_SET(variable_type, read<uint8_t>);
187 switch (variable_type) {
188 case Q_FLAGS2_CODE:
189 flags2_inited = true;
190 READER_TRY_SET(flags2, read<uint32_t>);
191 break;
192 case Q_SQL_MODE_CODE:
193 sql_mode_inited = true;
194 READER_TRY_SET(sql_mode, read<uint64_t>);
195 break;
196 case Q_CATALOG_NZ_CODE:
197 READER_TRY_SET(catalog_len, read<uint8_t>);
198 if (catalog_len) {
199 catalog = READER_CALL(ptr);
200 READER_TRY_CALL(forward, catalog_len);
201 }
202 break;
203 case Q_AUTO_INCREMENT:
204 READER_TRY_SET(auto_increment_increment, read<uint16_t>);
205 READER_TRY_SET(auto_increment_offset, read<uint16_t>);
206 break;
207 case Q_CHARSET_CODE:
208 charset_inited = true;
209 READER_TRY_CALL(memcpy<char *>, charset, 6);
210 break;
211 case Q_TIME_ZONE_CODE:
212 READER_TRY_SET(time_zone_len, read<uint8_t>);
213 if (time_zone_len) {
214 time_zone_str = READER_CALL(ptr);
215 READER_TRY_CALL(forward, time_zone_len);
216 }
217 break;
218 case Q_CATALOG_CODE: /* for 5.0.x where 0<=x<=3 masters */
219 READER_TRY_SET(catalog_len, read<uint8_t>);
220 if (catalog_len) {
221 catalog = READER_CALL(ptr);
222 READER_TRY_CALL(forward,
223 catalog_len + 1); // leap over end 0
224 }
225 break;
226 case Q_LC_TIME_NAMES_CODE:
227 READER_TRY_SET(lc_time_names_number, read<uint16_t>);
228 break;
229 case Q_CHARSET_DATABASE_CODE:
230 READER_TRY_SET(charset_database_number, read<uint16_t>);
231 break;
232 case Q_TABLE_MAP_FOR_UPDATE_CODE:
233 READER_TRY_SET(table_map_for_update, read<uint64_t>);
234 break;
235 case Q_MICROSECONDS: {
236 READER_TRY_SET(header()->when.tv_usec, read<uint32_t>, 3);
237 break;
238 }
239 case Q_INVOKER: {
240 READER_TRY_SET(user_len, read<uint8_t>);
241 if (user_len == 0) {
242 user = (const char *)"";
243 } else {
244 user = READER_CALL(ptr);
245 READER_TRY_CALL(forward, user_len);
246 }
247
248 READER_TRY_SET(host_len, read<uint8_t>);
249 if (host_len == 0) {
250 host = (const char *)"";
251 } else {
252 host = READER_CALL(ptr);
253 READER_TRY_CALL(forward, host_len);
254 }
255 break;
256 }
257 case Q_UPDATED_DB_NAMES: {
258 unsigned char i = 0;
259 #ifndef NDEBUG
260 bool is_corruption_injected = false;
261 #endif
262
263 READER_TRY_SET(mts_accessed_dbs, read<uint8_t>);
264 /*
265 Notice, the following check is positive also in case of
266 the master's MAX_DBS_IN_EVENT_MTS > the slave's one and the event
267 contains e.g the master's MAX_DBS_IN_EVENT_MTS db:s.
268 */
269 if (mts_accessed_dbs > MAX_DBS_IN_EVENT_MTS) {
270 mts_accessed_dbs = OVER_MAX_DBS_IN_EVENT_MTS;
271 break;
272 }
273
274 if (mts_accessed_dbs == 0)
275 READER_THROW("Invalid MTA accessed databases");
276
277 for (i = 0;
278 i < mts_accessed_dbs && READER_CALL(position) < end_variable_part;
279 i++) {
280 uint64_t remaining;
281 #ifndef NDEBUG
282 /*
283 This is specific to mysql test run on the server
284 for the keyword "query_log_event_mta_corrupt_db_names"
285 */
286 if (binary_log_debug::debug_query_mts_corrupt_db_names) {
287 if (mts_accessed_dbs == 2) {
288 char *pos = const_cast<char *>(READER_CALL(ptr));
289 BAPI_ASSERT(pos[sizeof("d?") - 1] == 0);
290 pos[sizeof("d?") - 1] = 'a';
291 is_corruption_injected = true;
292 }
293 }
294 #endif
295 if (READER_CALL(position) >= end_variable_part)
296 READER_THROW("Error reading MTA accessed databases");
297
298 remaining = end_variable_part - READER_CALL(position);
299 READER_TRY_CALL(strncpyz, mts_accessed_db_names[i],
300 std::min<unsigned long>(NAME_LEN, remaining),
301 NAME_LEN);
302 }
303 if (i != mts_accessed_dbs
304 #ifndef NDEBUG
305 || is_corruption_injected
306 #endif
307 )
308 READER_THROW("Invalid Q_UPDATED_DB_NAMES");
309 break;
310 }
311 case Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP: {
312 char value;
313 READER_TRY_SET(value, read<uint8_t>);
314 explicit_defaults_ts = value == 0 ? TERNARY_OFF : TERNARY_ON;
315 break;
316 }
317 case Q_DDL_LOGGED_WITH_XID:
318 /*
319 Like in Xid_log_event case, the xid value is not used on the slave
320 so the number does not really need to respect endiness.
321 */
322 READER_TRY_SET(ddl_xid, read<uint64_t>);
323 break;
324 case Q_DEFAULT_COLLATION_FOR_UTF8MB4:
325 READER_TRY_SET(default_collation_for_utf8mb4_number, read<uint16_t>);
326 break;
327 case Q_SQL_REQUIRE_PRIMARY_KEY:
328 READER_TRY_SET(sql_require_primary_key, read<uint8_t>);
329 break;
330 case Q_DEFAULT_TABLE_ENCRYPTION:
331 READER_TRY_SET(default_table_encryption, read<uint8_t>);
332 break;
333 default:
334 /* That's why you must write status vars in growing order of code */
335 READER_CALL(go_to, end_variable_part); // Break loop
336 }
337 }
338
339 /*
340 if time_zone_len or catalog_len are 0, then time_zone and catalog
341 are uninitialized at this point. shouldn't they point to the
342 zero-length null-terminated strings we allocated space for in the
343 my_alloc call above? /sven
344 */
345
346 /* A 2nd variable part; this is common to all versions */
347 db = READER_CALL(ptr);
348 READER_TRY_CALL(forward, db_len + 1);
349 q_len = data_len - db_len - 1;
350 if (q_len != READER_CALL(available_to_read))
351 READER_THROW("Invalid query length");
352 query = READER_CALL(ptr, q_len);
353
354 READER_CATCH_ERROR;
355 BAPI_VOID_RETURN;
356 }
357
358 /**
359 Layout for the data buffer is as follows
360 <pre>
361 +--------+-----------+------+------+---------+----+-------+----+
362 | catlog | time_zone | user | host | db name | \0 | Query | \0 |
363 +--------+-----------+------+------+---------+----+-------+----+
364 </pre>
365 */
366 int Query_event::fill_data_buf(Log_event_header::Byte *buf,
367 unsigned long buf_len) {
368 if (!buf) return 0;
369 /* We need to check the buffer size */
370 if (buf_len < catalog_len + 1 + time_zone_len + 1 + user_len + 1 + host_len +
371 1 + data_len)
372 return 0;
373
374 if (data_len && (data_len < db_len || data_len < q_len ||
375 data_len != (db_len + q_len + 1)))
376 return 0;
377
378 unsigned char *start = buf;
379 /*
380 Note: catalog_len is one less than "catalog.length()"
381 if Q_CATALOG flag is set
382 */
383 if (catalog_len) // If catalog is given
384 /*
385 This covers both the cases, where the catalog_nz flag is set of unset.
386 The end 0 will be a part of the string catalog in the second case,
387 hence using catalog.length() instead of catalog_len makes the flags
388 catalog_nz redundant.
389 */
390 copy_str_and_move(&start, &catalog, catalog_len);
391 if (time_zone_len > 0)
392 copy_str_and_move(&start, &time_zone_str, time_zone_len);
393 if (user_len > 0) copy_str_and_move(&start, &user, user_len);
394 if (host_len > 0) copy_str_and_move(&start, &host, host_len);
395 if (data_len) {
396 if (db_len > 0 && db) copy_str_and_move(&start, &db, db_len);
397 if (q_len > 0 && query) copy_str_and_move(&start, &query, q_len);
398 }
399 return 1;
400 }
401
402 User_var_event::User_var_event(const char *buf,
403 const Format_description_event *fde)
404 : Binary_log_event(&buf, fde) {
405 BAPI_ENTER("User_var_event::User_var_event(const char*, ...)");
406 READER_TRY_INITIALIZATION;
407 READER_ASSERT_POSITION(fde->common_header_len);
408 READER_TRY_CALL(forward, fde->post_header_len[USER_VAR_EVENT - 1]);
409
410 READER_TRY_SET(name_len, read<uint32_t>);
411 if (name_len == 0) READER_THROW("Invalid name length");
412 name = READER_CALL(strndup<const char *>, name_len);
413 READER_TRY_SET(is_null, read<uint8_t>);
414
415 flags = User_var_event::UNDEF_F; // defaults to UNDEF_F
416 if (is_null) {
417 type = STRING_RESULT;
418 /*
419 *my_charset_bin.number= 63, and my_charset_bin is defined in server
420 *so replacing it with its value.
421 */
422 charset_number = 63;
423 val_len = 0;
424 val = nullptr;
425 } else {
426 uint8_t type_tmp;
427 READER_TRY_SET(type_tmp, read<uint8_t>);
428 type = (Value_type)type_tmp;
429 switch (type) {
430 case STRING_RESULT:
431 case DECIMAL_RESULT:
432 case REAL_RESULT:
433 case INT_RESULT:
434 break;
435 default:
436 READER_THROW("Invalid type found while deserializing User_var_event");
437 }
438 READER_TRY_SET(charset_number, read<uint32_t>);
439 READER_TRY_SET(val_len, read<uint32_t>);
440 val = const_cast<char *>(READER_CALL(ptr, val_len));
441 // val[0] is precision and val[1] is scale so precision >= scale for decimal
442 if (type == DECIMAL_RESULT) {
443 if (val[0] < val[1])
444 READER_THROW(
445 "Invalid User value found while deserializing User_var_event");
446 }
447 /**
448 We need to check if this is from an old server
449 that did not pack information for flags.
450 We do this by checking if there are extra bytes
451 after the packed value. If there are we take the
452 extra byte and it's value is assumed to contain
453 the flags value.
454
455 Old events will not have this extra byte, thence,
456 we keep the flags set to UNDEF_F.
457 */
458 if (READER_CALL(available_to_read) > 0) {
459 READER_TRY_SET(flags, read<uint8_t>);
460 }
461 }
462
463 READER_CATCH_ERROR;
464 BAPI_VOID_RETURN;
465 }
466
467 User_var_event::~User_var_event() { bapi_free(const_cast<char *>(name)); }
468
469 /**
470 Constructor receives a packet from the MySQL master or the binary
471 log and decodes it to create an Intvar_event.
472 Written every time a statement uses an AUTO_INCREMENT column or the
473 LAST_INSERT_ID() function; precedes other events for the statement.
474 This is written only before a QUERY_EVENT and is not used with row-based
475 logging. An INTVAR_EVENT is written with a "subtype" in the event data part:
476
477 * INSERT_ID_EVENT indicates the value to use for an AUTO_INCREMENT column in
478 the next statement.
479
480 * LAST_INSERT_ID_EVENT indicates the value to use for the LAST_INSERT_ID()
481 function in the next statement.
482 */
483 Intvar_event::Intvar_event(const char *buf, const Format_description_event *fde)
484 : Binary_log_event(&buf, fde) {
485 BAPI_ENTER("Intvar_event::Intvar_event(const char*, ...)");
486 READER_TRY_INITIALIZATION;
487 READER_ASSERT_POSITION(fde->common_header_len);
488 READER_TRY_CALL(forward, fde->post_header_len[INTVAR_EVENT - 1]);
489
490 READER_TRY_SET(type, read<uint8_t>);
491 READER_TRY_SET(val, read<uint64_t>);
492
493 READER_CATCH_ERROR;
494 BAPI_VOID_RETURN;
495 }
496
497 Rand_event::Rand_event(const char *buf, const Format_description_event *fde)
498 : Binary_log_event(&buf, fde) {
499 BAPI_ENTER("Rand_event::Rand_event(const char*, ...)");
500 READER_TRY_INITIALIZATION;
501 READER_ASSERT_POSITION(fde->common_header_len);
502 READER_TRY_CALL(forward, fde->post_header_len[RAND_EVENT - 1]);
503
504 READER_TRY_SET(seed1, read<uint64_t>);
505 READER_TRY_SET(seed2, read<uint64_t>);
506
507 READER_CATCH_ERROR;
508 BAPI_VOID_RETURN;
509 }
510
511 #ifndef HAVE_MYSYS
512 void Query_event::print_event_info(std::ostream &info) {
513 if (memcmp(query, "BEGIN", 5) != 0 && memcmp(query, "COMMIT", 6) != 0) {
514 info << "use `" << db << "`; ";
515 }
516 info << query;
517 }
518
519 void Query_event::print_long_info(std::ostream &info) {
520 info << "Timestamp: " << header()->when.tv_sec;
521 info << "\tThread id: " << (int)thread_id;
522 info << "\tExec time: " << (int)query_exec_time;
523 info << "\nDatabase: " << db;
524 info << "\tQuery: ";
525 this->print_event_info(info);
526 }
527
528 void User_var_event::print_event_info(std::ostream &info) {
529 info << "@`" << name << "`=";
530 if (type == STRING_RESULT)
531 info << val;
532 else
533 info << "<Binary encoded value>";
534 // TODO: value is binary encoded, requires decoding
535 }
536
537 void User_var_event::print_long_info(std::ostream &info) {
538 info << "Timestamp: " << header()->when.tv_sec;
539 info << "\tType: " << get_value_type_string(type);
540 info << "\n";
541 this->print_event_info(info);
542 }
543
544 void Intvar_event::print_event_info(std::ostream &info) {
545 info << get_var_type_string();
546 info << "\tValue: " << val;
547 }
548
549 void Intvar_event::print_long_info(std::ostream &info) {
550 info << "Timestamp: " << header()->when.tv_sec;
551 info << "\t";
552 this->print_event_info(info);
553 }
554
555 void Rand_event::print_event_info(std::ostream &info) {
556 info << " SEED1 is " << seed1;
557 info << " SEED2 is " << seed2;
558 }
559 void Rand_event::print_long_info(std::ostream &info) {
560 info << "Timestamp: " << header()->when.tv_sec;
561 info << "\t";
562 this->print_event_info(info);
563 }
564
565 #endif
566 } // namespace binary_log