Commit graph

76,303 commits

Author SHA1 Message Date
Nikita Malyavin
ceb64ee4d1 MDEV-35936 More ASAN errors in check_key_referential_integrity
Given that key part lengths may differ on two sides of foreign key relation,
it's important to use a child (referenced) table's keys during the lengths
calculation.
2025-01-26 17:20:50 +01:00
Nikita Malyavin
594c0c55d3 key_copy: fix key_part length calculation for varchars/blobs 2025-01-25 00:44:43 +01:00
Nikita Malyavin
0a7ef33510 key_copy: remove redundant (and wrong) assertion 2025-01-25 00:33:17 +01:00
Nikita Malyavin
0b9beb476a MDEV-35908 Unexpected error, crash, MSAN errors, assertion failures upon CHECK
Two different problems are fixed here:
1. A bogus inet6 -> time match turned to be possible:
inet6 size is 20, while time size is 3, so the bigger is pointing to a smaller
When innodb makes a referencial check, it compares to the min(len1, len2), i.e.
in this case 3 bytes, and then checks that the rest is filled with 0x20,
that is, space-padded.

Probably, this is just a bogus hole in semantic checks, but now this reference
is possible. Yet, implementing correct checks for such cases will require a lot
of effort, so let's just skip such keys.

2. char(15) -> varchar(15) record check caused use-of-uninitialized.

This is a tough one. A from-table and a to-table store data in different
formats, both in and in key, which was a problem for key_copy.

The solution is to use save_in_field to first convert the data to the format of
the to-table's record, and then use a to-table's field to store the string in
the key.

Note that varchar(15) -> char(15) even doesn't work well in innodb, and
insertion is impossible, unless foreign_key_checks is off. However, cascade
deletions do work, though, again, with a quirk (see the tests).
2025-01-24 04:36:08 +01:00
Nikita Malyavin
b29b30e75e MDEV-35737 MSAN errors in check_table_referential_checks_needed upon LOAD INDEX
LOAD INDEX doesn't initialize Lex->check_opt and mysql_preload_keys doesn't use
it. However, Check_table_prelocking_strategy did, and it's always used in
opening table.

One solution could be using a different prelocking strategy for
mysql_preload_keys.

Let's instead just pass a correct check_opt to Check_table_prelocking_strategy.

