]> granicus.if.org Git - postgresql/commit
Replace a bunch more uses of strncpy() with safer coding.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 24 Jan 2015 18:05:42 +0000 (13:05 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 24 Jan 2015 18:05:42 +0000 (13:05 -0500)
commit586dd5d6a5d59e406bc8032bb52625ffb904311c
treeb47c5e9038b75bd100e507f8ba3c7dc92e50d603
parent9222cd84b0f227287f65df395d52dc7973a62d29
Replace a bunch more uses of strncpy() with safer coding.

strncpy() has a well-deserved reputation for being unsafe, so make an
effort to get rid of nearly all occurrences in HEAD.

A large fraction of the remaining uses were passing length less than or
equal to the known strlen() of the source, in which case no null-padding
can occur and the behavior is equivalent to memcpy(), though doubtless
slower and certainly harder to reason about.  So just use memcpy() in
these cases.

In other cases, use either StrNCpy() or strlcpy() as appropriate (depending
on whether padding to the full length of the destination buffer seems
useful).

I left a few strncpy() calls alone in the src/timezone/ code, to keep it
in sync with upstream (the IANA tzcode distribution).  There are also a
few such calls in ecpg that could possibly do with more analysis.

AFAICT, none of these changes are more than cosmetic, except for the four
occurrences in fe-secure-openssl.c, which are in fact buggy: an overlength
source leads to a non-null-terminated destination buffer and ensuing
misbehavior.  These don't seem like security issues, first because no stack
clobber is possible and second because if your values of sslcert etc are
coming from untrusted sources then you've got problems way worse than this.
Still, it's undesirable to have unpredictable behavior for overlength
inputs, so back-patch those four changes to all active branches.
26 files changed:
contrib/fuzzystrmatch/dmetaphone.c
contrib/isn/isn.c
contrib/pg_trgm/trgm_regexp.c
contrib/pgbench/pgbench.c
contrib/pgcrypto/crypt-des.c
contrib/xml2/xpath.c
src/backend/libpq/hba.c
src/backend/postmaster/pgstat.c
src/backend/regex/regerror.c
src/backend/replication/logical/logical.c
src/backend/replication/slot.c
src/backend/utils/adt/datetime.c
src/backend/utils/adt/name.c
src/backend/utils/adt/varlena.c
src/backend/utils/error/elog.c
src/bin/initdb/initdb.c
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_backup_db.c
src/interfaces/ecpg/ecpglib/execute.c
src/interfaces/ecpg/ecpglib/prepare.c
src/interfaces/ecpg/pgtypeslib/datetime.c
src/interfaces/ecpg/pgtypeslib/dt_common.c
src/interfaces/ecpg/test/pg_regress_ecpg.c
src/interfaces/libpq/fe-exec.c
src/interfaces/libpq/fe-protocol2.c
src/interfaces/libpq/fe-secure-openssl.c