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

Commit b04e3a2

Browse files
committed
Remove special-case treatment of all-zeroes MAC address, per today's
discussion in pgsql-general.
1 parent 5db5c2d commit b04e3a2

File tree

1 file changed

+26
-41
lines changed
  • src/backend/utils/adt

1 file changed

+26
-41
lines changed

src/backend/utils/adt/mac.c

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* PostgreSQL type definitions for MAC addresses.
33
*
4-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.20 2001/03/22 03:59:51 momjian Exp $
4+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.21 2001/08/21 21:23:21 tgl Exp $
55
*/
66

77
#include "postgres.h"
@@ -28,40 +28,32 @@ Datum
2828
macaddr_in(PG_FUNCTION_ARGS)
2929
{
3030
char *str = PG_GETARG_CSTRING(0);
31+
macaddr *result;
3132
int a,
3233
b,
3334
c,
3435
d,
3536
e,
3637
f;
37-
macaddr *result;
3838
int count;
3939

40-
if (strlen(str) > 0)
41-
{
42-
count = sscanf(str, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
43-
if (count != 6)
44-
count = sscanf(str, "%x-%x-%x-%x-%x-%x", &a, &b, &c, &d, &e, &f);
45-
if (count != 6)
46-
count = sscanf(str, "%2x%2x%2x:%2x%2x%2x", &a, &b, &c, &d, &e, &f);
47-
if (count != 6)
48-
count = sscanf(str, "%2x%2x%2x-%2x%2x%2x", &a, &b, &c, &d, &e, &f);
49-
if (count != 6)
50-
count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x", &a, &b, &c, &d, &e, &f);
51-
52-
if (count != 6)
53-
elog(ERROR, "macaddr_in: error in parsing \"%s\"", str);
54-
55-
if ((a < 0) || (a > 255) || (b < 0) || (b > 255) ||
56-
(c < 0) || (c > 255) || (d < 0) || (d > 255) ||
57-
(e < 0) || (e > 255) || (f < 0) || (f > 255))
58-
elog(ERROR, "macaddr_in: illegal address \"%s\"", str);
59-
}
60-
else
61-
{
62-
a = b = c = d = e = f = 0; /* special case for missing
63-
* address */
64-
}
40+
count = sscanf(str, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
41+
if (count != 6)
42+
count = sscanf(str, "%x-%x-%x-%x-%x-%x", &a, &b, &c, &d, &e, &f);
43+
if (count != 6)
44+
count = sscanf(str, "%2x%2x%2x:%2x%2x%2x", &a, &b, &c, &d, &e, &f);
45+
if (count != 6)
46+
count = sscanf(str, "%2x%2x%2x-%2x%2x%2x", &a, &b, &c, &d, &e, &f);
47+
if (count != 6)
48+
count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x", &a, &b, &c, &d, &e, &f);
49+
50+
if (count != 6)
51+
elog(ERROR, "macaddr_in: error in parsing \"%s\"", str);
52+
53+
if ((a < 0) || (a > 255) || (b < 0) || (b > 255) ||
54+
(c < 0) || (c > 255) || (d < 0) || (d > 255) ||
55+
(e < 0) || (e > 255) || (f < 0) || (f > 255))
56+
elog(ERROR, "macaddr_in: illegal address \"%s\"", str);
6557

6658
result = (macaddr *) palloc(sizeof(macaddr));
6759

@@ -87,20 +79,13 @@ macaddr_out(PG_FUNCTION_ARGS)
8779

8880
result = (char *) palloc(32);
8981

90-
if ((hibits(addr) > 0) || (lobits(addr) > 0))
91-
{
92-
sprintf(result, "%02x:%02x:%02x:%02x:%02x:%02x",
93-
addr->a, addr->b, addr->c, addr->d, addr->e, addr->f);
94-
}
95-
else
96-
{
97-
result[0] = '\0'; /* special case for missing address */
98-
}
82+
sprintf(result, "%02x:%02x:%02x:%02x:%02x:%02x",
83+
addr->a, addr->b, addr->c, addr->d, addr->e, addr->f);
9984

10085
PG_RETURN_CSTRING(result);
10186
}
10287

103-
/* macaddr_text()
88+
/*
10489
* Convert macaddr to text data type.
10590
*/
10691

@@ -127,23 +112,23 @@ macaddr_text(PG_FUNCTION_ARGS)
127112
PG_RETURN_TEXT_P(result);
128113
}
129114

130-
/* text_macaddr()
115+
/*
131116
* Convert text to macaddr data type.
132117
*/
133118

134119
Datum
135120
text_macaddr(PG_FUNCTION_ARGS)
136121
{
137-
Datum result;
138122
text *addr = PG_GETARG_TEXT_P(0);
123+
Datum result;
139124
char str[18];
140125
int len;
141126

142127
len = (VARSIZE(addr) - VARHDRSZ);
143128
if (len >= 18)
144129
elog(ERROR, "Text is too long to convert to MAC address");
145130

146-
memmove(str, VARDATA(addr), len);
131+
memcpy(str, VARDATA(addr), len);
147132
*(str + len) = '\0';
148133

149134
result = DirectFunctionCall1(macaddr_in, CStringGetDatum(str));
@@ -255,8 +240,8 @@ hashmacaddr(PG_FUNCTION_ARGS)
255240
Datum
256241
macaddr_trunc(PG_FUNCTION_ARGS)
257242
{
258-
macaddr *result;
259243
macaddr *addr = PG_GETARG_MACADDR_P(0);
244+
macaddr *result;
260245

261246
result = (macaddr *) palloc(sizeof(macaddr));
262247

0 commit comments

Comments
 (0)