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

Commit586dd5d

Browse files
committed
Replace a bunch more uses of strncpy() with safer coding.
strncpy() has a well-deserved reputation for being unsafe, so make aneffort to get rid of nearly all occurrences in HEAD.A large fraction of the remaining uses were passing length less than orequal to the known strlen() of the source, in which case no null-paddingcan occur and the behavior is equivalent to memcpy(), though doubtlessslower and certainly harder to reason about. So just use memcpy() inthese cases.In other cases, use either StrNCpy() or strlcpy() as appropriate (dependingon whether padding to the full length of the destination buffer seemsuseful).I left a few strncpy() calls alone in the src/timezone/ code, to keep itin sync with upstream (the IANA tzcode distribution). There are also afew such calls in ecpg that could possibly do with more analysis.AFAICT, none of these changes are more than cosmetic, except for the fouroccurrences in fe-secure-openssl.c, which are in fact buggy: an overlengthsource leads to a non-null-terminated destination buffer and ensuingmisbehavior. These don't seem like security issues, first because no stackclobber is possible and second because if your values of sslcert etc arecoming from untrusted sources then you've got problems way worse than this.Still, it's undesirable to have unpredictable behavior for overlengthinputs, so back-patch those four changes to all active branches.
1 parent9222cd8 commit586dd5d

File tree

26 files changed

+49
-50
lines changed

26 files changed

+49
-50
lines changed

‎contrib/fuzzystrmatch/dmetaphone.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ NewMetaString(char *init_str)
247247
META_MALLOC(s->str,s->bufsize,char);
248248
assert(s->str!=NULL);
249249

250-
strncpy(s->str,init_str,s->length+1);
250+
memcpy(s->str,init_str,s->length+1);
251251
s->free_string_on_destroy=1;
252252

253253
returns;

‎contrib/isn/isn.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,18 +825,18 @@ string2ean(const char *str, bool errorOK, ean13 *result,
825825
gotoeanwrongtype;
826826
break;
827827
caseISMN:
828-
strncpy(buf,"9790",4);/* this isn't for sure yet, for now
828+
memcpy(buf,"9790",4);/* this isn't for sure yet, for now
829829
* ISMN it's only 9790 */
830830
valid= (valid&& ((rcheck=checkdig(buf,13))==check||magic));
831831
break;
832832
caseISBN:
833-
strncpy(buf,"978",3);
833+
memcpy(buf,"978",3);
834834
valid= (valid&& ((rcheck=weight_checkdig(buf+3,10))==check||magic));
835835
break;
836836
caseISSN:
837-
strncpy(buf+10,"00",2);/* append 00 as the normal issue
837+
memcpy(buf+10,"00",2);/* append 00 as the normal issue
838838
* publication code */
839-
strncpy(buf,"977",3);
839+
memcpy(buf,"977",3);
840840
valid= (valid&& ((rcheck=weight_checkdig(buf+3,8))==check||magic));
841841
break;
842842
caseUPC:

‎contrib/pg_trgm/trgm_regexp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ convertPgWchar(pg_wchar c, trgm_mb_char *result)
877877
#endif
878878

879879
/* Fill result with exactly MAX_MULTIBYTE_CHAR_LEN bytes */
880-
strncpy(result->bytes,s,MAX_MULTIBYTE_CHAR_LEN);
880+
memcpy(result->bytes,s,MAX_MULTIBYTE_CHAR_LEN);
881881
return true;
882882
}
883883

‎contrib/pgbench/pgbench.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ replaceVariable(char **sql, char *param, int len, char *value)
829829

830830
if (valueln!=len)
831831
memmove(param+valueln,param+len,strlen(param+len)+1);
832-
strncpy(param,value,valueln);
832+
memcpy(param,value,valueln);
833833

834834
returnparam+valueln;
835835
}

