1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Do not pass NULL to memcpy() in Buffer/String objects.

glibc and others seem tolerant of this but the behavior is undefined.

Found with -fsanitize=undefined.
This commit is contained in:
David Steele 2022-03-24 09:32:18 -06:00
parent 98792b1b0c
commit ccbe2a1f70
4 changed files with 11 additions and 2 deletions

View File

@ -112,7 +112,10 @@ bufDup(const Buffer *buffer)
// Create object and copy data
Buffer *this = bufNew(bufUsed(buffer));
if (bufUsed(buffer) != 0)
memcpy(this->pub.buffer, buffer->pub.buffer, bufSize(this));
this->pub.used = bufSize(this);
FUNCTION_TEST_RETURN(this);

View File

@ -173,7 +173,9 @@ strNewBuf(const Buffer *buffer)
String *this = strNewFixed(bufUsed(buffer));
// Assign string
if (strSize(this) != 0)
memcpy(this->pub.buffer, bufPtrConst(buffer), strSize(this));
this->pub.buffer[strSize(this)] = 0;
FUNCTION_TEST_RETURN(this);

View File

@ -29,6 +29,8 @@ testRun(void)
TEST_ASSIGN(buffer, bufNewC("TEST-STR", sizeof("TEST-STR") - 1), "new buffer from string");
TEST_RESULT_BOOL(memcmp(bufPtr(buffer), "TEST-STR", 8) == 0, true, "check buffer");
TEST_RESULT_UINT(bufSize(bufDup(bufNew(0))), 0, "duplicate empty buffer");
TEST_RESULT_VOID(bufFree(buffer), "free buffer");
TEST_RESULT_VOID(bufFree(bufNew(0)), "free empty buffer");

View File

@ -49,6 +49,8 @@ testRun(void)
TEST_RESULT_UINT(strlen(strZ(string)), 13, "check size with strlen()");
TEST_RESULT_INT(strZNull(string)[2], 'a', "check character");
TEST_RESULT_UINT(strSize(strNewBuf(bufNew(0))), 0, "new string from empty buffer");
TEST_RESULT_VOID(strFree(string), "free string");
// -------------------------------------------------------------------------------------------------------------------------