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

Commitbd4b292

Browse files
committed
Use libc's snprintf, not sprintf, for special cases in snprintf.c.
snprintf.c has always fallen back on libc's *printf implementationwhen printing pointers (%p) and floats. When this code originated,we were still supporting some platforms that lacked native snprintf,so we used sprintf for that. That's not actually unsafe in our usage,but nonetheless builds on macOS are starting to complain about sprintfbeing unconditionally deprecated; and I wouldn't be surprised if otherplatforms follow suit. There seems little reason to believe that anyplatform supporting C99 wouldn't have standards-compliant snprintf,so let's just use that instead to suppress such warnings.Back-patch to v12, which is where we started to require C99. It'salso where we started to use our snprintf.c everywhere, so thiswouldn't be enough to suppress the warning in older branches anyway--- that is, in older branches these aren't necessarily all ourusages of libc's sprintf. It is enough in v12+ because anydeprecation annotation attached to libc's sprintf won't apply topg_sprintf. (Whether all our usages of pg_sprintf are adequatelysafe is not a matter I intend to address here, but perhaps it coulddo with some review.)Per report from Andres Freund and local testing.Discussion:https://postgr.es/m/20221015211955.q4cwbsfkyk3c4ty3@awork3.anarazel.de
1 parent16d11d6 commitbd4b292

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

‎src/port/snprintf.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,8 @@ fmtptr(const void *value, PrintfTarget *target)
10021002
intvallen;
10031003
charconvert[64];
10041004

1005-
/* we rely on regular C library'ssprintf to do the basic conversion */
1006-
vallen=sprintf(convert,"%p",value);
1005+
/* we rely on regular C library'ssnprintf to do the basic conversion */
1006+
vallen=snprintf(convert,sizeof(convert),"%p",value);
10071007
if (vallen<0)
10081008
target->failed= true;
10091009
else
@@ -1153,11 +1153,11 @@ fmtfloat(double value, char type, int forcesign, int leftjust,
11531153
intpadlen;/* amount to pad with spaces */
11541154

11551155
/*
1156-
* We rely on the regular C library'ssprintf to do the basic conversion,
1156+
* We rely on the regular C library'ssnprintf to do the basic conversion,
11571157
* then handle padding considerations here.
11581158
*
11591159
* The dynamic range of "double" is about 1E+-308 for IEEE math, and not
1160-
* too wildly more than that with other hardware. In "f" format,sprintf
1160+
* too wildly more than that with other hardware. In "f" format,snprintf
11611161
* could therefore generate at most 308 characters to the left of the
11621162
* decimal point; while we need to allow the precision to get as high as
11631163
* 308+17 to ensure that we don't truncate significant digits from very
@@ -1209,14 +1209,14 @@ fmtfloat(double value, char type, int forcesign, int leftjust,
12091209
fmt[2]='*';
12101210
fmt[3]=type;
12111211
fmt[4]='\0';
1212-
vallen=sprintf(convert,fmt,prec,value);
1212+
vallen=snprintf(convert,sizeof(convert),fmt,prec,value);
12131213
}
12141214
else
12151215
{
12161216
fmt[0]='%';
12171217
fmt[1]=type;
12181218
fmt[2]='\0';
1219-
vallen=sprintf(convert,fmt,value);
1219+
vallen=snprintf(convert,sizeof(convert),fmt,value);
12201220
}
12211221
if (vallen<0)
12221222
gotofail;
@@ -1345,7 +1345,7 @@ pg_strfromd(char *str, size_t count, int precision, double value)
13451345
fmt[2]='*';
13461346
fmt[3]='g';
13471347
fmt[4]='\0';
1348-
vallen=sprintf(convert,fmt,precision,value);
1348+
vallen=snprintf(convert,sizeof(convert),fmt,precision,value);
13491349
if (vallen<0)
13501350
{
13511351
target.failed= true;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp