pgbackrest.git / summary / log / commit / refs

commit    d89bc6f2d241ae864cb9dcae2f5c6fc3a0e655ee
Author:   David Steele <david@pgmasters.net>
Date:     Thu Apr 28 14:53:11 2022 +0000

    Fix leaks in the storage/gcs module.
    
    storageGcsAuthToken() memory was being cleaned up by the calling context, but seems better to keep this tidy and add a temp mem context.
    
    storageGcsRequest() leaked an HttpRequest.
    
    storageGcsInfo() leaked a number of objects. Use a temp mem context to prevent that.
    
    storageGcsPathRemoveCallback() leaked an HttpResponse.
    
    storageGcsRemove() leaked an HttpReponse.
    
    storageWriteGcsVerify() leaked a number of objects. Use a temp mem context to prevent that.
    
    storageWriteGcsBlock) leaked an HttpReponse.


src/storage/gcs/storage.c | 127 +++++++++++++++++++++++++++------------------- src/storage/gcs/write.c | 55 +++++++++++--------- 2 files changed, 104 insertions(+), 78 deletions(-) diff --git a/src/storage/gcs/storage.c b/src/storage/gcs/storage.c index 703016938..aad4252fa 100644 --- a/src/storage/gcs/storage.c +++ b/src/storage/gcs/storage.c @@ -112,7 +112,7 @@ typedef struct } StorageGcsAuthTokenResult; static StorageGcsAuthTokenResult -storageGcsAuthToken(HttpRequest *request, time_t timeBegin) +storageGcsAuthToken(HttpRequest *const request, const time_t timeBegin) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(HTTP_REQUEST, request); @@ -121,34 +121,38 @@ storageGcsAuthToken(HttpRequest *request, time_t timeBegin) StorageGcsAuthTokenResult result = {0}; - // Get the response - KeyValue *kvResponse = varKv(jsonToVar(strNewBuf(httpResponseContent(httpRequestResponse(request, true))))); + MEM_CONTEXT_TEMP_BEGIN() + { + // Get the response + KeyValue *const kvResponse = varKv(jsonToVar(strNewBuf(httpResponseContent(httpRequestResponse(request, true))))); - // Check for an error - const String *error = varStr(kvGet(kvResponse, GCS_JSON_ERROR_VAR)); + // Check for an error + const String *const error = varStr(kvGet(kvResponse, GCS_JSON_ERROR_VAR)); - if (error != NULL) - { - THROW_FMT( - ProtocolError, "unable to get authentication token: [%s] %s", strZ(error), - strZNull(varStr(kvGet(kvResponse, GCS_JSON_ERROR_DESCRIPTION_VAR)))); - } + if (error != NULL) + { + THROW_FMT( + ProtocolError, "unable to get authentication token: [%s] %s", strZ(error), + strZNull(varStr(kvGet(kvResponse, GCS_JSON_ERROR_DESCRIPTION_VAR)))); + } - MEM_CONTEXT_PRIOR_BEGIN() - { - // Get token - result.tokenType = strDup(varStr(kvGet(kvResponse, GCS_JSON_TOKEN_TYPE_VAR))); - CHECK(FormatError, result.tokenType != NULL, "token type missing"); - result.token = strDup(varStr(kvGet(kvResponse, GCS_JSON_ACCESS_TOKEN_VAR))); - CHECK(FormatError, result.token != NULL, "access token missing"); + MEM_CONTEXT_PRIOR_BEGIN() + { + // Get token + result.tokenType = strDup(varStr(kvGet(kvResponse, GCS_JSON_TOKEN_TYPE_VAR))); + CHECK(FormatError, result.tokenType != NULL, "token type missing"); + result.token = strDup(varStr(kvGet(kvResponse, GCS_JSON_ACCESS_TOKEN_VAR))); + CHECK(FormatError, result.token != NULL, "access token missing"); - // Get expiration - const Variant *const expiresIn = kvGet(kvResponse, GCS_JSON_EXPIRES_IN_VAR); - CHECK(FormatError, expiresIn != NULL, "expiry missing"); + // Get expiration + const Variant *const expiresIn = kvGet(kvResponse, GCS_JSON_EXPIRES_IN_VAR); + CHECK(FormatError, expiresIn != NULL, "expiry missing"); - result.timeExpire = timeBegin + (time_t)varInt64Force(expiresIn); + result.timeExpire = timeBegin + (time_t)varInt64Force(expiresIn); + } + MEM_CONTEXT_PRIOR_END(); } - MEM_CONTEXT_PRIOR_END(); + MEM_CONTEXT_TEMP_END(); FUNCTION_TEST_RETURN_TYPE(StorageGcsAuthTokenResult, result); } @@ -262,7 +266,11 @@ storageGcsAuthService(StorageGcs *this, time_t timeBegin) HttpRequest *request = httpRequestNewP( this->authClient, HTTP_VERB_POST_STR, httpUrlPath(this->authUrl), NULL, .header = header, .content = BUFSTR(content)); - result = storageGcsAuthToken(request, timeBegin); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = storageGcsAuthToken(request, timeBegin); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -297,7 +305,11 @@ storageGcsAuthAuto(StorageGcs *this, time_t timeBegin) HttpRequest *request = httpRequestNewP( this->authClient, HTTP_VERB_GET_STR, httpUrlPath(this->authUrl), NULL, .header = header); - result = storageGcsAuthToken(request, timeBegin); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = storageGcsAuthToken(request, timeBegin); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -456,7 +468,7 @@ storageGcsResponse(HttpRequest *request, StorageGcsResponseParam param) } HttpResponse * -storageGcsRequest(StorageGcs *this, const String *verb, StorageGcsRequestParam param) +storageGcsRequest(StorageGcs *const this, const String *const verb, const StorageGcsRequestParam param) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(STORAGE_GCS, this); @@ -473,13 +485,15 @@ storageGcsRequest(StorageGcs *this, const String *verb, StorageGcsRequestParam p FUNCTION_LOG_PARAM(BOOL, param.contentIo); FUNCTION_LOG_END(); - FUNCTION_LOG_RETURN( - HTTP_RESPONSE, - storageGcsResponseP( - storageGcsRequestAsyncP( - this, verb, .noBucket = param.noBucket, .upload = param.upload, .noAuth = param.noAuth, .object = param.object, - .header = param.header, .query = param.query, .content = param.content), - .allowMissing = param.allowMissing, .allowIncomplete = param.allowIncomplete, .contentIo = param.contentIo)); + HttpRequest *const request = storageGcsRequestAsyncP( + this, verb, .noBucket = param.noBucket, .upload = param.upload, .noAuth = param.noAuth, .object = param.object, + .header = param.header, .query = param.query, .content = param.content); + HttpResponse *const result = storageGcsResponseP( + request, .allowMissing = param.allowMissing, .allowIncomplete = param.allowIncomplete, .contentIo = param.contentIo); + + httpRequestFree(request); + + FUNCTION_LOG_RETURN(HTTP_RESPONSE, result); } /*********************************************************************************************************************************** @@ -687,7 +701,7 @@ storageGcsListInternal( /**********************************************************************************************************************************/ static StorageInfo -storageGcsInfo(THIS_VOID, const String *file, StorageInfoLevel level, StorageInterfaceInfoParam param) +storageGcsInfo(THIS_VOID, const String *const file, const StorageInfoLevel level, const StorageInterfaceInfoParam param) { THIS(StorageGcs); @@ -701,22 +715,30 @@ storageGcsInfo(THIS_VOID, const String *file, StorageInfoLevel level, StorageInt ASSERT(this != NULL); ASSERT(file != NULL); - // // Attempt to get file info - HttpResponse *httpResponse = storageGcsRequestP( - this, HTTP_VERB_GET_STR, .object = file, .allowMissing = true, - .query = httpQueryAdd( - httpQueryNewP(), GCS_QUERY_FIELDS_STR, - level >= storageInfoLevelBasic ? STRDEF(GCS_JSON_SIZE "," GCS_JSON_UPDATED) : EMPTY_STR)); - - // Does the file exist? - StorageInfo result = {.level = level, .exists = httpResponseCodeOk(httpResponse)}; + StorageInfo result = {.level = level}; - // Add basic level info if requested and the file exists - if (result.level >= storageInfoLevelBasic && result.exists) + MEM_CONTEXT_TEMP_BEGIN() { - result.type = storageTypeFile; - storageGcsInfoFile(&result, varKv(jsonToVar(strNewBuf(httpResponseContent(httpResponse))))); + // Attempt to get file info + HttpResponse *const httpResponse = storageGcsRequestP( + this, HTTP_VERB_GET_STR, .object = file, .allowMissing = true, + .query = httpQueryAdd( + httpQueryNewP(), GCS_QUERY_FIELDS_STR, + level >= storageInfoLevelBasic ? STRDEF(GCS_JSON_SIZE "," GCS_JSON_UPDATED) : EMPTY_STR)); + + // Does the file exist? + result.exists = httpResponseCodeOk(httpResponse); + + // Add basic level info if requested and the file exists + if (result.level >= storageInfoLevelBasic && result.exists) + { + result.type = storageTypeFile; + storageGcsInfoFile(&result, varKv(jsonToVar(strNewBuf(httpResponseContent(httpResponse))))); + } + + httpResponseFree(httpResponse); } + MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN(STORAGE_INFO, result); } @@ -803,7 +825,7 @@ typedef struct StorageGcsPathRemoveData } StorageGcsPathRemoveData; static void -storageGcsPathRemoveCallback(void *callbackData, const StorageInfo *info) +storageGcsPathRemoveCallback(void *const callbackData, const StorageInfo *const info) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM_P(VOID, callbackData); @@ -813,13 +835,12 @@ storageGcsPathRemoveCallback(void *callbackData, const StorageInfo *info) ASSERT(callbackData != NULL); ASSERT(info != NULL); - StorageGcsPathRemoveData *data = callbackData; + StorageGcsPathRemoveData *const data = callbackData; // Get response from prior async request if (data->request != NULL) { - storageGcsResponseP(data->request, .allowMissing = true); - + httpResponseFree(storageGcsResponseP(data->request, .allowMissing = true)); httpRequestFree(data->request); data->request = NULL; } @@ -875,7 +896,7 @@ storageGcsPathRemove(THIS_VOID, const String *path, bool recurse, StorageInterfa /**********************************************************************************************************************************/ static void -storageGcsRemove(THIS_VOID, const String *file, StorageInterfaceRemoveParam param) +storageGcsRemove(THIS_VOID, const String *const file, const StorageInterfaceRemoveParam param) { THIS(StorageGcs); @@ -889,7 +910,7 @@ storageGcsRemove(THIS_VOID, const String *file, StorageInterfaceRemoveParam para ASSERT(file != NULL); ASSERT(!param.errorOnMissing); - storageGcsRequestP(this, HTTP_VERB_DELETE_STR, .object = file, .allowMissing = true); + httpResponseFree(storageGcsRequestP(this, HTTP_VERB_DELETE_STR, .object = file, .allowMissing = true)); FUNCTION_LOG_RETURN_VOID(); } diff --git a/src/storage/gcs/write.c b/src/storage/gcs/write.c index b8939c00c..6acdc9364 100644 --- a/src/storage/gcs/write.c +++ b/src/storage/gcs/write.c @@ -74,43 +74,47 @@ storageWriteGcsOpen(THIS_VOID) Verify upload ***********************************************************************************************************************************/ static void -storageWriteGcsVerify(StorageWriteGcs *this, HttpResponse *response) +storageWriteGcsVerify(StorageWriteGcs *const this, HttpResponse *const response) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(STORAGE_WRITE_GCS, this); FUNCTION_LOG_PARAM(HTTP_RESPONSE, response); FUNCTION_LOG_END(); - KeyValue *content = varKv(jsonToVar(strNewBuf(httpResponseContent(response)))); - - // Check the md5 hash - const String *md5base64 = varStr(kvGet(content, GCS_JSON_MD5_HASH_VAR)); - CHECK(FormatError, md5base64 != NULL, "MD5 missing"); - - const String *md5actual = bufHex(bufNewDecode(encodeBase64, md5base64)); - const String *md5expected = pckReadStrP(pckReadNew(ioFilterResult(this->md5hash))); - - if (!strEq(md5actual, md5expected)) + MEM_CONTEXT_TEMP_BEGIN() { - THROW_FMT( - FormatError, "expected md5 '%s' for '%s' but actual is '%s'", strZ(md5expected), strZ(this->interface.name), - strZ(md5actual)); - } + KeyValue *const content = varKv(jsonToVar(strNewBuf(httpResponseContent(response)))); - // Check the size when available - const String *sizeStr = varStr(kvGet(content, GCS_JSON_SIZE_VAR)); + // Check the md5 hash + const String *const md5base64 = varStr(kvGet(content, GCS_JSON_MD5_HASH_VAR)); + CHECK(FormatError, md5base64 != NULL, "MD5 missing"); - if (sizeStr != NULL) - { - uint64_t size = cvtZToUInt64(strZ(sizeStr)); + const String *const md5actual = bufHex(bufNewDecode(encodeBase64, md5base64)); + const String *const md5expected = pckReadStrP(pckReadNew(ioFilterResult(this->md5hash))); - if (size != this->uploadTotal) + if (!strEq(md5actual, md5expected)) { THROW_FMT( - FormatError, "expected size %" PRIu64 " for '%s' but actual is %" PRIu64, size, strZ(this->interface.name), - this->uploadTotal); + FormatError, "expected md5 '%s' for '%s' but actual is '%s'", strZ(md5expected), strZ(this->interface.name), + strZ(md5actual)); + } + + // Check the size when available + const String *const sizeStr = varStr(kvGet(content, GCS_JSON_SIZE_VAR)); + + if (sizeStr != NULL) + { + const uint64_t size = cvtZToUInt64(strZ(sizeStr)); + + if (size != this->uploadTotal) + { + THROW_FMT( + FormatError, "expected size %" PRIu64 " for '%s' but actual is %" PRIu64, size, strZ(this->interface.name), + this->uploadTotal); + } } } + MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN_VOID(); } @@ -119,7 +123,7 @@ storageWriteGcsVerify(StorageWriteGcs *this, HttpResponse *response) Flush bytes to upload chunk ***********************************************************************************************************************************/ static void -storageWriteGcsBlock(StorageWriteGcs *this, bool done) +storageWriteGcsBlock(StorageWriteGcs *const this, const bool done) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(STORAGE_WRITE_GCS, this); @@ -131,12 +135,13 @@ storageWriteGcsBlock(StorageWriteGcs *this, bool done) // If there is an outstanding async request then wait for the response to ensure the request did not error if (this->request != NULL) { - HttpResponse *response = storageGcsResponseP(this->request, .allowIncomplete = !done); + HttpResponse *const response = storageGcsResponseP(this->request, .allowIncomplete = !done); // If done then verify the md5 checksum if (done) storageWriteGcsVerify(this, response); + httpResponseFree(response); httpRequestFree(this->request); this->request = NULL; } [parent: 083c93eaa3d5]