Skip to content

Commit b4ab0ef

Browse files
committed
Fixed bug #71998 Function pg_insert does not insert when column type = inet
1 parent 58ea2ad commit b4ab0ef

2 files changed

Lines changed: 202 additions & 3 deletions

File tree

ext/pgsql/pgsql.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6145,8 +6145,11 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
61456145
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
61466146
}
61476147
else {
6148-
/* better regex? IPV6 and IPV4 */
6149-
if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$", 0) == FAILURE) {
6148+
/* The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. See more in the doc.
6149+
The regex might still be not perfect, but catches the most of IP variants. We might decide to remove the regex
6150+
at all though and let the server side to handle it.*/
6151+
if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\/[0-9]{1,3})?$", 0) == FAILURE
6152+
&& php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\/[0-9]{1,3})?$", 0) == FAILURE) {
61506153
err = 1;
61516154
}
61526155
else {
@@ -6165,7 +6168,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
61656168
}
61666169
PGSQL_CONV_CHECK_IGNORE();
61676170
if (err) {
6168-
php_error_docref(NULL, E_NOTICE, "Expects NULL or string for '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
6171+
php_error_docref(NULL, E_NOTICE, "Expects NULL or IPv4 or IPv6 address string for '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
61696172
}
61706173
break;
61716174

ext/pgsql/tests/bug71998.phpt

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
--TEST--
2+
Bug #71998 Function pg_insert does not insert when column type = inet
3+
--SKIPIF--
4+
<?php include("skipif.inc"); ?>
5+
--FILE--
6+
<?php
7+
// Kudos for the IP regex to
8+
// http://stackoverflow.com/a/17871737/3358424
9+
10+
include('config.inc');
11+
12+
$db = pg_connect($conn_str);
13+
14+
pg_query("CREATE TABLE tmp_statistics (id integer NOT NULL, remote_addr inet);");
15+
16+
$ips = array(
17+
/* IPv4*/
18+
"127.0.0.1",
19+
"10.0.0.1",
20+
"192.168.1.1",
21+
"0.0.0.0",
22+
"255.255.255.255",
23+
"192.168.1.35/24",
24+
25+
/* IPv6 */
26+
"::1",
27+
"::10.2.3.4",
28+
"::ffff:10.4.3.2",
29+
"1:2:3:4:5:6:7:8",
30+
"::ffff:10.0.0.1",
31+
"::ffff:1.2.3.4",
32+
"::ffff:0.0.0.0",
33+
"1:2:3:4:5:6:77:88",
34+
"::ffff:255.255.255.255",
35+
"fe08::7:8",
36+
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
37+
"::5:aef1:ffff/128",
38+
"2001:4f8:3:ba::/112",
39+
40+
);
41+
42+
$bad = array(
43+
/* bad */
44+
"256.257.258.259",
45+
"fe08::7:8interface",
46+
"schnitzel",
47+
"10002.3.4",
48+
"1.2.3.4.5",
49+
"256.0.0.0",
50+
"260.0.0.0",
51+
);
52+
53+
$ips = array_merge($ips, $bad);
54+
$i = 0;
55+
$errors = 0;
56+
foreach ($ips as $ip) {
57+
$data = array("id" => ++$i, "remote_addr" => $ip);
58+
$r = @pg_insert($db, 'tmp_statistics', $data);
59+
60+
if (!$r && in_array($ip, $bad)) {
61+
$errors++;
62+
//echo pg_last_error($db);
63+
}
64+
65+
//pg_query($db, "INSERT INTO tmp_statistics (id, remote_addr) VALUES (2, '127.0.0.1')"); // OK, record inserted
66+
}
67+
68+
69+
$r = pg_query($db, "SELECT * FROM tmp_statistics");
70+
while (false != ($row = pg_fetch_row($r))) {
71+
var_dump($row);
72+
}
73+
echo $errors, " errors catched\n";
74+
75+
pg_query($db, "DROP TABLE tmp_statistics");
76+
pg_close($db);
77+
78+
?>
79+
==DONE==
80+
--EXPECT--
81+
array(2) {
82+
[0]=>
83+
string(1) "1"
84+
[1]=>
85+
string(9) "127.0.0.1"
86+
}
87+
array(2) {
88+
[0]=>
89+
string(1) "2"
90+
[1]=>
91+
string(8) "10.0.0.1"
92+
}
93+
array(2) {
94+
[0]=>
95+
string(1) "3"
96+
[1]=>
97+
string(11) "192.168.1.1"
98+
}
99+
array(2) {
100+
[0]=>
101+
string(1) "4"
102+
[1]=>
103+
string(7) "0.0.0.0"
104+
}
105+
array(2) {
106+
[0]=>
107+
string(1) "5"
108+
[1]=>
109+
string(15) "255.255.255.255"
110+
}
111+
array(2) {
112+
[0]=>
113+
string(1) "6"
114+
[1]=>
115+
string(15) "192.168.1.35/24"
116+
}
117+
array(2) {
118+
[0]=>
119+
string(1) "7"
120+
[1]=>
121+
string(3) "::1"
122+
}
123+
array(2) {
124+
[0]=>
125+
string(1) "8"
126+
[1]=>
127+
string(10) "::10.2.3.4"
128+
}
129+
array(2) {
130+
[0]=>
131+
string(1) "9"
132+
[1]=>
133+
string(15) "::ffff:10.4.3.2"
134+
}
135+
array(2) {
136+
[0]=>
137+
string(2) "10"
138+
[1]=>
139+
string(15) "1:2:3:4:5:6:7:8"
140+
}
141+
array(2) {
142+
[0]=>
143+
string(2) "11"
144+
[1]=>
145+
string(15) "::ffff:10.0.0.1"
146+
}
147+
array(2) {
148+
[0]=>
149+
string(2) "12"
150+
[1]=>
151+
string(14) "::ffff:1.2.3.4"
152+
}
153+
array(2) {
154+
[0]=>
155+
string(2) "13"
156+
[1]=>
157+
string(14) "::ffff:0.0.0.0"
158+
}
159+
array(2) {
160+
[0]=>
161+
string(2) "14"
162+
[1]=>
163+
string(17) "1:2:3:4:5:6:77:88"
164+
}
165+
array(2) {
166+
[0]=>
167+
string(2) "15"
168+
[1]=>
169+
string(22) "::ffff:255.255.255.255"
170+
}
171+
array(2) {
172+
[0]=>
173+
string(2) "16"
174+
[1]=>
175+
string(9) "fe08::7:8"
176+
}
177+
array(2) {
178+
[0]=>
179+
string(2) "17"
180+
[1]=>
181+
string(39) "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
182+
}
183+
array(2) {
184+
[0]=>
185+
string(2) "18"
186+
[1]=>
187+
string(13) "::5:aef1:ffff"
188+
}
189+
array(2) {
190+
[0]=>
191+
string(2) "19"
192+
[1]=>
193+
string(19) "2001:4f8:3:ba::/112"
194+
}
195+
7 errors catched
196+
==DONE==

0 commit comments

Comments
 (0)