select * from parse_address('123 Main Street, Kansas City, MO 45678');
 num |   street    | street2 |    address1     |    city     | state |  zip  | zipplus | country 
-----+-------------+---------+-----------------+-------------+-------+-------+---------+---------
 123 | Main Street |         | 123 Main Street | Kansas City | MO    | 45678 |         | US
(1 row)

select * from standardize_address('us_lex'::text, 'us_gaz'::text, 'us_rules'::text, '123 Main Street'::text, 'Kansas City, MO 45678'::text);
 building | house_num | predir | qual | pretype | name | suftype | sufdir | ruralroute | extra |    city     |  state   | country | postcode | box | unit 
----------+-----------+--------+------+---------+------+---------+--------+------------+-------+-------------+----------+---------+----------+-----+------
          | 123       |        |      |         | MAIN | STREET  |        |            |       | KANSAS CITY | MISSOURI |         | 45678    |     | 
(1 row)

SELECT '#2981' As ticket, * FROM standardize_address('us_lex','us_gaz','us_rules', '1566 NEW STATE HWY, RAYNHAM, MA');
 ticket | building | house_num | predir | qual | pretype |       name        | suftype | sufdir | ruralroute | extra |  city   |     state     | country | postcode | box | unit 
--------+----------+-----------+--------+------+---------+-------------------+---------+--------+------------+-------+---------+---------------+---------+----------+-----+------
 #2981  |          | 1566      |        |      |         | NEW STATE HIGHWAY |         |        |            |       | RAYNHAM | MASSACHUSETTS | US      |          |     | 
(1 row)

SELECT '#2978a' As ticket, * FROM standardize_address('us_lex','us_gaz','us_rules', '10-20 DORRANCE ST PROVIDENCE RI' );
 ticket | building | house_num | predir | qual | pretype |   name   | suftype | sufdir | ruralroute | extra |    city    |    state     | country | postcode | box | unit 
--------+----------+-----------+--------+------+---------+----------+---------+--------+------------+-------+------------+--------------+---------+----------+-----+------
 #2978a |          | 10 20     |        |      |         | DORRANCE | STREET  |        |            |       | PROVIDENCE | RHODE ISLAND | US      |          |     | 
(1 row)

SELECT '#2978b' As ticket, * FROM standardize_address('us_lex','us_gaz','us_rules', '10 20 DORRANCE ST PROVIDENCE RI' );
 ticket | building | house_num | predir | qual | pretype |   name   | suftype | sufdir | ruralroute | extra |    city    |    state     | country | postcode | box | unit 
--------+----------+-----------+--------+------+---------+----------+---------+--------+------------+-------+------------+--------------+---------+----------+-----+------
 #2978b |          | 10 20     |        |      |         | DORRANCE | STREET  |        |            |       | PROVIDENCE | RHODE ISLAND | US      |          |     | 
(1 row)

SELECT '#2978c' As ticket, * FROM standardize_address('us_lex','us_gaz','us_rules', '10-20 DORRANCE ST, PROVIDENCE, RI');
 ticket | building | house_num | predir | qual | pretype |   name   | suftype | sufdir | ruralroute | extra |    city    |    state     | country | postcode | box | unit 
--------+----------+-----------+--------+------+---------+----------+---------+--------+------------+-------+------------+--------------+---------+----------+-----+------
 #2978c |          | 10 20     |        |      |         | DORRANCE | STREET  |        |            |       | PROVIDENCE | RHODE ISLAND | US      |          |     | 
(1 row)

SELECT '#state_only_macro' AS ticket, * FROM standardize_address('us_lex','us_gaz','us_rules', '25 Prince Street, NC 09985');
      ticket       | building | house_num | predir | qual | pretype |  name  | suftype | sufdir | ruralroute | extra | city |     state      | country | postcode | box | unit 
-------------------+----------+-----------+--------+------+---------+--------+---------+--------+------------+-------+------+----------------+---------+----------+-----+------
 #state_only_macro |          | 25        |        |      |         | PRINCE | STREET  |        |            |       |      | NORTH CAROLINA | US      | 09985    |     | 
(1 row)

SELECT '#5299a' AS ticket, * FROM standardize_address('us_lex',  'us_gaz', 'us_rules','1 Timepiece Point','Boston, MA, 02220');
 ticket | building | house_num | predir | qual | pretype |   name    | suftype | sufdir | ruralroute | extra |  city  |     state     | country | postcode | box | unit 
