Skip site navigation (1) Skip section navigation (2)

MACADDR parsing issues

From: Matthijs Bomhoff <matthijs(at)quarantainenet(dot)nl>
To: pgsql-bugs(at)postgresql(dot)org
Subject: MACADDR parsing issues
Date: 2011-06-06 14:00:52
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-bugs

While playing around with different MAC address notations supported by postgresql, I encountered the following:

db=> select '08002b-010203'::macaddr;
(1 row)

db=> select '08002b-01023'::macaddr;
(1 row)

db=> select '08002b-0123'::macaddr;
(1 row)

db=> select '08002b-123'::macaddr;
ERROR:  invalid input syntax for type macaddr: "08002b-123"
LINE 1: select '08002b-123'::macaddr;
db=> select '08002b-1203'::macaddr;
ERROR:  invalid octet value in "macaddr" value: "08002b-1203"
LINE 1: select '08002b-1203'::macaddr;

(These particular results have been encountered on 8.4.4, but similar issues still seem to exist in the git head I pulled last week.)

Note how for example '08002b-0123' is accepted as a valid MAC and is parsed as '08:00:2b:00:12:03' leading to two additional zeroes being added in different places. Furthermore, the last example actually matches the pattern for a MAC without delimiters, incorrectly parsing "-1" as one of the octets and thus resulting in an error about invalid octets instead of an invalid syntax.

In case anyone is interested, I have attached a simple attempt at a patch for the MAC address parser that makes it a bit more strict in such a way that it still accepts the formats specified in the documentation, but rejects many other "broken" addresses that are currently accepted with sometimes surprising results. The attached version also rejects MACs containing additional whitespace between the octets and separators etc. The patch probably still needs a bit of work to make it more in line with your coding style, as well as a decent review to make sure it doesn't break anything else, but I'll leave that to those who know more about postgresql, MAC notations and sscanf :)

I have also added a couple of additional test cases in the same diff, although the code could still use a few more for possible corner cases etc.


Matthijs Bomhoff

Attachment: mac_parser.diff
Description: application/octet-stream (8.3 KB)


pgsql-bugs by date

Next:From: Tom LaneDate: 2011-06-06 14:31:50
Subject: Re: MACADDR parsing issues
Previous:From: Peter EisentrautDate: 2011-06-06 10:22:36
Subject: Re: BUG #6052: ADD COLUMN - ERROR: tables can have at most 1600 columns

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group