Skip to content

Commit 6bf5a3b

Browse files
abarkovvuvova
authored andcommitted
MDEV-26785 Hyphens inside the value of uuid datatype
1 parent 4300f50 commit 6bf5a3b

File tree

3 files changed

+102
-16
lines changed

3 files changed

+102
-16
lines changed

plugin/type_uuid/mysql-test/type_uuid/type_uuid.result

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,3 +3120,27 @@ SELECT MIN(a), MAX(a) FROM t1 GROUP BY id;
31203120
MIN(a) MAX(a)
31213121
00000000-0000-0000-0000-000000000fff 00000000-0000-0000-0000-000000008888
31223122
DROP TABLE t1;
3123+
#
3124+
# MDEV-26785 Hyphens inside the value of uuid datatype
3125+
#
3126+
CREATE TABLE t1 (a UUID);
3127+
INSERT INTO t1 VALUES ('0000000000000000000000000000000'/*31 digits*/);
3128+
ERROR 22007: Incorrect uuid value: '0000000000000000000000000000000' for column `test`.`t1`.`a` at row 1
3129+
INSERT INTO t1 VALUES ('000000000000000000000000000000000'/*33 digits*/);
3130+
ERROR 22007: Incorrect uuid value: '000000000000000000000000000000000' for column `test`.`t1`.`a` at row 1
3131+
INSERT INTO t1 VALUES ('-00000000000000000000000000000000'/*leading hyphen*/);
3132+
ERROR 22007: Incorrect uuid value: '-00000000000000000000000000000000' for column `test`.`t1`.`a` at row 1
3133+
INSERT INTO t1 VALUES ('-00000000000000000000000000000000-'/*trailing hyphen*/);
3134+
ERROR 22007: Incorrect uuid value: '-00000000000000000000000000000000-' for column `test`.`t1`.`a` at row 1
3135+
INSERT INTO t1 VALUES ('00000000000000000000000000000000');
3136+
INSERT INTO t1 VALUES ('0-0000000000000000000000000000011');
3137+
INSERT INTO t1 VALUES ('0--0000000000000000000000000000012');
3138+
INSERT INTO t1 VALUES ('0---0000000000000000000000000000013');
3139+
INSERT INTO t1 VALUES ('0----0000000000000000000000000000014');
3140+
INSERT INTO t1 VALUES ('00-000000000000000000000000000021');
3141+
INSERT INTO t1 VALUES ('00--000000000000000000000000000022');
3142+
INSERT INTO t1 VALUES ('00---000000000000000000000000000023');
3143+
INSERT INTO t1 VALUES ('00----000000000000000000000000000024');
3144+
INSERT INTO t1 VALUES ('5796dac11a1c11--------------ecab4ef859-713e4be4');
3145+
INSERT INTO t1 VALUES ('5796dac11a1c11---------------ecab4ef859-713e4be4');
3146+
DROP TABLE t1;

plugin/type_uuid/mysql-test/type_uuid/type_uuid.test

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,3 +1617,32 @@ INSERT INTO t1 VALUES
16171617
(1, '00000000-0000-0000-0000-000000008888');
16181618
SELECT MIN(a), MAX(a) FROM t1 GROUP BY id;
16191619
DROP TABLE t1;
1620+
1621+
1622+
--echo #
1623+
--echo # MDEV-26785 Hyphens inside the value of uuid datatype
1624+
--echo #
1625+
1626+
CREATE TABLE t1 (a UUID);
1627+
--error ER_TRUNCATED_WRONG_VALUE
1628+
INSERT INTO t1 VALUES ('0000000000000000000000000000000'/*31 digits*/);
1629+
--error ER_TRUNCATED_WRONG_VALUE
1630+
INSERT INTO t1 VALUES ('000000000000000000000000000000000'/*33 digits*/);
1631+
--error ER_TRUNCATED_WRONG_VALUE
1632+
INSERT INTO t1 VALUES ('-00000000000000000000000000000000'/*leading hyphen*/);
1633+
--error ER_TRUNCATED_WRONG_VALUE
1634+
INSERT INTO t1 VALUES ('-00000000000000000000000000000000-'/*trailing hyphen*/);
1635+
1636+
INSERT INTO t1 VALUES ('00000000000000000000000000000000');
1637+
INSERT INTO t1 VALUES ('0-0000000000000000000000000000011');
1638+
INSERT INTO t1 VALUES ('0--0000000000000000000000000000012');
1639+
INSERT INTO t1 VALUES ('0---0000000000000000000000000000013');
1640+
INSERT INTO t1 VALUES ('0----0000000000000000000000000000014');
1641+
INSERT INTO t1 VALUES ('00-000000000000000000000000000021');
1642+
INSERT INTO t1 VALUES ('00--000000000000000000000000000022');
1643+
INSERT INTO t1 VALUES ('00---000000000000000000000000000023');
1644+
INSERT INTO t1 VALUES ('00----000000000000000000000000000024');
1645+
1646+
INSERT INTO t1 VALUES ('5796dac11a1c11--------------ecab4ef859-713e4be4');
1647+
INSERT INTO t1 VALUES ('5796dac11a1c11---------------ecab4ef859-713e4be4');
1648+
DROP TABLE t1;

plugin/type_uuid/sql_type_uuid.cc

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,61 @@ static bool get_digit(char ch, uint *val)
4242
}
4343

4444

45-
bool UUID::ascii_to_fbt(const char *str, size_t str_length)
45+
static bool get_digit(uint *val, const char *str, const char *end)
4646
{
47-
if (str_length < 32 || str_length > 3 * binary_length() - 1)
47+
if (str >= end)
4848
return true;
49+
return get_digit(*str, val);
50+
}
51+
4952

50-
uint oidx= 0;
51-
for (const char *s= str; s < str + str_length; )
53+
static size_t skip_hyphens(const char *str, const char *end)
54+
{
55+
const char *str0= str;
56+
for ( ; str < end; str++)
5257
{
53-
if (oidx >= binary_length())
54-
goto err;
55-
if (*s == '-')
56-
{
57-
if (s == str)
58-
goto err;
59-
s++;
60-
continue;
61-
}
62-
uint hi, lo;
63-
if (get_digit(*s++, &hi) || get_digit(*s++, &lo))
58+
if (str[0] != '-')
59+
break;
60+
}
61+
return str - str0;
62+
}
63+
64+
65+
static const char *get_two_digits(char *val, const char *str, const char *end)
66+
{
67+
uint hi, lo;
68+
if (get_digit(&hi, str++, end))
69+
return NULL;
70+
str+= skip_hyphens(str, end);
71+
if (get_digit(&lo, str++, end))
72+
return NULL;
73+
*val= (char) ((hi << 4) + lo);
74+
return str;
75+
}
76+
77+
78+
bool UUID::ascii_to_fbt(const char *str, size_t str_length)
79+
{
80+
const char *end= str + str_length;
81+
/*
82+
The format understood:
83+
- Hyphen is not allowed on the first and the last position.
84+
- Otherwise, hyphens are allowed on any (odd and even) position,
85+
with any amount.
86+
*/
87+
if (str_length < 32)
88+
goto err;
89+
90+
for (uint oidx= 0; oidx < binary_length(); oidx++)
91+
{
92+
if (!(str= get_two_digits(&m_buffer[oidx], str, end)))
6493
goto err;
65-
m_buffer[oidx++]= (char) ((hi << 4) + lo);
94+
// Allow hypheps after two digits, but not after the last digit
95+
if (oidx + 1 < binary_length())
96+
str+= skip_hyphens(str, end);
6697
}
98+
if (str < end)
99+
goto err; // Some input left
67100
return false;
68101
err:
69102
bzero(m_buffer, sizeof(m_buffer));

0 commit comments

Comments
 (0)