1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-16 06:01:02 +03:00

Mop up some no-longer-necessary hacks around printf %.*s format.

Commit 54cd4f045 added some kluges to work around an old glibc bug,
namely that %.*s could misbehave if glibc thought any characters in
the supplied string were incorrectly encoded.  Now that we use our
own snprintf.c implementation, we need not worry about that bug (even
if it still exists in the wild).  Revert a couple of particularly
ugly hacks, and remove or improve assorted comments.

Note that there can still be encoding-related hazards here: blindly
clipping at a fixed length risks producing wrongly-encoded output
if the clip splits a multibyte character.  However, code that's
doing correct multibyte-aware clipping doesn't really need a comment
about that, while code that isn't needs an explanation why not,
rather than a red-herring comment about an obsolete bug.

Discussion: https://postgr.es/m/279428.1593373684@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2020-06-29 17:12:38 -04:00
parent f7a476f0d6
commit c410af098c
9 changed files with 16 additions and 75 deletions

View File

@ -305,20 +305,6 @@ format_numeric_locale(const char *my_str)
}
/*
* fputnbytes: print exactly N bytes to a file
*
* We avoid using %.*s here because it can misbehave if the data
* is not valid in what libc thinks is the prevailing encoding.
*/
static void
fputnbytes(FILE *f, const char *str, size_t n)
{
while (n-- > 0)
fputc(*str++, f);
}
static void
print_separator(struct separator sep, FILE *fout)
{
@ -1042,16 +1028,14 @@ print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
{
/* spaces first */
fprintf(fout, "%*s", width_wrap[j] - chars_to_output, "");
fputnbytes(fout,
(char *) (this_line->ptr + bytes_output[j]),
bytes_to_output);
fwrite((char *) (this_line->ptr + bytes_output[j]),
1, bytes_to_output, fout);
}
else /* Left aligned cell */
{
/* spaces second */
fputnbytes(fout,
(char *) (this_line->ptr + bytes_output[j]),
bytes_to_output);
fwrite((char *) (this_line->ptr + bytes_output[j]),
1, bytes_to_output, fout);
}
bytes_output[j] += bytes_to_output;
@ -1637,8 +1621,8 @@ print_aligned_vertical(const printTableContent *cont,
*/
bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
&target_width, encoding);
fputnbytes(fout, (char *) (dlineptr[dline].ptr + offset),
bytes_to_output);
fwrite((char *) (dlineptr[dline].ptr + offset),
1, bytes_to_output, fout);
chars_to_output -= target_width;
offset += bytes_to_output;