Skip to content

MDEV-41179 MAX_KEY_PARTS ranges in select causing SEGV - #5782

Open
mariadb-RexJohnston wants to merge 1 commit into
10.11from
10.11-MDEV-41179
Open

mariadb-RexJohnston wants to merge 1 commit into
10.11from
10.11-MDEV-41179

Conversation

@mariadb-RexJohnston

Copy link
Copy Markdown
Member

When a select containing 32 ranges is made on a table containing a compound key with 32 parts, the range optimizer can run off the end of a stack variable, invalidly overwriting subsequent stack variables.

In the struct st_sel_arg_range_seq, we have an array
RANGE_SEQ_ENTRY stack[MAX_REF_PARTS];

MAX_REF_PARTS is 32.

check_quick_select
/ sel_arg_range_seq_init
initialises stack[0] as NOT a key part
/ sel_arg_range_seq_next
iterates through the key parts, adding key part n to stack[n+1]

key part #32 gets referenced by step_down_to(), setting seq->i off the end of the array.

The above change has exposed an issue with key length calculation on MS Windows. The field is a ulong and with all 32 key parts wanted, we calculate this as (1UL << 32) - 1. The first part of the calculation overflows the field. The result is undefined in C++.

Fix: change key_part_map to 64 bits long, so it is the same on Linux
and MS Windows.

When a select containing 32 ranges is made on a table containing a
compound key with 32 parts, the range optimizer can run off the end
of a stack variable, invalidly overwriting subsequent stack variables.

In the struct st_sel_arg_range_seq, we have an array
  RANGE_SEQ_ENTRY stack[MAX_REF_PARTS];

MAX_REF_PARTS is 32.

check_quick_select
  / sel_arg_range_seq_init
    initialises stack[0] as NOT a key part
  / sel_arg_range_seq_next
    iterates through the key parts, adding key part n to stack[n+1]

key part #32 gets referenced by step_down_to(), setting seq->i off the
end of the array.

The above change has exposed an issue with key length calculation
on MS Windows.  The field is a ulong and with all 32 key parts
wanted, we calculate this as (1UL << 32) - 1.  The first part of
the calculation overflows the field.  The result is undefined in C++.

Fix:  change key_part_map to 64 bits long, so it is the same on Linux
 and MS Windows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants