diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2016-08-30 17:02:02 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2016-08-30 17:02:02 -0400 |
commit | 9daec77e165de461fca9d5bc3ece86a91aba5804 (patch) | |
tree | 0d5fc873a9602824e8363548b71af20b01f565c5 /src/bin/scripts/common.c | |
parent | 37f6fd1eaab698983ca1fb2a036d52381347ac71 (diff) | |
download | postgresql-9daec77e165de461fca9d5bc3ece86a91aba5804.tar.gz postgresql-9daec77e165de461fca9d5bc3ece86a91aba5804.zip |
Simplify correct use of simple_prompt().
The previous API for this function had it returning a malloc'd string.
That meant that callers had to check for NULL return, which few of them
were doing, and it also meant that callers had to remember to free()
the string later, which required extra logic in most cases.
Instead, make simple_prompt() write into a buffer supplied by the caller.
Anywhere that the maximum required input length is reasonably small,
which is almost all of the callers, we can just use a local or static
array as the buffer instead of dealing with malloc/free.
A fair number of callers used "pointer == NULL" as a proxy for "haven't
requested the password yet". Maintaining the same behavior requires
adding a separate boolean flag for that, which adds back some of the
complexity we save by removing free()s. Nonetheless, this nets out
at a small reduction in overall code size, and considerably less code
than we would have had if we'd added the missing NULL-return checks
everywhere they were needed.
In passing, clean up the API comment for simple_prompt() and get rid
of a very-unnecessary malloc/free in its Windows code path.
This is nominally a bug fix, but it does not seem worth back-patching,
because the actual risk of an OOM failure in any of these places seems
pretty tiny, and all of them are client-side not server-side anyway.
This patch is by me, but it owes a great deal to Michael Paquier
who identified the problem and drafted a patch for fixing it the
other way.
Discussion: <CAB7nPqRu07Ot6iht9i9KRfYLpDaF2ZuUv5y_+72uP23ZAGysRg@mail.gmail.com>
Diffstat (limited to 'src/bin/scripts/common.c')
-rw-r--r-- | src/bin/scripts/common.c | 34 |
1 files changed, 13 insertions, 21 deletions
diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c index 7c1ebe059fa..a71cc64a8c2 100644 --- a/src/bin/scripts/common.c +++ b/src/bin/scripts/common.c @@ -68,19 +68,19 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport, const char *progname, bool fail_ok, bool allow_password_reuse) { PGconn *conn; - static char *password = NULL; bool new_pass; + static bool have_password = false; + static char password[100]; if (!allow_password_reuse) + have_password = false; + + if (!have_password && prompt_password == TRI_YES) { - if (password) - free(password); - password = NULL; + simple_prompt("Password: ", password, sizeof(password), false); + have_password = true; } - if (password == NULL && prompt_password == TRI_YES) - password = simple_prompt("Password: ", 100, false); - /* * Start the connection. Loop until we have a password if requested by * backend. @@ -97,7 +97,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport, keywords[2] = "user"; values[2] = pguser; keywords[3] = "password"; - values[3] = password; + values[3] = have_password ? password : NULL; keywords[4] = "dbname"; values[4] = dbname; keywords[5] = "fallback_application_name"; @@ -123,9 +123,8 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport, prompt_password != TRI_NO) { PQfinish(conn); - if (password) - free(password); - password = simple_prompt("Password: ", 100, false); + simple_prompt("Password: ", password, sizeof(password), false); + have_password = true; new_pass = true; } } while (new_pass); @@ -275,22 +274,15 @@ yesno_prompt(const char *question) for (;;) { - char *resp; + char resp[10]; - resp = simple_prompt(prompt, 1, true); + simple_prompt(prompt, resp, sizeof(resp), true); if (strcmp(resp, _(PG_YESLETTER)) == 0) - { - free(resp); return true; - } - else if (strcmp(resp, _(PG_NOLETTER)) == 0) - { - free(resp); + if (strcmp(resp, _(PG_NOLETTER)) == 0) return false; - } - free(resp); printf(_("Please answer \"%s\" or \"%s\".\n"), _(PG_YESLETTER), _(PG_NOLETTER)); } |