Base task: MDEV-34309
2025-01-14 22:49:42 +01:00
Nikita Malyavin
d4707d3206 MDEV-35731 Assertion `(mem_root->flags & 4) == 0' failed upon 2nd exec of CHECK
... TABLE with FK

The normal order of things in prepared statement is:
1. open tables during prepare
2. use them during all the executions

Currently, tables are opened in the body of CHECK TABLE's execution part.
This is motivated by the nature of this statement: we have to run the checks for
all the tables specified in the list, event if some of them fail to open.

Rewriting it as in normal order is possible, but is out of scope of this task.

That is, we shouldn't activate stmt_arena when constructing the referential
tables list.

It's not enough, since the next statement execution will try to reuse all the
TABLE_LIST's in lex->query_list, but the referential tables are allocated on the
normal query arena, so they should be cleaned up from that list.

Given how specific the open_only_one_table work is, we only have to re-link
table lists as they were before the tables are opened. It's enough to just
assign next_local to next_global for each table specified by
user, i.e. in lex->first_select_lex().

lex->query_tables_last and other lex fields are already maintained by that
function.

Base task: MDEV-34309
2025-01-14 22:41:43 +01:00
Nikita Malyavin
2af2808f57 MDEV-35730 Server crashes in Field::ptr_in_record
In BIT -> TIME reference, BIT length is 1, and TIME length is 4.
It's likely a quirk, that such relation is possible, since 0 -> 0 is not a valid
 reference.

However, some parts of key_copy should be fixed:
1. Make sure that key_length is deduced according to to_key_info key_parts.
2. Mind the case when to_key_part is NOT NULL, but key_part is NULL.
3. Use correct length in field->get_key_image call.

Also fix the double run of checks of self-referencing keys.

Base task: MDEV-34309
2025-01-14 02:21:42 +01:00
Nikita Malyavin
f0a0e0bf92 MDEV-35733 ASAN errors in row_mysql_store_col_in_innobase_format
Fix keymap size

Base task: MDEV-34309
2025-01-13 04:14:41 +01:00
Nikita Malyavin
01b0c6c996 MDEV-34309 [2/2] CHECK TABLE: implement referential integrity check 2024-12-17 00:17:36 +01:00
Nikita Malyavin
1115f790dd MDEV-34309 [1/2] prelock referentially related tables for CHECK TABLE 2024-12-17 00:17:36 +01:00
Nikita Malyavin
5d7f6f7e6e cleanup: replace thd->calloc<T>(N) with operator new T[N] {}
### Preamble

C++ initializes objects in three stages:
1. Optionally, zero-initializes the object fields.
2. Member-initializes fields that are explicitly set.
3. If applicable, calls a constructor.

The following expressions:
x = new T[N];
x = new T;
T x;

only member-initialize and call a default constructor. Stage 1 is skipped,
because () braces are omitted.

This is known as default-initialization.

Apart from Stage 2, the following:
x = new T[N]();
x = new T();
const T &x = T();

Is known as value-initialization:
If no default constructor is present, infer zero-initialization.
Otherwise, the default constructor is called.

Note that it's not possible to write `T x();`, as it is ambiguous to a function
call.

Since C++11, it's also possible to zero initialize objects with '{}' braces:
x = new T[N]{};
x = new T{};
T x{};

This also both zero-initializes and calls a default constructor.

There is no much difference in between empty-braced () and {}. Both call a
default constructor or initializer-list constructor, when available. Having both
constructors is ambiguous.

Scalars (i.e. fundamental data types) and POD types have no constructor.
Therefore, stage 2 for them is skipped.

Other than that, there is no much difference in the result

Exambles:
new char[123] -- would return an uninitialized array of char.
new char[123]() -- forces zero-initialization
new char[123]{} -- forces zero-initialization
new char[123]{123} -- forces zero-initialization, and also value-initializes
 the first element to 123

struct A {
  int x = 0xaf;
  int y;
}

All of the following:
A a;
A *a = new A;
A *a = new A[123];

Causes member A::x be initialized to 0xaf, since it happens at
value-initialization stage. A::y is left uninitialized.

A *a = new A[123] {};
and other similars result in {.x=0xaf, .y=0}.

### In this commit

Change all the calls to thd->calloc() to new(thd) T[N]{}, or new(thd) T{}.

POD types will be zero-initialized, so a special attention should be put to
classes with default constructors.

Among all uses, two cases of interest were found:
1. TABLE_LIST: has a default constructor TABLE_LIST() = default. This infers
zero-initialization behavior (i.e. as if there's no constructor).
2. USER_AUTH: has a default constructor, that initializes all fields. Strings
are initialized to "", which is fine.
3. Security_context: had a custom default constructor, initializing only two
fields. It was removed, and fields are made member-initialized.
2024-12-17 00:17:36 +01:00
Nikita Malyavin
be0e61d5ce cleanup: remove virtual QUICK_RANGE_SELECT::clone
QUICK_RANGE_SELECT::clone is never used, and besides it does not inherit
Sql_alloc, so the usage of `new` without agruments would be leak-prone.

So, remove it.
2024-12-17 00:17:36 +01:00
Nikita Malyavin
a7c1ef9776 cleanup: further replace thd->alloc() with operator new
Remove THD:alloc() completely.

THD::calloc will still stay. It's still possible to use thd_alloc() as a plain C
replacement.

my_bitmap.h: add my_bitmap_array_size(bits) to count in my_bitmap_map, which is
ulonglong

All the bitmap arrays are now allocated with new my_bitmap_map[size]. This is
bigger than it was before, but essentially aligns the allocated array to
my_bitmap_map size, which was violated before.

sql_select: replace thd->alloc() with thd_alloc()

table_status_by_host_context: add default

sql_parse.cc: add_proc_to_list: add struct Order_Item

Made as a part of MDEV-34309
2024-12-13 19:56:30 +01:00
Nikita Malyavin
019f40218e cleanup: add operator new(size_t, const THD*) for single-object allocations
Made as a part of MDEV-34309
2024-12-13 19:53:30 +01:00
Nikita Malyavin
8260743ada cleanup: add operator new[](size_t, const THD*)
Note that List and QUICK_RANGE inherit Sql_alloc, so they use new (mem_root).

sql_select.cc: remove rollup_fields->empty() since it's now done by a List
constructor.

Made as a part of MDEV-34309
2024-12-13 19:48:36 +01:00
Nikita Malyavin
9b4ac2539a cleanup: add new(thd) to Sql_alloc and Item
Sql_alloc: add operator new(size_t, const THD *) noexcept. Also change throw()
to noexcept in other Sql_alloc operators.

Item: add operator new(size_t, const THD *).

The implementations are in the bottom of item.cc and sql_class.cc.

Made as a part of MDEV-34309
2024-12-13 16:11:52 +01:00
Nikita Malyavin
aa3000fe58 FOREIGN_KEY_INFO: Store Lex_* strings without extra pointer indirection.
Made as a part of MDEV-34309
2024-12-09 23:55:19 +01:00
Nikita Malyavin
12ef85c4ac Fix deallocation of Sql_cmd_dml::result
Made as a part of MDEV-34309
2024-12-09 23:55:19 +01:00
Marko Mäkelä
f0961301c8 Merge 11.7 into main 2024-12-02 17:55:44 +02:00
Marko Mäkelä
33907f9ec6 Merge 11.4 into 11.7 2024-12-02 17:51:17 +02:00
Marko Mäkelä
2719cc4925 Merge 10.11 into 11.4 2024-12-02 11:35:34 +02:00
Marko Mäkelä
4d9548876e MDEV-31340 fixup: clang++-20 -Wdeprecated-literal-operator 2024-12-02 10:44:06 +02:00
Marko Mäkelä
3d23adb766 Merge 10.6 into 10.11 2024-11-29 13:43:17 +02:00
Marko Mäkelä
7d4077cc11 Merge 10.5 into 10.6 2024-11-29 12:37:46 +02:00
Daniele Sciascia
e821c9fa7c MDEV-35281 SR transaction crashes with innodb_snapshot_isolation
Ignore snapshot isolation conflict during fragment removal, before
streaming transaction commits. This happens when a streaming
transaction creates a read view that precedes the INSERTion of
fragments into the streaming_log table. Fragments are INSERTed
using a different transaction. These fragment are then removed
as part of COMMIT of the streaming transaction. This fragment
removal operation could fail when the fragments were not part
the transaction's read view, thus violating snapshot isolation.
2024-11-29 08:06:32 +01:00
Alexander Barkov
fdb6db6b47 MDEV-29462 ASAN: heap-use-after-free in Binary_string::copy on DO CONVERT
Item_char_typecast::val_str_generic() uses Item::str_value as a buffer.
Item::val_str_ascii() also used Item::str_value as a buffer.
As a result, str_value tried to copy to itself.

Fixing val_str_ascii() to use a local buffer instead of str_value.
2024-11-28 16:34:32 +04:00
Ivan Prisyazhnyy
f39a61505f MDEV-33075 [backport/2f5174e556] use more robust self-pipe to wake up poll() in break_connect_loop()
Backport of 2f5174e556:
MDEV-33075 Resolve server shutdown issues on macOS, Solaris, and FreeBSD.

This commit addresses multiple server shutdown problems observed on macOS,
Solaris, and FreeBSD:

1. Corrected a non-portable assumption where socket shutdown was expected
to wake up poll() with listening sockets in the main thread.

Use more robust self-pipe to wake up poll() by writing to the pipe's write
end.

Signed-off-by: Ivan Prisyazhnyy <john.koepi@gmail.com>
2024-11-27 14:59:50 +02:00
Ivan Prisyazhnyy
c4cadb768f MDEV-33075 [backport/2f5174e556] fix rnd crash on macOS from pthread_kill(signal_handler)
Backport of 2f5174e556:
MDEV-33075 Resolve server shutdown issues on macOS, Solaris, and FreeBSD.

This commit addresses multiple server shutdown problems observed on macOS,
Solaris, and FreeBSD:

2. Fixed a random crash on macOS from pthread_kill(signal_handler)
when the signal_handler was detached and the thread had already exited.

Use more robust `kill(getpid(), SIGTERM)` to wake up the signal handler
thread.

Additionally, the shutdown code underwent light refactoring
for better readability and maintainability:

- Modified `break_connect_loop()` to no longer wait for the main thread,
  aligning behavior with Windows (since 10.4).
2024-11-27 14:59:50 +02:00
Ivan Prisyazhnyy
8214707699 MDEV-33075 [backport/2f5174e556] fix signal handler thread exit on abort
Backport of 2f5174e556:
MDEV-33075 Resolve server shutdown issues on macOS, Solaris, and FreeBSD.

This commit addresses multiple server shutdown problems observed on macOS,
Solaris, and FreeBSD:

3. Made sure, that signal handler thread always exits once `abort_loop` is
set, and also calls `my_thread_end()` and clears `signal_thread_in_use`
when exiting.

This fixes warning "1 thread did not exit"  by `my_global_thread_end()`
seen on FreeBSD/macOS when the process is terminated via signal.

Additionally, the shutdown code underwent light refactoring
for better readability and maintainability:

- Removed dead code related to the unused `USE_ONE_SIGNAL_HAND`
  preprocessor constant.

Signed-off-by: Ivan Prisyazhnyy <john.koepi@gmail.com>
2024-11-27 14:59:50 +02:00
Ivan Prisyazhnyy
490274e850 MDEV-33075 [backport/2f5174e556] eliminated support for #ifndef HAVE_POLL
Backport of 2f5174e556:
MDEV-33075 Resolve server shutdown issues on macOS, Solaris, and FreeBSD.

Eliminated support for `#ifndef HAVE_POLL` in `handle_connection_sockets`
This code is also dead, since 10.4

