You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-07-15 01:04:37 +02:00
Use designated initializers to initialize structs.
Previously memNew() used memset() to initialize all struct members to 0, NULL, false, etc. While this appears to work in practice, it is a violation of the C specification. For instance, NULL == 0 must be true but neither NULL nor 0 must be represented with all zero bits. Instead use designated initializers to initialize structs. These guarantee that struct members will be properly initialized even if they are not specified in the initializer. Note that due to a quirk in the C99 specification at least one member must be explicitly initialized even if it needs to be the default value. Since pre-zeroed memory is no longer required, adjust memAllocInternal()/memReallocInternal() to return raw memory and update dependent functions accordingly. All instances of memset() have been removed except in debug/test code where needed. Add memMewPtrArray() to allocate an array of pointers and automatically set all pointers to NULL. Rename memGrowRaw() to the more logical memResize().
This commit is contained in:
@ -56,17 +56,17 @@ testRun(void)
|
||||
|
||||
// Test if the buffer was overrun
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
size_t bufferSize = 256;
|
||||
unsigned char *buffer = memNew(bufferSize + 1);
|
||||
unsigned char buffer[256] = {0};
|
||||
|
||||
cryptoRandomBytes(buffer, bufferSize);
|
||||
TEST_RESULT_BOOL(buffer[bufferSize] == 0, true, "check that buffer did not overrun (though random byte could be 0)");
|
||||
cryptoRandomBytes(buffer, sizeof(buffer) - 1);
|
||||
TEST_RESULT_BOOL(
|
||||
buffer[sizeof(buffer) - 1] == 0, true, "check that buffer did not overrun (though random byte could be 0)");
|
||||
|
||||
// Count bytes that are not zero (there shouldn't be all zeroes)
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
int nonZeroTotal = 0;
|
||||
|
||||
for (unsigned int charIdx = 0; charIdx < bufferSize; charIdx++)
|
||||
for (unsigned int charIdx = 0; charIdx < sizeof(buffer) - 1; charIdx++)
|
||||
if (buffer[charIdx] != 0) // {uncoverable_branch - ok if there are no zeros}
|
||||
nonZeroTotal++;
|
||||
|
||||
|
Reference in New Issue
Block a user