--------+----------+-----------+--------+------+---------+-----------+---------+--------+------------+-------+--------+---------------+---------+----------+-----+------
 #5299a |          | 1         |        |      |         | TIMEPIECE | POINT   |        |            |       | BOSTON | MASSACHUSETTS |         | 02220    |     | 
(1 row)

SELECT '#5299b' AS ticket, * FROM standardize_address('us_lex',  'us_gaz', 'us_rules','50 Gold Piece Drive','Boston, MA, 02020');
 ticket | building | house_num | predir | qual | pretype |    name    | suftype | sufdir | ruralroute | extra |  city  |     state     | country | postcode | box | unit 
--------+----------+-----------+--------+------+---------+------------+---------+--------+------------+-------+--------+---------------+---------+----------+-----+------
 #5299b |          | 50        |        |      |         | GOLD PIECE | DRIVE   |        |            |       | BOSTON | MASSACHUSETTS |         | 02020    |     | 
(1 row)

SELECT '#5695a' AS ticket, * FROM standardize_address('us_lex', 'us_gaz', 'us_rules', 'ONE E PIMA ST STE 999, TUCSON, AZ');
 ticket | building | house_num | predir | qual | pretype | name | suftype | sufdir | ruralroute | extra |  city  |  state  | country | postcode | box |   unit    
--------+----------+-----------+--------+------+---------+------+---------+--------+------------+-------+--------+---------+---------+----------+-----+-----------
 #5695a |          | 1         | EAST   |      |         | PIMA | STREET  |        |            |       | TUCSON | ARIZONA | US      |          |     | SUITE 999
(1 row)

SELECT '#2459a' AS ticket, * FROM standardize_address('us_lex', 'us_gaz', 'us_rules', '26 Court Street, Boston, Massachusetts 02109, France');
 ticket | building | house_num | predir | qual | pretype | name  | suftype | sufdir | ruralroute | extra |  city  |     state     | country | postcode | box | unit 
--------+----------+-----------+--------+------+---------+-------+---------+--------+------------+-------+--------+---------------+---------+----------+-----+------
 #2459a |          | 26        |        |      |         | COURT | STREET  |        |            |       | BOSTON | MASSACHUSETTS | FR      | 02109    |     | 
(1 row)

SELECT '#2459b' AS ticket, * FROM standardize_address('us_lex', 'us_gaz', 'us_rules', '212 3rd Ave N, MINNEAPOLIS, MN 553404');
 ticket | building | house_num | predir | qual | pretype | name | suftype | sufdir | ruralroute | extra |    city     |   state   | country | postcode | box | unit 
--------+----------+-----------+--------+------+---------+------+---------+--------+------------+-------+-------------+-----------+---------+----------+-----+------
 #2459b |          | 212       |        |      |         | 3    | AVENUE  | NORTH  |            |       | MINNEAPOLIS | MINNESOTA | US      | 55340-4  |     | 
(1 row)

DO $$
BEGIN
	PERFORM standardize_address('us_lex', 'us_gaz', 'us_rules', '   ');
EXCEPTION WHEN OTHERS THEN
	RAISE NOTICE 'blank-input: %', SQLERRM;
END
$$;
NOTICE:  blank-input: standardize_address() could not parse the address into components.
-- CVE: rule with >128 terms must be rejected gracefully, not crash (stack OOB write)
CREATE TEMP TABLE t_overlong_rule(id serial, rule text);
INSERT INTO t_overlong_rule(rule) SELECT string_agg('1', ' ') FROM generate_series(1, 130);
DO $$
BEGIN
	PERFORM standardize_address('us_lex', 'us_gaz', 't_overlong_rule', '1 Main St', 'Boston, MA');
EXCEPTION WHEN OTHERS THEN
	RAISE NOTICE 'overlong-rule: %', SQLERRM;
END
$$;
NOTICE:  load_roles: rule exceeds 128 terms
NOTICE:  overlong-rule: CreateStd: failed to load 't_overlong_rule' for rules
-- OOB read: rule missing type/weight tokens must be rejected, not read past array end
CREATE TEMP TABLE t_short_rule(id serial, rule text);
INSERT INTO t_short_rule(rule) VALUES ('1 -1 5 -1');
DO $$
BEGIN
	PERFORM standardize_address('us_lex', 'us_gaz', 't_short_rule', '1 Main St', 'Boston, MA');
EXCEPTION WHEN OTHERS THEN
	RAISE NOTICE 'short-rule: %', SQLERRM;
END
$$;
NOTICE:  load_roles: failed to add rule 1 (6): 1 -1 5 -1
NOTICE:  short-rule: CreateStd: failed to load 't_short_rule' for rules
