if (des_setkey((char *) keybuf))
return NULL;
}
- StrNCpy(output, setting, 10);
+ strlcpy(output, setting, 10);
/*
* Double check that we weren't given a short setting. If we were, the
*/
ctl->shared = shared;
ctl->do_fsync = true; /* default behavior */
- StrNCpy(ctl->Dir, subdir, sizeof(ctl->Dir));
+ strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
}
/*
case 'r':
/* %r: filename of last restartpoint */
sp++;
- StrNCpy(dp, lastRestartPointFname, endp - dp);
+ strlcpy(dp, lastRestartPointFname, endp - dp);
dp += strlen(dp);
break;
case '%':
conDesc = table_open(ConstraintRelationId, AccessShareLock);
/* try the unmodified label first */
- StrNCpy(modlabel, label, sizeof(modlabel));
+ strlcpy(modlabel, label, sizeof(modlabel));
for (;;)
{
char modlabel[NAMEDATALEN];
/* try the unmodified label first */
- StrNCpy(modlabel, label, sizeof(modlabel));
+ strlcpy(modlabel, label, sizeof(modlabel));
for (;;)
{
char modlabel[NAMEDATALEN];
/* try the unmodified label first */
- StrNCpy(modlabel, label, sizeof(modlabel));
+ strlcpy(modlabel, label, sizeof(modlabel));
for (;;)
{
* Truncate relname to appropriate length (probably a waste of time, as
* parser should have done this already).
*/
- StrNCpy(relname, stmt->relation->relname, NAMEDATALEN);
+ strlcpy(relname, stmt->relation->relname, NAMEDATALEN);
/*
* Check consistency of arguments
*/
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ARCHIVER);
msg.m_failed = failed;
- StrNCpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
+ strlcpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
msg.m_timestamp = GetCurrentTimestamp();
pgstat_send(&msg, sizeof(msg));
}
#include "replication/snapbuild.h"
#include "storage/proc.h"
#include "storage/procarray.h"
+#include "utils/builtins.h"
#include "utils/memutils.h"
/* data for errcontext callback */
{
TransactionId xmin_horizon = InvalidTransactionId;
ReplicationSlot *slot;
+ NameData plugin_name;
LogicalDecodingContext *ctx;
MemoryContext old_context;
(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
errmsg("cannot create logical replication slot in transaction that has performed writes")));
- /* register output plugin name with slot */
+ /*
+ * Register output plugin name with slot. We need the mutex to avoid
+ * concurrent reading of a partially copied string. But we don't want any
+ * complicated code while holding a spinlock, so do namestrcpy() outside.
+ */
+ namestrcpy(&plugin_name, plugin);
SpinLockAcquire(&slot->mutex);
- StrNCpy(NameStr(slot->data.plugin), plugin, NAMEDATALEN);
+ slot->data.plugin = plugin_name;
SpinLockRelease(&slot->mutex);
if (XLogRecPtrIsInvalid(restart_lsn))
/* first initialize persistent data */
memset(&slot->data, 0, sizeof(ReplicationSlotPersistentData));
- StrNCpy(NameStr(slot->data.name), name, NAMEDATALEN);
+ namestrcpy(&slot->data.name, name);
slot->data.database = db_specific ? MyDatabaseId : InvalidOid;
slot->data.persistency = persistency;
elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age);
#endif
old->valid = false;
- StrNCpy(old->str, str, DCH_CACHE_SIZE + 1);
+ strlcpy(old->str, str, DCH_CACHE_SIZE + 1);
old->age = (++DCHCounter);
/* caller is expected to fill format, then set valid */
return old;
DCHCache[n_DCHCache] = ent = (DCHCacheEntry *)
MemoryContextAllocZero(TopMemoryContext, sizeof(DCHCacheEntry));
ent->valid = false;
- StrNCpy(ent->str, str, DCH_CACHE_SIZE + 1);
+ strlcpy(ent->str, str, DCH_CACHE_SIZE + 1);
ent->std = std;
ent->age = (++DCHCounter);
/* caller is expected to fill format, then set valid */
elog(DEBUG_elog_output, "OLD: \"%s\" AGE: %d", old->str, old->age);
#endif
old->valid = false;
- StrNCpy(old->str, str, NUM_CACHE_SIZE + 1);
+ strlcpy(old->str, str, NUM_CACHE_SIZE + 1);
old->age = (++NUMCounter);
/* caller is expected to fill format and Num, then set valid */
return old;
NUMCache[n_NUMCache] = ent = (NUMCacheEntry *)
MemoryContextAllocZero(TopMemoryContext, sizeof(NUMCacheEntry));
ent->valid = false;
- StrNCpy(ent->str, str, NUM_CACHE_SIZE + 1);
+ strlcpy(ent->str, str, NUM_CACHE_SIZE + 1);
ent->age = (++NUMCounter);
/* caller is expected to fill format and Num, then set valid */
++n_NUMCache;
* MISCELLANEOUS PUBLIC ROUTINES *
*****************************************************************************/
-int
-namecpy(Name n1, const NameData *n2)
-{
- if (!n1 || !n2)
- return -1;
- StrNCpy(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
- return 0;
-}
-
-#ifdef NOT_USED
-int
-namecat(Name n1, Name n2)
-{
- return namestrcat(n1, NameStr(*n2)); /* n2 can't be any longer than n1 */
-}
-#endif
-
-int
+void
namestrcpy(Name name, const char *str)
{
- if (!name || !str)
- return -1;
- StrNCpy(NameStr(*name), str, NAMEDATALEN);
- return 0;
-}
-
-#ifdef NOT_USED
-int
-namestrcat(Name name, const char *str)
-{
- int i;
- char *p,
- *q;
-
- if (!name || !str)
- return -1;
- for (i = 0, p = NameStr(*name); i < NAMEDATALEN && *p; ++i, ++p)
- ;
- for (q = str; i < NAMEDATALEN; ++i, ++p, ++q)
- {
- *p = *q;
- if (!*q)
- break;
- }
- return 0;
+ /* NB: We need to zero-pad the destination. */
+ strncpy(NameStr(*name), str, NAMEDATALEN);
+ NameStr(*name)[NAMEDATALEN-1] = '\0';
}
-#endif
/*
* Compare a NAME to a C string
#endif
#ifdef WIN32
-/*
- * This Windows file defines StrNCpy. We don't need it here, so we undefine
- * it to keep the compiler quiet, and undefine it again after the file is
- * included, so we don't accidentally use theirs.
- */
-#undef StrNCpy
#include <shlwapi.h>
-#ifdef StrNCpy
-#undef StrNCpy
-#endif
#endif
#define MAX_L10N_DATA 80
if (HeapTupleIsValid(roletup))
{
role_rec = (Form_pg_authid) GETSTRUCT(roletup);
- StrNCpy(NameStr(*result), NameStr(role_rec->rolname), NAMEDATALEN);
+ *result = role_rec->rolname;
ReleaseSysCache(roletup);
}
else
if (first_dir_separator(argv0) != NULL)
{
if (is_absolute_path(argv0))
- StrNCpy(retpath, argv0, MAXPGPATH);
+ strlcpy(retpath, argv0, MAXPGPATH);
else
join_path_components(retpath, cwd, argv0);
canonicalize_path(retpath);
if (!endp)
endp = startp + strlen(startp); /* point to end */
- StrNCpy(test_path, startp, Min(endp - startp + 1, MAXPGPATH));
+ strlcpy(test_path, startp, Min(endp - startp + 1, MAXPGPATH));
if (is_absolute_path(test_path))
join_path_components(retpath, test_path, argv0);
*/
#define Abs(x) ((x) >= 0 ? (x) : -(x))
-/*
- * StrNCpy
- * Like standard library function strncpy(), except that result string
- * is guaranteed to be null-terminated --- that is, at most N-1 bytes
- * of the source string will be kept.
- * Also, the macro returns no result (too hard to do that without
- * evaluating the arguments multiple times, which seems worse).
- *
- * BTW: when you need to copy a non-null-terminated string (like a text
- * datum) and add a null, do not do it with StrNCpy(..., len+1). That
- * might seem to work, but it fetches one byte more than there is in the
- * text object. One fine day you'll have a SIGSEGV because there isn't
- * another byte before the end of memory. Don't laugh, we've had real
- * live bug reports from real live users over exactly this mistake.
- * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
- */
-#define StrNCpy(dst,src,len) \
- do \
- { \
- char * _dst = (dst); \
- Size _len = (len); \
-\
- if (_len > 0) \
- { \
- strncpy(_dst, (src), _len); \
- _dst[_len-1] = '\0'; \
- } \
- } while (0)
-
/* Get a bit mask of the bits set in non-long aligned addresses */
#define LONG_ALIGN_MASK (sizeof(long) - 1)
extern int2vector *buildint2vector(const int16 *int2s, int n);
/* name.c */
-extern int namecpy(Name n1, const NameData *n2);
-extern int namestrcpy(Name name, const char *str);
+extern void namestrcpy(Name name, const char *str);
extern int namestrcmp(Name name, const char *str);
/* numutils.c */
* Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
* contains an error message, which doesn't fit in the buffer
*/
- StrNCpy(*tzn, tm->tm_zone, MAXTZLEN + 1);
+ strlcpy(*tzn, tm->tm_zone, MAXTZLEN + 1);
if (strlen(tm->tm_zone) > MAXTZLEN)
tm->tm_isdst = -1;
}
* Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
* contains an error message, which doesn't fit in the buffer
*/
- StrNCpy(*tzn, TZNAME_GLOBAL[tm->tm_isdst], MAXTZLEN + 1);
+ strlcpy(*tzn, TZNAME_GLOBAL[tm->tm_isdst], MAXTZLEN + 1);
if (strlen(TZNAME_GLOBAL[tm->tm_isdst]) > MAXTZLEN)
tm->tm_isdst = -1;
}
if (plen > 1)
{
n = (char *) malloc(plen);
- StrNCpy(n, p + 1, plen);
+ strlcpy(n, p + 1, plen);
replace_string(linebuf, n, "");
}
}
{
Assert(ssl_passphrase != NULL);
- StrNCpy(buf, ssl_passphrase, size);
+ strlcpy(buf, ssl_passphrase, size);
for (char *p = buf; *p; p++)
{
char c = *p;