mirror of
https://github.com/postgres/postgres.git
synced 2025-07-03 20:02:46 +03:00
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".
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user