Signed-off-by: Ivan Prisyazhnyy <john.koepi@gmail.com>
2024-11-27 14:59:50 +02:00
Jan Lindström
f5aed74573 MDEV-35486 : MDEV-33997 test failed
Problem was that at wsrep_to_isolation_end saved_lock_wait_timeout
variable was set to thd->variables.lock_wait_timeout when RSU
is used and variable value was 0 leading sporadic lock wait timeout
errors. Fixed by removing incorrect variable set.

Signed-off-by: Julius Goryavsky <julius.goryavsky@mariadb.com>
2024-11-27 13:00:08 +01:00
Alexander Barkov
8b057889d7 MDEV-34981 Functions missing from INFORMATION_SCHEMA.SQL_FUNCTIONS
Plugin functions are now displayed in I_S.SQL_FUNCTIONS
2024-11-27 14:24:52 +04:00
Ahmed Ibrahim
a35f744d78 MDEV-31736: format_bytes implementation 2024-11-26 13:10:01 +04:00
Alexander Barkov
425d2521ec MDEV-33472 Assertion `0' failed in Item_row::illegal_method_call on CREATE EVENT
Do not accept rows as event parameters.
2024-11-25 18:29:13 +04:00
Alexander Barkov
2e404c9850 MDEV-21029 Incorrect result for expression with the <=> operator and IS NULL
Item_func_equal erroneously derived is_null() from the parent class.
Overriding it to return false because <=> can never return NULL.
2024-11-25 10:37:02 +04:00
Brandon Nesterenko
78d7bb1d27 MDEV-34348: Miscellaneous fixes
Partial commit of the greater MDEV-34348 scope.
MDEV-34348: MariaDB is violating clang-16 -Wcast-function-type-strict

Various additional fixes, each too small to put into
their own commit.

Reviewed By:
============
Marko Mäkelä <marko.makela@mariadb.com>
2024-11-23 08:14:23 -07:00
Brandon Nesterenko
3c785499da MDEV-34348: Fix casts relating to tree_walk_action
Partial commit of the greater MDEV-34348 scope.
MDEV-34348: MariaDB is violating clang-16 -Wcast-function-type-strict

Reviewed By:
============
Marko Mäkelä <marko.makela@mariadb.com>
2024-11-23 08:14:23 -07:00
Brandon Nesterenko
5432fa802b MDEV-34348: Fix casts in sql_acl
Partial commit of the greater MDEV-34348 scope.
MDEV-34348: MariaDB is violating clang-16 -Wcast-function-type-strict

Reviewed By:
============
Marko Mäkelä <marko.makela@mariadb.com>
2024-11-23 08:14:23 -07:00
Brandon Nesterenko
7a8eb26bda MDEV-34348: Fix casting related to plugins
Partial commit of the greater MDEV-34348 scope.
MDEV-34348: MariaDB is violating clang-16 -Wcast-function-type-strict

Reviewed By:
============
Marko Mäkelä <marko.makela@mariadb.com>
2024-11-23 08:14:23 -07:00
Brandon Nesterenko
840fe316d4 MDEV-34348: my_hash_get_key fixes
Partial commit of the greater MDEV-34348 scope.
MDEV-34348: MariaDB is violating clang-16 -Wcast-function-type-strict

Change the type of my_hash_get_key to:
 1) Return const
 2) Change the context parameter to be const void*

Also fix casting in hash adjacent areas.

Reviewed By:
============
Marko Mäkelä <marko.makela@mariadb.com>
2024-11-23 08:14:22 -07:00
Brandon Nesterenko
dbfee9fc2b MDEV-34348: Consolidate cmp function declarations
Partial commit of the greater MDEV-34348 scope.
MDEV-34348: MariaDB is violating clang-16 -Wcast-function-type-strict

The functions queue_compare, qsort2_cmp, and qsort_cmp2
all had similar interfaces, and were used interchangable
and unsafely cast to one another.

This patch consolidates the functions all into the
qsort_cmp2 interface.

Reviewed By:
============
Marko Mäkelä <marko.makela@mariadb.com>
2024-11-23 08:14:22 -07:00
Alexander Barkov
95df7ea33a MDEV-31881 ASAN: unknown-crash in check_ulonglong (sql/sql_analyse.cc) on SELECT ... FROM ... PROCEDURE ANALYSE()
Fixing a wrong condition which made the code read 1 byte behind the buffer.
2024-11-22 14:54:08 +04:00
Alexander Barkov
39f1f30f68 MDEV-23687 Assertion `is_valid_value_slow()' failed in Datetime::Datetime upon EXTRACT under mode ZERO_DATE_TIME_CAST
Item_{date|datetime}_typecase::get_date() erroneously passed the
TIME_INTERVAL_DAY flag from the caller to args[0] which made
CAST('100000:00:00' AS DATETIME) parse '100000:00:00' as TIME
rather that DATETIME.
Suppressing this flag.
2024-11-22 12:15:03 +04:00
ParadoxV5
cf2d49ddcf Extract some of #3360 fixes to 10.5.x
That PR uncovered countless issues on `my_snprintf` uses.
This commit backports a squashed subset of their fixes.
2024-11-21 22:43:56 +11:00
Monty
93fb364cd9 Removed not used ha_drop_table()
This was done after changing call in sql_select.cc from
ha_drop_table() to drop_table(), like in 11.5
2024-11-20 09:59:43 +02:00
Monty
0de9e40f4b Added status variable "stack_usable" to be able to check stack usage 2024-11-19 19:02:45 +02:00
Alexander Barkov
ae0cbfe934 MDEV-28001 greatest/least with bigint unsigned maxium has unexpected results compared to 0
LEAST() and GREATEST() erroneously calcucalted the result as signed
for BIGINT UNSIGNED arguments.

Adding a new method for unsigned arguments:
  Item_func_min_max::val_uint_native()
2024-11-19 14:26:39 +04:00
Alexander Barkov
74184074a0 MDEV-28652 SUBSTRING(str,pos,len) returns incorrect result in view (returns an empty string)
Item_func_substr::fix_length_and_dec() incorrecltly calculated its max_length
to 0 when a huge number was passed as the third argument:
  substring('hello', 1, 4294967295)
Fixing this.
2024-11-19 12:35:00 +04:00
Alexander Barkov
09fe74c7fd MDEV-25174 DOUBLE columns do not accept large hex hybrids
Limit only signed integer fields fields to LONGLONG_MAX.
Double and decimal fields do not need this limit, as they
can store integers up to ULONGLONG_MAX without problems.
2024-11-19 11:50:12 +04:00
Alexander Barkov
70dbd63e02 MDEV-24337 Server crash in DTCollation::set_repertoire_from_charset
The loop in Item_func_in::get_func_mm_tree incorrectly used array->count
in the loop. Fixing it to array->used_count.
2024-11-19 10:57:14 +04:00