Skip to content

Commit 430ce18

Browse files
committed
libpq: Discard leading and trailing spaces for parameters and values in URIs
Integer values applied a parsing rule through pqParseIntParam() that made URIs like this one working, even if these include spaces around values: "postgresql://localhost:5432/postgres?keepalives=1 &keepalives_idle=1 " This commit changes the parsing so as spaces before and after parameters and values are discarded, offering more consistency with the parsing that already applied to libpq for integer values in URIs. Note that %20 can be used in a URI for a space character. ECPGconnect() has been discarded leading and trailing spaces around parameters and values that for a long time, as well. Like f22e84d, this is done as a HEAD-only change. Reviewed-by: Yuto Sasaki Discussion: https://postgr.es/m/[email protected]
1 parent 68dfecb commit 430ce18

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/interfaces/libpq/fe-connect.c

+36-6
Original file line numberDiff line numberDiff line change
@@ -6763,9 +6763,9 @@ conninfo_uri_parse_params(char *params,
67636763
static char *
67646764
conninfo_uri_decode(const char *str, PQExpBuffer errorMessage)
67656765
{
6766-
char *buf;
6767-
char *p;
6768-
const char *q = str;
6766+
char *buf; /* result */
6767+
char *p; /* output location */
6768+
const char *q = str; /* input location */
67696769

67706770
buf = malloc(strlen(str) + 1);
67716771
if (buf == NULL)
@@ -6775,13 +6775,23 @@ conninfo_uri_decode(const char *str, PQExpBuffer errorMessage)
67756775
}
67766776
p = buf;
67776777

6778+
/* skip leading whitespaces */
6779+
for (const char *s = q; *s == ' '; s++)
6780+
{
6781+
q++;
6782+
continue;
6783+
}
6784+
67786785
for (;;)
67796786
{
67806787
if (*q != '%')
67816788
{
6782-
/* copy and check for NUL terminator */
6783-
if (!(*(p++) = *(q++)))
6784-
break;
6789+
/* if found a whitespace or NUL, the string ends */
6790+
if (*q == ' ' || *q == '\0')
6791+
goto end;
6792+
6793+
/* copy character */
6794+
*(p++) = *(q++);
67856795
}
67866796
else
67876797
{
@@ -6817,6 +6827,26 @@ conninfo_uri_decode(const char *str, PQExpBuffer errorMessage)
68176827
}
68186828
}
68196829

6830+
end:
6831+
6832+
/* skip trailing whitespaces */
6833+
for (const char *s = q; *s == ' '; s++)
6834+
{
6835+
q++;
6836+
continue;
6837+
}
6838+
6839+
/* Not at the end of the string yet? Fail. */
6840+
if (*q != '\0')
6841+
{
6842+
libpq_append_error(errorMessage, "trailing data found: \"%s\"", str);
6843+
free(buf);
6844+
return NULL;
6845+
}
6846+
6847+
/* Copy NUL terminator */
6848+
*p = '\0';
6849+
68206850
return buf;
68216851
}
68226852

src/interfaces/libpq/t/001_uri.pl

+18
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@
8686
q{user='uri-user' host='host' (inet)},
8787
q{},
8888
],
89+
[
90+
# Leading and trailing spaces, works.
91+
q{postgresql://host? user = uri-user & port = 12345 },
92+
q{user='uri-user' host='host' port='12345' (inet)},
93+
q{},
94+
],
95+
[
96+
# Trailing data in parameter.
97+
q{postgresql://host? user user = uri & port = 12345 12 },
98+
q{},
99+
q{libpq_uri_regress: trailing data found: " user user "},
100+
],
101+
[
102+
# Trailing data in value.
103+
q{postgresql://host? user = uri-user & port = 12345 12 },
104+
q{},
105+
q{libpq_uri_regress: trailing data found: " 12345 12 "},
106+
],
89107
[ q{postgresql://host?}, q{host='host' (inet)}, q{}, ],
90108
[
91109
q{postgresql://[::1]:12345/db},

0 commit comments

Comments
 (0)