Work around a subtle portability problem in use of printf %s format.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 8 May 2010 16:40:52 +0000 (16:40 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 8 May 2010 16:40:52 +0000 (16:40 +0000)
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".

src/backend/parser/scansup.c
src/interfaces/libpq/fe-misc.c

index 14467dcdc775ec8192b83b77913d6c2eeb1fe17b..254e3d7d763713ed3123fa714b830aad0ca8e464 100644 (file)
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/parser/scansup.c,v 1.24.4.1 2004/02/21 00:35:13 tgl Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/parser/scansup.c,v 1.24.4.2 2010/05/08 16:40:52 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';
    }
 }
index 0f3f2813711dc86d19867b5e0f55f6421b4c3350..c16c8d3330afcfad71ef1f1e65e0bf0fde228410 100644 (file)
@@ -23,7 +23,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.103.2.1 2005/07/08 15:25:19 tgl Exp $
+ *   $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.103.2.2 2010/05/08 16:40:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -69,6 +69,20 @@ static int pqSocketCheck(PGconn *conn, int forRead, int forWrite,
 static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
 
 
+/*
+ * 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);
+}
+
+
 /*
  * pqGetc: get 1 character from the connection
  *
@@ -175,7 +189,11 @@ pqGetnchar(char *s, size_t len, PGconn *conn)
    conn->inCursor += len;
 
    if (conn->Pfdebug)
-       fprintf(conn->Pfdebug, "From backend (%lu)> %.*s\n", (unsigned long) len, (int) len, s);
+   {
+       fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
+       fputnbytes(conn->Pfdebug, s, len);
+       fprintf(conn->Pfdebug, "\n");
+   }
 
    return 0;
 }
@@ -191,7 +209,11 @@ pqPutnchar(const char *s, size_t len, PGconn *conn)
        return EOF;
 
    if (conn->Pfdebug)
-       fprintf(conn->Pfdebug, "To backend> %.*s\n", (int) len, s);
+   {
+       fprintf(conn->Pfdebug, "To backend> ");
+       fputnbytes(conn->Pfdebug, s, len);
+       fprintf(conn->Pfdebug, "\n");
+   }
 
    return 0;
 }