|
1 | 1 | /*
|
2 | 2 | * PostgreSQL type definitions for MAC addresses.
|
3 | 3 | *
|
4 |
| - * $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.26 2002/09/04 20:31:28 momjian Exp $ |
| 4 | + * $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.27 2002/10/13 15:39:17 tgl Exp $ |
5 | 5 | */
|
6 | 6 |
|
7 | 7 | #include "postgres.h"
|
@@ -35,19 +35,28 @@ macaddr_in(PG_FUNCTION_ARGS)
|
35 | 35 | d,
|
36 | 36 | e,
|
37 | 37 | f;
|
| 38 | + char junk[2]; |
38 | 39 | int count;
|
39 | 40 |
|
40 |
| - count = sscanf(str, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f); |
| 41 | + /* %1s matches iff there is trailing non-whitespace garbage */ |
| 42 | + |
| 43 | + count = sscanf(str, "%x:%x:%x:%x:%x:%x%1s", |
| 44 | + &a, &b, &c, &d, &e, &f, junk); |
41 | 45 | if (count != 6)
|
42 |
| - count = sscanf(str, "%x-%x-%x-%x-%x-%x", &a, &b, &c, &d, &e, &f); |
| 46 | + count = sscanf(str, "%x-%x-%x-%x-%x-%x%1s", |
| 47 | + &a, &b, &c, &d, &e, &f, junk); |
43 | 48 | if (count != 6)
|
44 |
| - count = sscanf(str, "%2x%2x%2x:%2x%2x%2x", &a, &b, &c, &d, &e, &f); |
| 49 | + count = sscanf(str, "%2x%2x%2x:%2x%2x%2x%1s", |
| 50 | + &a, &b, &c, &d, &e, &f, junk); |
45 | 51 | if (count != 6)
|
46 |
| - count = sscanf(str, "%2x%2x%2x-%2x%2x%2x", &a, &b, &c, &d, &e, &f); |
| 52 | + count = sscanf(str, "%2x%2x%2x-%2x%2x%2x%1s", |
| 53 | + &a, &b, &c, &d, &e, &f, junk); |
47 | 54 | if (count != 6)
|
48 |
| - count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x", &a, &b, &c, &d, &e, &f); |
| 55 | + count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x%1s", |
| 56 | + &a, &b, &c, &d, &e, &f, junk); |
49 | 57 | if (count != 6)
|
50 |
| - count = sscanf(str, "%2x%2x%2x%2x%2x%2x", &a, &b, &c, &d, &e, &f); |
| 58 | + count = sscanf(str, "%2x%2x%2x%2x%2x%2x%1s", |
| 59 | + &a, &b, &c, &d, &e, &f, junk); |
51 | 60 | if (count != 6)
|
52 | 61 | elog(ERROR, "macaddr_in: error in parsing \"%s\"", str);
|
53 | 62 |
|
@@ -122,11 +131,11 @@ text_macaddr(PG_FUNCTION_ARGS)
|
122 | 131 | {
|
123 | 132 | text *addr = PG_GETARG_TEXT_P(0);
|
124 | 133 | Datum result;
|
125 |
| - char str[18]; |
| 134 | + char str[100]; |
126 | 135 | int len;
|
127 | 136 |
|
128 | 137 | len = (VARSIZE(addr) - VARHDRSZ);
|
129 |
| - if (len >= 18) |
| 138 | + if (len >= sizeof(str)) |
130 | 139 | elog(ERROR, "Text is too long to convert to MAC address");
|
131 | 140 |
|
132 | 141 | memcpy(str, VARDATA(addr), len);
|
|
0 commit comments