pgsql: Simplify correct use of simple_prompt().

Поиск
Список
Период
Сортировка
От Tom Lane
Тема pgsql: Simplify correct use of simple_prompt().
Дата
Msg-id E1beqAi-0003v5-2C@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
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>

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/9daec77e165de461fca9d5bc3ece86a91aba5804

Modified Files
--------------
contrib/oid2name/oid2name.c        | 13 ++++++-----
contrib/vacuumlo/vacuumlo.c        | 17 +++++++++------
src/bin/initdb/initdb.c            | 23 ++++++++------------
src/bin/pg_basebackup/nls.mk       |  1 +
src/bin/pg_basebackup/streamutil.c | 14 ++++++------
src/bin/pg_dump/pg_backup_db.c     | 35 ++++++++++++------------------
src/bin/pg_dump/pg_dumpall.c       | 17 +++++++++------
src/bin/pgbench/pgbench.c          | 10 +++++----
src/bin/psql/command.c             | 25 +++++++++++-----------
src/bin/psql/startup.c             | 16 ++++++++------
src/bin/scripts/common.c           | 34 +++++++++++------------------
src/bin/scripts/createuser.c       | 21 +++++++++++-------
src/bin/scripts/dropuser.c         |  7 +++++-
src/include/port.h                 |  3 ++-
src/port/sprompt.c                 | 44 +++++++++++++++-----------------------
15 files changed, 138 insertions(+), 142 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: pgsql: Fix initdb misbehavior when user mis-enters superuser password.
Следующее
От: Tom Lane
Дата:
Сообщение: pgsql: Fix a bunch of places that called malloc and friends with no NUL