Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit47bdd80

Browse files
committed
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 becounted either in bytes or characters. Our code was assuming bytes, whichis wrong at least for glibc's implementation, and in any case libc mighthave a different idea of the prevailing encoding than we do. Hence, forportable 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 formattingfailure reported by Hernan Gonzalez. In HEAD only, I also added commentsto some places where it appears safe to continue using "%.*s".
1 parent79c712a commit47bdd80

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

‎src/backend/parser/scansup.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/parser/scansup.c,v 1.24.4.1 2004/02/21 00:35:13 tgl Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/parser/scansup.c,v 1.24.4.2 2010/05/08 16:40:52 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -176,10 +176,20 @@ truncate_identifier(char *ident, int len, bool warn)
176176
{
177177
len=pg_mbcliplen(ident,len,NAMEDATALEN-1);
178178
if (warn)
179+
{
180+
/*
181+
* Cannot use %.*s here because some machines interpret %s's
182+
* precision in characters, others in bytes.
183+
*/
184+
charbuf[NAMEDATALEN];
185+
186+
memcpy(buf,ident,len);
187+
buf[len]='\0';
179188
ereport(NOTICE,
180189
(errcode(ERRCODE_NAME_TOO_LONG),
181-
errmsg("identifier \"%s\" will be truncated to \"%.*s\"",
182-
ident,len,ident)));
190+
errmsg("identifier \"%s\" will be truncated to \"%s\"",
191+
ident,buf)));
192+
}
183193
ident[len]='\0';
184194
}
185195
}

‎src/interfaces/libpq/fe-misc.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Portions Copyright (c) 1994, Regents of the University of California
2424
*
2525
* IDENTIFICATION
26-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.103.2.1 2005/07/0815:25:19 tgl Exp $
26+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.103.2.2 2010/05/0816:40:52 tgl Exp $
2727
*
2828
*-------------------------------------------------------------------------
2929
*/
@@ -69,6 +69,20 @@ static int pqSocketCheck(PGconn *conn, int forRead, int forWrite,
6969
staticintpqSocketPoll(intsock,intforRead,intforWrite,time_tend_time);
7070

7171

72+
/*
73+
* fputnbytes: print exactly N bytes to a file
74+
*
75+
* Think not to use fprintf with a %.*s format for this. Some machines
76+
* believe %s's precision is measured in characters, others in bytes.
77+
*/
78+
staticvoid
79+
fputnbytes(FILE*f,constchar*str,size_tn)
80+
{
81+
while (n-->0)
82+
fputc(*str++,f);
83+
}
84+
85+
7286
/*
7387
* pqGetc: get 1 character from the connection
7488
*
@@ -175,7 +189,11 @@ pqGetnchar(char *s, size_t len, PGconn *conn)
175189
conn->inCursor+=len;
176190

177191
if (conn->Pfdebug)
178-
fprintf(conn->Pfdebug,"From backend (%lu)> %.*s\n", (unsigned long)len, (int)len,s);
192+
{
193+
fprintf(conn->Pfdebug,"From backend (%lu)> ", (unsigned long)len);
194+
fputnbytes(conn->Pfdebug,s,len);
195+
fprintf(conn->Pfdebug,"\n");
196+
}
179197

180198
return0;
181199
}
@@ -191,7 +209,11 @@ pqPutnchar(const char *s, size_t len, PGconn *conn)
191209
returnEOF;
192210

193211
if (conn->Pfdebug)
194-
fprintf(conn->Pfdebug,"To backend> %.*s\n", (int)len,s);
212+
{
213+
fprintf(conn->Pfdebug,"To backend> ");
214+
fputnbytes(conn->Pfdebug,s,len);
215+
fprintf(conn->Pfdebug,"\n");
216+
}
195217

196218
return0;
197219
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp