summaryrefslogtreecommitdiff
path: root/src/bin/psql/help.c
diff options
context:
space:
mode:
authorTom Lane2010-05-08 16:39:53 +0000
committerTom Lane2010-05-08 16:39:53 +0000
commit54cd4f04576833abc394e131288bf3dd7dcf4806 (patch)
tree0772e1bedbca8466701b6a116e065e7a00959ebd /src/bin/psql/help.c
parent71a185a24d573dc1449777ff9fa8f3020af6f13c (diff)
Work around a subtle portability problem in use of printf %s format.
Depending on which spec you read, field widths and precisions in %s may be counted either in bytes or characters. Our code was assuming bytes, which is wrong at least for glibc's implementation, and in any case libc might have a different idea of the prevailing encoding than we do. Hence, for portable results we must avoid using anything more complex than just "%s" unless the string to be printed is known to be all-ASCII. This patch fixes the cases I could find, including the psql formatting failure reported by Hernan Gonzalez. In HEAD only, I also added comments to some places where it appears safe to continue using "%.*s".
Diffstat (limited to 'src/bin/psql/help.c')
-rw-r--r--src/bin/psql/help.c18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index a591dec792a..98f13750bf2 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.157 2010/03/07 17:02:34 mha Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.158 2010/05/08 16:39:51 tgl Exp $
*/
#include "postgres_fe.h"
@@ -284,6 +284,7 @@ slashUsage(unsigned short int pager)
/*
* helpSQL -- help with SQL commands
*
+ * Note: we assume caller removed any trailing spaces in "topic".
*/
void
helpSQL(const char *topic, unsigned short int pager)
@@ -352,17 +353,16 @@ helpSQL(const char *topic, unsigned short int pager)
wordlen;
int nl_count = 0;
- /* User gets two chances: exact match, then the first word */
-
- /* First pass : strip trailing spaces and semicolons */
+ /*
+ * We first try exact match, then first + second words, then first
+ * word only.
+ */
len = strlen(topic);
- while (topic[len - 1] == ' ' || topic[len - 1] == ';')
- len--;
- for (x = 1; x <= 3; x++) /* Three chances to guess that word... */
+ for (x = 1; x <= 3; x++)
{
if (x > 1) /* Nothing on first pass - try the opening
- * words */
+ * word(s) */
{
wordlen = j = 1;
while (topic[j] != ' ' && j++ < len)
@@ -423,7 +423,7 @@ helpSQL(const char *topic, unsigned short int pager)
}
if (!help_found)
- fprintf(output, _("No help available for \"%-.*s\".\nTry \\h with no arguments to see available help.\n"), (int) len, topic);
+ fprintf(output, _("No help available for \"%s\".\nTry \\h with no arguments to see available help.\n"), topic);
/* Only close if we used the pager */
if (output != stdout)