static char *
conninfo_uri_decode(const char *str, PQExpBuffer errorMessage)
{
- char *buf;
- char *p;
- const char *q = str;
+ char *buf; /* result */
+ char *p; /* output location */
+ const char *q = str; /* input location */
buf = malloc(strlen(str) + 1);
if (buf == NULL)
}
p = buf;
+ /* skip leading whitespaces */
+ for (const char *s = q; *s == ' '; s++)
+ {
+ q++;
+ continue;
+ }
+
for (;;)
{
if (*q != '%')
{
- /* copy and check for NUL terminator */
- if (!(*(p++) = *(q++)))
- break;
+ /* if found a whitespace or NUL, the string ends */
+ if (*q == ' ' || *q == '\0')
+ goto end;
+
+ /* copy character */
+ *(p++) = *(q++);
}
else
{
}
}
+end:
+
+ /* skip trailing whitespaces */
+ for (const char *s = q; *s == ' '; s++)
+ {
+ q++;
+ continue;
+ }
+
+ /* Not at the end of the string yet? Fail. */
+ if (*q != '\0')
+ {
+ libpq_append_error(errorMessage, "trailing data found: \"%s\"", str);
+ free(buf);
+ return NULL;
+ }
+
+ /* Copy NUL terminator */
+ *p = '\0';
+
return buf;
}
q{user='uri-user' host='host' (inet)},
q{},
],
+ [
+ # Leading and trailing spaces, works.
+ q{postgresql://host? user = uri-user & port = 12345 },
+ q{user='uri-user' host='host' port='12345' (inet)},
+ q{},
+ ],
+ [
+ # Trailing data in parameter.
+ q{postgresql://host? user user = uri & port = 12345 12 },
+ q{},
+ q{libpq_uri_regress: trailing data found: " user user "},
+ ],
+ [
+ # Trailing data in value.
+ q{postgresql://host? user = uri-user & port = 12345 12 },
+ q{},
+ q{libpq_uri_regress: trailing data found: " 12345 12 "},
+ ],
[ q{postgresql://host?}, q{host='host' (inet)}, q{}, ],
[
q{postgresql://[::1]:12345/db},