Repository navigation
MDEV-41186 Fix stack-buffer-overflow in make_unique_constraint_name - #5749
Conversation
|
|
|
The truncation budget in size_t avail= sizeof(buff) - (1 + MY_INT32_NUM_DECIMAL_DIGITS + 1);
end= buff + Well_formed_prefix(system_charset_info, buff,
MY_MIN(size_t(end - buff), avail))
.length();
*end++= '_';
I reproduced this by building the branch and running it live: CREATE TABLE t2 (s DATE NOT NULL, e DATE NOT NULL,
PERIOD FOR xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (s, e),
CONSTRAINT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CHECK (s < e));
ALTER TABLE t2 DROP CONSTRAINT `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_1`;That error proves a genuine 66-character constraint name ( This is worth fixing in this same patch, since Well_formed_prefix(CHARSET_INFO *cs, const char *str, size_t length, size_t nchars)Suggested fix — cap by characters as well as bytes: size_t avail= sizeof(buff) - (1 + MY_INT32_NUM_DECIMAL_DIGITS + 1);
size_t avail_chars= NAME_CHAR_LEN - (1 + MY_INT32_NUM_DECIMAL_DIGITS);
end= buff + Well_formed_prefix(system_charset_info, buff,
MY_MIN(size_t(end - buff), avail),
avail_chars)
.length();
*end++= '_';Test case that fails against the current patch (add alongside the existing multi-byte one in --echo # 64-character all-ASCII period name, same collision as above
CREATE TABLE t2 (s DATE NOT NULL, e DATE NOT NULL,
PERIOD FOR xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (s, e),
CONSTRAINT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CHECK (s < e));
--error ER_PERIOD_CONSTRAINT_DROP
ALTER TABLE t2 DROP CONSTRAINT `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_1`;
DROP TABLE t2; |
vaintroub
left a comment
There was a problem hiding this comment.
Claude found a bug that can produce duplicate constraint names, using just ASCII long names, as in comment.
Could you please fix it.
Otherwise it looks good.
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
Suffixed names can still exceed the 64-character identifier limit for ASCII or shorter multibyte PERIOD names.
Review effort: Lite
Findings: None
What changed in this PR
Fixes a stack-buffer overflow when generating unique implicit CHECK constraint names for maximum-length multibyte PERIOD identifiers.
Changes:
- Truncates names at character boundaries when suffixes are required.
- Adds regression coverage for collision and non-collision cases.
| File | Changes |
|---|---|
sql/sql_table.cc |
Adjusts unique constraint name generation. |
mysql-test/suite/period/t/create.test |
Adds regression scenarios. |
mysql-test/suite/period/r/create.result |
Records expected results. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
ee1326a to
f33dde1
Compare
|
Approved — this fixes the byte-vs-character gap correctly, and I rebuilt and ran One small cleanup suggestion on the new ASCII test case: the |
vaintroub
left a comment
There was a problem hiding this comment.
Approved, but with suggestion to drop failing DROP CONSTRAINT from test now, since it was only necessary to show ambiguity previously.
A multi-byte PERIOD name at the 64-character limit (192 bytes) filled the name buffer exactly, leaving no room for the '_N' suffix appended when generating a unique name for the implicit CHECK constraint. Truncate on a character boundary, but only once a suffix is actually needed (mirroring make_unique_key_name), so a non-colliding name is never altered. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
f33dde1 to
fede302
Compare
A multi-byte PERIOD name at the 64-character limit (192 bytes) filled the name buffer exactly, leaving no room for the '_N' suffix appended when generating a unique name for the implicit CHECK constraint.
Truncate on a character boundary, but only once a suffix is actually needed (mirroring make_unique_key_name), so a non-colliding name is never altered.