Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1784f27

Browse files
committed
Replace remaining StrNCpy() by strlcpy()
They are equivalent, except that StrNCpy() zero-fills the entire destination buffer instead of providing just one trailing zero. For all but a tiny number of callers, that's just overhead rather than being desirable. Remove StrNCpy() as it is now unused. In some cases, namestrcpy() is the more appropriate function to use. While we're here, simplify the API of namestrcpy(): Remove the return value, don't check for NULL input. Nothing was using that anyway. Also, remove a few unused name-related functions. Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/flat/44f5e198-36f6-6cdb-7fa9-60e34784daae%402ndquadrant.com
1 parent cec57b1 commit 1784f27

File tree

20 files changed

+34
-106
lines changed

20 files changed

+34
-106
lines changed

contrib/pgcrypto/crypt-des.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ px_crypt_des(const char *key, const char *setting)
720720
if (des_setkey((char *) keybuf))
721721
return NULL;
722722
}
723-
StrNCpy(output, setting, 10);
723+
strlcpy(output, setting, 10);
724724

725725
/*
726726
* Double check that we weren't given a short setting. If we were, the

src/backend/access/transam/slru.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
252252
*/
253253
ctl->shared = shared;
254254
ctl->do_fsync = true; /* default behavior */
255-
StrNCpy(ctl->Dir, subdir, sizeof(ctl->Dir));
255+
strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
256256
}
257257

258258
/*

src/backend/access/transam/xlogarchive.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ ExecuteRecoveryCommand(const char *command, const char *commandName, bool failOn
323323
case 'r':
324324
/* %r: filename of last restartpoint */
325325
sp++;
326-
StrNCpy(dp, lastRestartPointFname, endp - dp);
326+
strlcpy(dp, lastRestartPointFname, endp - dp);
327327
dp += strlen(dp);
328328
break;
329329
case '%':

src/backend/catalog/pg_constraint.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ ChooseConstraintName(const char *name1, const char *name2,
484484
conDesc = table_open(ConstraintRelationId, AccessShareLock);
485485

486486
/* try the unmodified label first */
487-
StrNCpy(modlabel, label, sizeof(modlabel));
487+
strlcpy(modlabel, label, sizeof(modlabel));
488488

489489
for (;;)
490490
{

src/backend/commands/indexcmds.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ ChooseRelationName(const char *name1, const char *name2,
22462246
char modlabel[NAMEDATALEN];
22472247

22482248
/* try the unmodified label first */
2249-
StrNCpy(modlabel, label, sizeof(modlabel));
2249+
strlcpy(modlabel, label, sizeof(modlabel));
22502250

22512251
for (;;)
22522252
{

src/backend/commands/statscmds.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ ChooseExtendedStatisticName(const char *name1, const char *name2,
681681
char modlabel[NAMEDATALEN];
682682

683683
/* try the unmodified label first */
684-
StrNCpy(modlabel, label, sizeof(modlabel));
684+
strlcpy(modlabel, label, sizeof(modlabel));
685685

686686
for (;;)
687687
{

src/backend/commands/tablecmds.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
606606
* Truncate relname to appropriate length (probably a waste of time, as
607607
* parser should have done this already).
608608
*/
609-
StrNCpy(relname, stmt->relation->relname, NAMEDATALEN);
609+
strlcpy(relname, stmt->relation->relname, NAMEDATALEN);
610610

611611
/*
612612
* Check consistency of arguments

src/backend/postmaster/pgstat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4367,7 +4367,7 @@ pgstat_send_archiver(const char *xlog, bool failed)
43674367
*/
43684368
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ARCHIVER);
43694369
msg.m_failed = failed;
4370-
StrNCpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
4370+
strlcpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
43714371
msg.m_timestamp = GetCurrentTimestamp();
43724372
pgstat_send(&msg, sizeof(msg));
43734373
}

src/backend/replication/logical/logical.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "replication/snapbuild.h"
4040
#include "storage/proc.h"
4141
#include "storage/procarray.h"
42+
#include "utils/builtins.h"
4243
#include "utils/memutils.h"
4344

4445
/* data for errcontext callback */
@@ -288,6 +289,7 @@ CreateInitDecodingContext(const char *plugin,
288289
{
289290
TransactionId xmin_horizon = InvalidTransactionId;
290291
ReplicationSlot *slot;
292+
NameData plugin_name;
291293
LogicalDecodingContext *ctx;
292294
MemoryContext old_context;
293295

@@ -319,9 +321,14 @@ CreateInitDecodingContext(const char *plugin,
319321
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
320322
errmsg("cannot create logical replication slot in transaction that has performed writes")));
321323

322-
/* register output plugin name with slot */
324+
/*
325+
* Register output plugin name with slot. We need the mutex to avoid
326+
* concurrent reading of a partially copied string. But we don't want any
327+
* complicated code while holding a spinlock, so do namestrcpy() outside.
328+
*/
329+
namestrcpy(&plugin_name, plugin);
323330
SpinLockAcquire(&slot->mutex);
324-
StrNCpy(NameStr(slot->data.plugin), plugin, NAMEDATALEN);
331+
slot->data.plugin = plugin_name;
325332
SpinLockRelease(&slot->mutex);
326333

327334
if (XLogRecPtrIsInvalid(restart_lsn))

src/backend/replication/slot.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ ReplicationSlotCreate(const char *name, bool db_specific,
275275

276276
/* first initialize persistent data */
277277
memset(&slot->data, 0, sizeof(ReplicationSlotPersistentData));
278-
StrNCpy(NameStr(slot->data.name), name, NAMEDATALEN);
278+
namestrcpy(&slot->data.name, name);
279279
slot->data.database = db_specific ? MyDatabaseId : InvalidOid;
280280
slot->data.persistency = persistency;
281281

src/backend/utils/adt/formatting.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -3890,7 +3890,7 @@ DCH_cache_getnew(const char *str, bool std)
38903890
elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age);
38913891
#endif
38923892
old->valid = false;
3893-
StrNCpy(old->str, str, DCH_CACHE_SIZE + 1);
3893+
strlcpy(old->str, str, DCH_CACHE_SIZE + 1);
38943894
old->age = (++DCHCounter);
38953895
/* caller is expected to fill format, then set valid */
38963896
return old;
@@ -3904,7 +3904,7 @@ DCH_cache_getnew(const char *str, bool std)
39043904
DCHCache[n_DCHCache] = ent = (DCHCacheEntry *)
39053905
MemoryContextAllocZero(TopMemoryContext, sizeof(DCHCacheEntry));
39063906
ent->valid = false;
3907-
StrNCpy(ent->str, str, DCH_CACHE_SIZE + 1);
3907+
strlcpy(ent->str, str, DCH_CACHE_SIZE + 1);
39083908
ent->std = std;
39093909
ent->age = (++DCHCounter);
39103910
/* caller is expected to fill format, then set valid */
@@ -4799,7 +4799,7 @@ NUM_cache_getnew(const char *str)
47994799
elog(DEBUG_elog_output, "OLD: \"%s\" AGE: %d", old->str, old->age);
48004800
#endif
48014801
old->valid = false;
4802-
StrNCpy(old->str, str, NUM_CACHE_SIZE + 1);
4802+
strlcpy(old->str, str, NUM_CACHE_SIZE + 1);
48034803
old->age = (++NUMCounter);
48044804
/* caller is expected to fill format and Num, then set valid */
48054805
return old;
@@ -4813,7 +4813,7 @@ NUM_cache_getnew(const char *str)
48134813
NUMCache[n_NUMCache] = ent = (NUMCacheEntry *)
48144814
MemoryContextAllocZero(TopMemoryContext, sizeof(NUMCacheEntry));
48154815
ent->valid = false;
4816-
StrNCpy(ent->str, str, NUM_CACHE_SIZE + 1);
4816+
strlcpy(ent->str, str, NUM_CACHE_SIZE + 1);
48174817
ent->age = (++NUMCounter);
48184818
/* caller is expected to fill format and Num, then set valid */
48194819
++n_NUMCache;

src/backend/utils/adt/name.c

+4-44
Original file line numberDiff line numberDiff line change
@@ -229,53 +229,13 @@ btnamesortsupport(PG_FUNCTION_ARGS)
229229
* MISCELLANEOUS PUBLIC ROUTINES *
230230
*****************************************************************************/
231231

232-
int
233-
namecpy(Name n1, const NameData *n2)
234-
{
235-
if (!n1 || !n2)
236-
return -1;
237-
StrNCpy(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
238-
return 0;
239-
}
240-
241-
#ifdef NOT_USED
242-
int
243-
namecat(Name n1, Name n2)
244-
{
245-
return namestrcat(n1, NameStr(*n2)); /* n2 can't be any longer than n1 */
246-
}
247-
#endif
248-
249-
int
232+
void
250233
namestrcpy(Name name, const char *str)
251234
{
252-
if (!name || !str)
253-
return -1;
254-
StrNCpy(NameStr(*name), str, NAMEDATALEN);
255-
return 0;
256-
}
257-
258-
#ifdef NOT_USED
259-
int
260-
namestrcat(Name name, const char *str)
261-
{
262-
int i;
263-
char *p,
264-
*q;
265-
266-
if (!name || !str)
267-
return -1;
268-
for (i = 0, p = NameStr(*name); i < NAMEDATALEN && *p; ++i, ++p)
269-
;
270-
for (q = str; i < NAMEDATALEN; ++i, ++p, ++q)
271-
{
272-
*p = *q;
273-
if (!*q)
274-
break;
275-
}
276-
return 0;
235+
/* NB: We need to zero-pad the destination. */
236+
strncpy(NameStr(*name), str, NAMEDATALEN);
237+
NameStr(*name)[NAMEDATALEN-1] = '\0';
277238
}
278-
#endif
279239

280240
/*
281241
* Compare a NAME to a C string

src/backend/utils/adt/pg_locale.c

-9
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,7 @@
7575
#endif
7676

7777
#ifdef WIN32
78-
/*
79-
* This Windows file defines StrNCpy. We don't need it here, so we undefine
80-
* it to keep the compiler quiet, and undefine it again after the file is
81-
* included, so we don't accidentally use theirs.
82-
*/
83-
#undef StrNCpy
8478
#include <shlwapi.h>
85-
#ifdef StrNCpy
86-
#undef StrNCpy
87-
#endif
8879
#endif
8980

9081
#define MAX_L10N_DATA 80

src/backend/utils/adt/ruleutils.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,7 @@ pg_get_userbyid(PG_FUNCTION_ARGS)
24892489
if (HeapTupleIsValid(roletup))
24902490
{
24912491
role_rec = (Form_pg_authid) GETSTRUCT(roletup);
2492-
StrNCpy(NameStr(*result), NameStr(role_rec->rolname), NAMEDATALEN);
2492+
*result = role_rec->rolname;
24932493
ReleaseSysCache(roletup);
24942494
}
24952495
else

src/common/exec.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ find_my_exec(const char *argv0, char *retpath)
144144
if (first_dir_separator(argv0) != NULL)
145145
{
146146
if (is_absolute_path(argv0))
147-
StrNCpy(retpath, argv0, MAXPGPATH);
147+
strlcpy(retpath, argv0, MAXPGPATH);
148148
else
149149
join_path_components(retpath, cwd, argv0);
150150
canonicalize_path(retpath);
@@ -184,7 +184,7 @@ find_my_exec(const char *argv0, char *retpath)
184184
if (!endp)
185185
endp = startp + strlen(startp); /* point to end */
186186

187-
StrNCpy(test_path, startp, Min(endp - startp + 1, MAXPGPATH));
187+
strlcpy(test_path, startp, Min(endp - startp + 1, MAXPGPATH));
188188

189189
if (is_absolute_path(test_path))
190190
join_path_components(retpath, test_path, argv0);

src/include/c.h

-29
Original file line numberDiff line numberDiff line change
@@ -932,35 +932,6 @@ extern void ExceptionalCondition(const char *conditionName,
932932
*/
933933
#define Abs(x) ((x) >= 0 ? (x) : -(x))
934934

935-
/*
936-
* StrNCpy
937-
* Like standard library function strncpy(), except that result string
938-
* is guaranteed to be null-terminated --- that is, at most N-1 bytes
939-
* of the source string will be kept.
940-
* Also, the macro returns no result (too hard to do that without
941-
* evaluating the arguments multiple times, which seems worse).
942-
*
943-
* BTW: when you need to copy a non-null-terminated string (like a text
944-
* datum) and add a null, do not do it with StrNCpy(..., len+1). That
945-
* might seem to work, but it fetches one byte more than there is in the
946-
* text object. One fine day you'll have a SIGSEGV because there isn't
947-
* another byte before the end of memory. Don't laugh, we've had real
948-
* live bug reports from real live users over exactly this mistake.
949-
* Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
950-
*/
951-
#define StrNCpy(dst,src,len) \
952-
do \
953-
{ \
954-
char * _dst = (dst); \
955-
Size _len = (len); \
956-
\
957-
if (_len > 0) \
958-
{ \
959-
strncpy(_dst, (src), _len); \
960-
_dst[_len-1] = '\0'; \
961-
} \
962-
} while (0)
963-
964935

965936
/* Get a bit mask of the bits set in non-long aligned addresses */
966937
#define LONG_ALIGN_MASK (sizeof(long) - 1)

src/include/utils/builtins.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ extern uint64 hex_decode(const char *src, size_t len, char *dst);
3939
extern int2vector *buildint2vector(const int16 *int2s, int n);
4040

4141
/* name.c */
42-
extern int namecpy(Name n1, const NameData *n2);
43-
extern int namestrcpy(Name name, const char *str);
42+
extern void namestrcpy(Name name, const char *str);
4443
extern int namestrcmp(Name name, const char *str);
4544

4645
/* numutils.c */

src/interfaces/ecpg/pgtypeslib/dt_common.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm *tm, char **tzn)
10151015
* Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
10161016
* contains an error message, which doesn't fit in the buffer
10171017
*/
1018-
StrNCpy(*tzn, tm->tm_zone, MAXTZLEN + 1);
1018+
strlcpy(*tzn, tm->tm_zone, MAXTZLEN + 1);
10191019
if (strlen(tm->tm_zone) > MAXTZLEN)
10201020
tm->tm_isdst = -1;
10211021
}
@@ -1033,7 +1033,7 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm *tm, char **tzn)
10331033
* Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
10341034
* contains an error message, which doesn't fit in the buffer
10351035
*/
1036-
StrNCpy(*tzn, TZNAME_GLOBAL[tm->tm_isdst], MAXTZLEN + 1);
1036+
strlcpy(*tzn, TZNAME_GLOBAL[tm->tm_isdst], MAXTZLEN + 1);
10371037
if (strlen(TZNAME_GLOBAL[tm->tm_isdst]) > MAXTZLEN)
10381038
tm->tm_isdst = -1;
10391039
}

src/interfaces/ecpg/test/pg_regress_ecpg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ecpg_filter(const char *sourcefile, const char *outfile)
6363
if (plen > 1)
6464
{
6565
n = (char *) malloc(plen);
66-
StrNCpy(n, p + 1, plen);
66+
strlcpy(n, p + 1, plen);
6767
replace_string(linebuf, n, "");
6868
}
6969
}

src/test/modules/ssl_passphrase_callback/ssl_passphrase_func.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ rot13_passphrase(char *buf, int size, int rwflag, void *userdata)
7474
{
7575

7676
Assert(ssl_passphrase != NULL);
77-
StrNCpy(buf, ssl_passphrase, size);
77+
strlcpy(buf, ssl_passphrase, size);
7878
for (char *p = buf; *p; p++)
7979
{
8080
char c = *p;

0 commit comments

Comments
 (0)