1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-02-21 19:48:29 +02:00

Use memcpy() instead of strncpy when source size is known.

In this case the destination will be large enough to hold the source so memcpy is more efficient.

Also, in highly optimized builds the compiler may warn for strncpy() when it can see that the source size won't include the terminator.
This commit is contained in:
David Steele 2022-12-31 15:49:32 +07:00
parent 2332ce8ffc
commit 4a64c5d80c
3 changed files with 8 additions and 8 deletions

View File

@ -262,7 +262,7 @@ cvtZSubNToIntBase(const char *const value, const size_t offset, const size_t siz
char buffer[CVT_BASE10_BUFFER_SIZE + 1];
ASSERT(size <= CVT_BASE10_BUFFER_SIZE);
strncpy(buffer, value + offset, size);
memcpy(buffer, value + offset, size);
buffer[size] = '\0';
FUNCTION_TEST_RETURN(INT, cvtZToIntBase(buffer, base));
@ -326,7 +326,7 @@ cvtZSubNToInt64Base(const char *const value, const size_t offset, const size_t s
char buffer[CVT_BASE10_BUFFER_SIZE + 1];
ASSERT(size <= CVT_BASE10_BUFFER_SIZE);
strncpy(buffer, value + offset, size);
memcpy(buffer, value + offset, size);
buffer[size] = '\0';
FUNCTION_TEST_RETURN(INT64, cvtZToInt64Base(buffer, base));
@ -469,7 +469,7 @@ cvtZSubNToUIntBase(const char *const value, const size_t offset, const size_t si
char buffer[CVT_BASE10_BUFFER_SIZE + 1];
ASSERT(size <= CVT_BASE10_BUFFER_SIZE);
strncpy(buffer, value + offset, size);
memcpy(buffer, value + offset, size);
buffer[size] = '\0';
FUNCTION_TEST_RETURN(UINT, cvtZToUIntBase(buffer, base));
@ -539,7 +539,7 @@ cvtZSubNToUInt64Base(const char *const value, const size_t offset, const size_t
char buffer[CVT_BASE10_BUFFER_SIZE + 1];
ASSERT(size <= CVT_BASE10_BUFFER_SIZE);
strncpy(buffer, value + offset, size);
memcpy(buffer, value + offset, size);
buffer[size] = '\0';
FUNCTION_TEST_RETURN(UINT64, cvtZToUInt64Base(buffer, base));

View File

@ -163,7 +163,7 @@ strNewZ(const char *const string)
String *this = strNewFixed(strlen(string));
// Assign string
strncpy(this->pub.buffer, string, strSize(this));
memcpy(this->pub.buffer, string, strSize(this));
this->pub.buffer[strSize(this)] = '\0';
FUNCTION_TEST_RETURN(STRING, this);
@ -270,7 +270,7 @@ strNewZN(const char *string, size_t size)
String *this = strNewFixed(size);
// Assign string
strncpy(this->pub.buffer, string, strSize(this));
memcpy(this->pub.buffer, string, strSize(this));
this->pub.buffer[strSize(this)] = 0;
FUNCTION_TEST_RETURN(STRING, this);
@ -442,7 +442,7 @@ strCatZN(String *this, const char *cat, size_t size)
strResize(this, size);
// Append the string
strncpy(this->pub.buffer + strSize(this), cat, size);
memcpy(this->pub.buffer + strSize(this), cat, size);
this->pub.buffer[strSize(this) + size] = '\0';
// Update size/extra

View File

@ -868,7 +868,7 @@ varNewStr(const String *data)
};
// Assign the string
strncpy(pubData->buffer, strZ(data), strSize(data));
memcpy(pubData->buffer, strZ(data), strSize(data));
pubData->buffer[strSize(data)] = '\0';
}
}