diff options
author | Tom Lane | 2010-05-08 16:39:53 +0000 |
---|---|---|
committer | Tom Lane | 2010-05-08 16:39:53 +0000 |
commit | 54cd4f04576833abc394e131288bf3dd7dcf4806 (patch) | |
tree | 0772e1bedbca8466701b6a116e065e7a00959ebd /src/backend | |
parent | 71a185a24d573dc1449777ff9fa8f3020af6f13c (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/backend')
-rw-r--r-- | src/backend/lib/stringinfo.c | 5 | ||||
-rw-r--r-- | src/backend/parser/scansup.c | 16 | ||||
-rw-r--r-- | src/backend/tsearch/wparser_def.c | 9 | ||||
-rw-r--r-- | src/backend/utils/adt/datetime.c | 11 | ||||
-rw-r--r-- | src/backend/utils/error/elog.c | 6 |
5 files changed, 37 insertions, 10 deletions
diff --git a/src/backend/lib/stringinfo.c b/src/backend/lib/stringinfo.c index 88db51871e9..9ae2455000f 100644 --- a/src/backend/lib/stringinfo.c +++ b/src/backend/lib/stringinfo.c @@ -9,7 +9,7 @@ * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.52 2010/01/02 16:57:45 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.53 2010/05/08 16:39:49 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -226,7 +226,8 @@ appendBinaryStringInfo(StringInfo str, const char *data, int datalen) /* * Keep a trailing null in place, even though it's probably useless for - * binary data... + * binary data. (Some callers are dealing with text but call this + * because their input isn't null-terminated.) */ str->data[str->len] = '\0'; } diff --git a/src/backend/parser/scansup.c b/src/backend/parser/scansup.c index 5bc6d8d6071..417c79dd14e 100644 --- a/src/backend/parser/scansup.c +++ b/src/backend/parser/scansup.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/scansup.c,v 1.39 2010/01/02 16:57:50 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/parser/scansup.c,v 1.40 2010/05/08 16:39:49 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -176,10 +176,20 @@ truncate_identifier(char *ident, int len, bool warn) { len = pg_mbcliplen(ident, len, NAMEDATALEN - 1); if (warn) + { + /* + * Cannot use %.*s here because some machines interpret %s's + * precision in characters, others in bytes. + */ + char buf[NAMEDATALEN]; + + memcpy(buf, ident, len); + buf[len] = '\0'; ereport(NOTICE, (errcode(ERRCODE_NAME_TOO_LONG), - errmsg("identifier \"%s\" will be truncated to \"%.*s\"", - ident, len, ident))); + errmsg("identifier \"%s\" will be truncated to \"%s\"", + ident, buf))); + } ident[len] = '\0'; } } diff --git a/src/backend/tsearch/wparser_def.c b/src/backend/tsearch/wparser_def.c index a2da9210c4c..d2e47ceaf5e 100644 --- a/src/backend/tsearch/wparser_def.c +++ b/src/backend/tsearch/wparser_def.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/tsearch/wparser_def.c,v 1.30 2010/04/28 02:04:16 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/tsearch/wparser_def.c,v 1.31 2010/05/08 16:39:49 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -322,6 +322,12 @@ TParserInit(char *str, int len) prs->state->state = TPS_Base; #ifdef WPARSER_TRACE + /* + * Use of %.*s here is not portable when the string contains multibyte + * characters: some machines interpret the length in characters, others + * in bytes. Since it's only a debugging aid, we haven't bothered to + * fix this. + */ fprintf(stderr, "parsing \"%.*s\"\n", len, str); #endif @@ -361,6 +367,7 @@ TParserCopyInit(const TParser *orig) prs->state->state = TPS_Base; #ifdef WPARSER_TRACE + /* See note above about %.*s */ fprintf(stderr, "parsing copy of \"%.*s\"\n", prs->lenstr, prs->str); #endif diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c index f9e40f115fc..743ca8345d6 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.210 2010/01/02 16:57:53 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.211 2010/05/08 16:39:51 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -3740,6 +3740,14 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, AppendTimestampSeconds(str + strlen(str), tm, fsec); + /* + * Note: the uses of %.*s in this function would be unportable + * if the timezone names ever contain non-ASCII characters, + * since some platforms think the string length is measured + * in characters not bytes. However, all TZ abbreviations in + * the Olson database are plain ASCII. + */ + if (tzp != NULL && tm->tm_isdst >= 0) { if (*tzn != NULL) @@ -4091,6 +4099,7 @@ CheckDateTokenTable(const char *tablename, const datetkn *base, int nel) { if (strncmp(base[i - 1].token, base[i].token, TOKMAXLEN) >= 0) { + /* %.*s is safe since all our tokens are ASCII */ elog(LOG, "ordering error in %s table: \"%.*s\" >= \"%.*s\"", tablename, TOKMAXLEN, base[i - 1].token, diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index a6992e65d94..b2fab359b87 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -42,7 +42,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.223 2010/02/26 02:01:12 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.224 2010/05/08 16:39:51 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1871,7 +1871,7 @@ log_line_prefix(StringInfo buf, ErrorData *edata) int displen; psdisp = get_ps_display(&displen); - appendStringInfo(buf, "%.*s", displen, psdisp); + appendBinaryStringInfo(buf, psdisp, displen); } break; case 'r': @@ -2029,7 +2029,7 @@ write_csvlog(ErrorData *edata) initStringInfo(&msgbuf); psdisp = get_ps_display(&displen); - appendStringInfo(&msgbuf, "%.*s", displen, psdisp); + appendBinaryStringInfo(&msgbuf, psdisp, displen); appendCSVLiteral(&buf, msgbuf.data); pfree(msgbuf.data); |