‎contrib/pgcrypto/crypt-des.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,15 +708,14 @@ px_crypt_des(const char *key, const char *setting)
708708
if (des_setkey((char*)keybuf))
709709
return (NULL);
710710
}
711-
strncpy(output,setting,9);
711+
StrNCpy(output,setting,10);
712712

713713
/*
714714
* Double check that we weren't given a short setting. If we were, the
715715
* above code will probably have created weird values for count and
716716
* salt, but we don't really care. Just make sure the output string
717717
* doesn't have an extra NUL in it.
718718
*/
719-
output[9]='\0';
720719
p=output+strlen(output);
721720
}
722721
else

‎contrib/xml2/xpath.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ xpath_string(PG_FUNCTION_ARGS)
327327
/* We could try casting to string using the libxml function? */
328328

329329
xpath= (xmlChar*)palloc(pathsize+9);
330-
strncpy((char*)xpath,"string(",7);
330+
memcpy((char*)xpath,"string(",7);
331331
memcpy((char*) (xpath+7),VARDATA(xpathsupp),pathsize);
332332
xpath[pathsize+7]=')';
333333
xpath[pathsize+8]='\0';

‎src/backend/libpq/hba.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,8 @@ check_ident_usermap(IdentLine *identLine, const char *usermap_name,
19961996

19971997
if ((ofs=strstr(identLine->pg_role,"\\1"))!=NULL)
19981998
{
1999+
intoffset;
2000+
19992001
/* substitution of the first argument requested */
20002002
if (matches[1].rm_so<0)
20012003
{
@@ -2012,8 +2014,9 @@ check_ident_usermap(IdentLine *identLine, const char *usermap_name,
20122014
* plus null terminator
20132015
*/
20142016
regexp_pgrole=palloc0(strlen(identLine->pg_role)-2+ (matches[1].rm_eo-matches[1].rm_so)+1);
2015-
strncpy(regexp_pgrole,identLine->pg_role, (ofs-identLine->pg_role));
2016-
memcpy(regexp_pgrole+strlen(regexp_pgrole),
2017+
offset=ofs-identLine->pg_role;
2018+
memcpy(regexp_pgrole,identLine->pg_role,offset);
2019+
memcpy(regexp_pgrole+offset,
20172020
ident_user+matches[1].rm_so,
20182021
matches[1].rm_eo-matches[1].rm_so);
20192022
strcat(regexp_pgrole,ofs+2);

‎src/backend/postmaster/pgstat.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,7 @@ pgstat_send_archiver(const char *xlog, bool failed)
30953095
*/
30963096
pgstat_setheader(&msg.m_hdr,PGSTAT_MTYPE_ARCHIVER);
30973097
msg.m_failed=failed;
3098-
strncpy(msg.m_xlog,xlog,sizeof(msg.m_xlog));
3098+
StrNCpy(msg.m_xlog,xlog,sizeof(msg.m_xlog));
30993099
msg.m_timestamp=GetCurrentTimestamp();
31003100
pgstat_send(&msg,sizeof(msg));
31013101
}

‎src/backend/regex/regerror.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pg_regerror(int errcode,/* error code, or REG_ATOI or REG_ITOA */
111111
strcpy(errbuf,msg);
112112
else
113113
{/* truncate to fit */
114-
strncpy(errbuf,msg,errbuf_size-1);
114+
memcpy(errbuf,msg,errbuf_size-1);
115115
errbuf[errbuf_size-1]='\0';
116116
}
117117
}

‎src/backend/replication/logical/logical.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,7 @@ CreateInitDecodingContext(char *plugin,
244244

245245
/* register output plugin name with slot */
246246
SpinLockAcquire(&slot->mutex);
247-
strncpy(NameStr(slot->data.plugin),plugin,
248-
NAMEDATALEN);
249-
NameStr(slot->data.plugin)[NAMEDATALEN-1]='\0';
247+
StrNCpy(NameStr(slot->data.plugin),plugin,NAMEDATALEN);
250248
SpinLockRelease(&slot->mutex);
251249

252250
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp