Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 84aa797

Browse files
committed
Allow uuid_in() to parse a wider variety of variant input formats for the UUID
data type. This patch takes the approach of allowing an optional hyphen after each group of four hex digits. Author: Robert Haas <robertmhaas@gmail.com>
1 parent 48cbe59 commit 84aa797

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

doc/src/sgml/datatype.sgml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.230 2008/10/14 17:12:32 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.231 2008/11/03 22:14:40 petere Exp $ -->
22

33
<chapter id="datatype">
44
<title id="datatype-title">Data Types</title>
@@ -3550,11 +3550,14 @@ a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
35503550
<productname>PostgreSQL</productname> also accepts the following
35513551
alternative forms for input:
35523552
use of upper-case digits, the standard format surrounded by
3553-
braces, and omitting the hyphens. Examples are:
3553+
braces, omitting some or all hyphens, adding a hyphen after any
3554+
group of four digits. Examples are:
35543555
<programlisting>
35553556
A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11
35563557
{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}
35573558
a0eebc999c0b4ef8bb6d6bb9bd380a11
3559+
a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11
3560+
{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}
35583561
</programlisting>
35593562
Output is always in the standard form.
35603563
</para>

src/backend/utils/adt/uuid.c

+26-35
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 2007-2008, PostgreSQL Global Development Group
77
*
88
* IDENTIFICATION
9-
* $PostgreSQL: pgsql/src/backend/utils/adt/uuid.c,v 1.7 2008/01/01 20:31:21 tgl Exp $
9+
* $PostgreSQL: pgsql/src/backend/utils/adt/uuid.c,v 1.8 2008/11/03 22:14:40 petere Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -74,60 +74,51 @@ uuid_out(PG_FUNCTION_ARGS)
7474
}
7575

7676
/*
77-
* We allow UUIDs in three input formats: 8x-4x-4x-4x-12x,
78-
* {8x-4x-4x-4x-12x}, and 32x, where "nx" means n hexadecimal digits
79-
* (only the first format is used for output). We convert the first
80-
* two formats into the latter format before further processing.
77+
* We allow UUIDs as a series of 32 hexadecimal digits with an optional dash
78+
* after each group of 4 hexadecimal digits, and optionally surrounded by {}.
79+
* (The canonical format 8x-4x-4x-4x-12x, where "nx" means n hexadecimal
80+
* digits, is the only one used for output.)
8181
*/
8282
static void
8383
string_to_uuid(const char *source, pg_uuid_t *uuid)
8484
{
85-
char hex_buf[32]; /* not NUL terminated */
86-
int i;
87-
int src_len;
88-
89-
src_len = strlen(source);
90-
if (src_len != 32 && src_len != 36 && src_len != 38)
91-
goto syntax_error;
85+
const char *src = source;
86+
int i, braces = 0;
9287

93-
if (src_len == 32)
94-
memcpy(hex_buf, source, src_len);
95-
else
88+
if (src[0] == '{')
9689
{
97-
const char *str = source;
98-
99-
if (src_len == 38)
100-
{
101-
if (str[0] != '{' || str[37] != '}')
102-
goto syntax_error;
103-
104-
str++; /* skip the first character */
105-
}
106-
107-
if (str[8] != '-' || str[13] != '-' ||
108-
str[18] != '-' || str[23] != '-')
109-
goto syntax_error;
110-
111-
memcpy(hex_buf, str, 8);
112-
memcpy(hex_buf + 8, str + 9, 4);
113-
memcpy(hex_buf + 12, str + 14, 4);
114-
memcpy(hex_buf + 16, str + 19, 4);
115-
memcpy(hex_buf + 20, str + 24, 12);
90+
++src;
91+
braces = 1;
11692
}
11793

11894
for (i = 0; i < UUID_LEN; i++)
11995
{
12096
char str_buf[3];
12197

122-
memcpy(str_buf, &hex_buf[i * 2], 2);
98+
if (src[0] == '\0' || src[1] == '\0')
99+
goto syntax_error;
100+
memcpy(str_buf, src, 2);
123101
if (!isxdigit((unsigned char) str_buf[0]) ||
124102
!isxdigit((unsigned char) str_buf[1]))
125103
goto syntax_error;
126104

127105
str_buf[2] = '\0';
128106
uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
107+
src += 2;
108+
if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1)
109+
src++;
129110
}
130111

112+
if (braces)
113+
{
114+
if (*src!= '}')
115+
goto syntax_error;
116+
++src;
117+
}
118+
119+
if (*src != '\0')
120+
goto syntax_error;
121+
131122
return;
132123

133124
syntax_error:

0 commit comments

Comments
 (0)