mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +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/print.c,v 1.124 2010/03/01 21:27:26 heikki Exp $
|
||||
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.125 2010/05/08 16:39:52 tgl Exp $
|
||||
*/
|
||||
#include "postgres_fe.h"
|
||||
|
||||
@ -252,6 +252,20 @@ format_numeric_locale(const char *my_str)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* fputnbytes: print exactly N bytes to a file
|
||||
*
|
||||
* Think not to use fprintf with a %.*s format for this. Some machines
|
||||
* believe %s's precision is measured in characters, others in bytes.
|
||||
*/
|
||||
static void
|
||||
fputnbytes(FILE *f, const char *str, size_t n)
|
||||
{
|
||||
while (n-- > 0)
|
||||
fputc(*str++, f);
|
||||
}
|
||||
|
||||
|
||||
/*************************/
|
||||
/* Unaligned text */
|
||||
/*************************/
|
||||
@ -913,14 +927,16 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
|
||||
{
|
||||
/* spaces first */
|
||||
fprintf(fout, "%*s", width_wrap[j] - chars_to_output, "");
|
||||
fprintf(fout, "%.*s", bytes_to_output,
|
||||
this_line->ptr + bytes_output[j]);
|
||||
fputnbytes(fout,
|
||||
this_line->ptr + bytes_output[j],
|
||||
bytes_to_output);
|
||||
}
|
||||
else /* Left aligned cell */
|
||||
{
|
||||
/* spaces second */
|
||||
fprintf(fout, "%.*s", bytes_to_output,
|
||||
this_line->ptr + bytes_output[j]);
|
||||
fputnbytes(fout,
|
||||
this_line->ptr + bytes_output[j],
|
||||
bytes_to_output);
|
||||
}
|
||||
|
||||
bytes_output[j] += bytes_to_output;
|
||||
|
Reference in New Issue
Block a user