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

Commited437e2

Browse files
committed
Adjust comments about avoiding use of printf's %.*s.
My initial impression that glibc was measuring the precision in characters(which is what the Linux man page says it does) was incorrect. It does takethe precision to be in bytes, but it also tries to truncate the string at acharacter boundary. The bottom line remains the same: it will mess upif the string is not in the encoding it expects, so we need to avoid %.*sanytime there's a significant risk of that. Previous code changes are stillgood, but adjust the comments to reflect this knowledge. Per research byHernan Gonzalez.
1 parent54cd4f0 commited437e2

File tree

6 files changed

+21
-26
lines changed

6 files changed

+21
-26
lines changed

‎src/backend/parser/scansup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/parser/scansup.c,v 1.40 2010/05/08 16:39:49 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/parser/scansup.c,v 1.41 2010/05/09 02:15:59 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -178,8 +178,8 @@ truncate_identifier(char *ident, int len, bool warn)
178178
if (warn)
179179
{
180180
/*
181-
*Cannot use%.*s here becausesome machines interpret %s's
182-
*precisionincharacters, others in bytes.
181+
*We avoid using%.*s here becauseit can misbehave if the data
182+
*is not validinwhat libc thinks is the prevailing encoding.
183183
*/
184184
charbuf[NAMEDATALEN];
185185

‎src/backend/tsearch/wparser_def.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/tsearch/wparser_def.c,v 1.31 2010/05/08 16:39:49 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/tsearch/wparser_def.c,v 1.32 2010/05/09 02:15:59 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -323,10 +323,9 @@ TParserInit(char *str, int len)
323323

324324
#ifdefWPARSER_TRACE
325325
/*
326-
* Use of %.*s here is not portable when the string contains multibyte
327-
* characters: some machines interpret the length in characters, others
328-
* in bytes. Since it's only a debugging aid, we haven't bothered to
329-
* fix this.
326+
* Use of %.*s here is a bit risky since it can misbehave if the data
327+
* is not in what libc thinks is the prevailing encoding. However,
328+
* since this is just a debugging aid, we choose to live with that.
330329
*/
331330
fprintf(stderr,"parsing \"%.*s\"\n",len,str);
332331
#endif

‎src/backend/utils/adt/datetime.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.211 2010/05/08 16:39:51 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.212 2010/05/09 02:15:59 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3741,11 +3741,9 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
37413741
AppendTimestampSeconds(str+strlen(str),tm,fsec);
37423742

37433743
/*
3744-
* Note: the uses of %.*s in this function would be unportable
3745-
* if the timezone names ever contain non-ASCII characters,
3746-
* since some platforms think the string length is measured
3747-
* in characters not bytes. However, all TZ abbreviations in
3748-
* the Olson database are plain ASCII.
3744+
* Note: the uses of %.*s in this function would be risky if the
3745+
* timezone names ever contain non-ASCII characters. However, all
3746+
* TZ abbreviations in the Olson database are plain ASCII.
37493747
*/
37503748

37513749
if (tzp!=NULL&&tm->tm_isdst >=0)

‎src/bin/psql/print.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.125 2010/05/08 16:39:52 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.126 2010/05/09 02:15:59 tgl Exp $
77
*/
88
#include"postgres_fe.h"
99

@@ -255,8 +255,8 @@ format_numeric_locale(const char *my_str)
255255
/*
256256
* fputnbytes: print exactly N bytes to a file
257257
*
258-
*Think not to use fprintf with a%.*sformat for this. Some machines
259-
*believe %s's precision is measured in characters, others in bytes.
258+
*We avoid using%.*shere because it can misbehave if the data
259+
*is not valid in what libc thinks is the prevailing encoding.
260260
*/
261261
staticvoid
262262
fputnbytes(FILE*f,constchar*str,size_tn)

‎src/interfaces/ecpg/pgtypeslib/dt_common.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/pgtypeslib/dt_common.c,v 1.52 2010/05/08 16:39:52 tgl Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/pgtypeslib/dt_common.c,v 1.53 2010/05/09 02:15:59 tgl Exp $ */
22

33
#include"postgres_fe.h"
44

@@ -856,11 +856,9 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
856856
sprintf(str+strlen(str)," BC");
857857

858858
/*
859-
* Note: the uses of %.*s in this function would be unportable
860-
* if the timezone names ever contain non-ASCII characters,
861-
* since some platforms think the string length is measured
862-
* in characters not bytes. However, all TZ abbreviations in
863-
* the Olson database are plain ASCII.
859+
* Note: the uses of %.*s in this function would be risky if the
860+
* timezone names ever contain non-ASCII characters. However, all
861+
* TZ abbreviations in the Olson database are plain ASCII.
864862
*/
865863

866864
if (tzp!=NULL&&tm->tm_isdst >=0)

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

Lines changed: 3 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-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-misc.c,v 1.142 2010/05/0816:39:53 tgl Exp $
26+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-misc.c,v 1.143 2010/05/09 02:16:00 tgl Exp $
2727
*
2828
*-------------------------------------------------------------------------
2929
*/
@@ -70,8 +70,8 @@ static intpqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
7070
/*
7171
* fputnbytes: print exactly N bytes to a file
7272
*
73-
*Think not to use fprintf with a%.*sformat for this. Some machines
74-
*believe %s's precision is measured in characters, others in bytes.
73+
*We avoid using%.*shere because it can misbehave if the data
74+
*is not valid in what libc thinks is the prevailing encoding.
7575
*/
7676
staticvoid
7777
fputnbytes(FILE*f,constchar*str,size_tn)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp