Skip to content

Commit

Permalink
fix: solve memory issues discovered on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
saturneric committed Dec 3, 2024
1 parent f4525ea commit c48aaf4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/core/function/SecureMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ auto SecureMemoryAllocator::Reallocate(void* ptr, std::size_t size) -> void* {
return addr;
}

void SecureMemoryAllocator::Deallocate(void* p) { std::free(p); }
void SecureMemoryAllocator::Deallocate(void* p) {
if (p != nullptr) std::free(p);
}

} // namespace GpgFrontend
56 changes: 40 additions & 16 deletions src/sdk/GFSDKGpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgSignData(int channel, char** key_ids,
int key_ids_size, char* data,
int sign_mode, int ascii,
GFGpgSignResult** ps) -> int {
void* mem = GFAllocateMemory(sizeof(GFGpgSignResult));
if (mem == nullptr) {
*ps = nullptr;
return -1;
}

std::memset(mem, 0, sizeof(GFGpgSignResult));
*ps = new (mem) GFGpgSignResult{};
auto* s = *ps;

auto singer_ids = CharArrayToQStringList(key_ids, key_ids_size);

GpgFrontend::KeyArgsList signer_keys;
Expand All @@ -68,10 +78,6 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgSignData(int channel, char** key_ids,
GpgFrontend::GpgBasicOperator::GetInstance(channel).SignSync(
signer_keys, in_buffer, gpg_sign_mode, ascii != 0);

*ps =
static_cast<GFGpgSignResult*>(GFAllocateMemory(sizeof(GFGpgSignResult)));
auto* s = *ps;

if (GpgFrontend::CheckGpgError(err) != GPG_ERR_NO_ERROR) {
s->error_string = GFStrDup(GpgFrontend::DescribeGpgErrCode(err).second);
return -1;
Expand Down Expand Up @@ -130,6 +136,16 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgKeyPrimaryUID(int channel, char* key_id,
auto GPGFRONTEND_MODULE_SDK_EXPORT
GFGpgEncryptData(int channel, char** key_ids, int key_ids_size, char* data,
int ascii, GFGpgEncryptionResult** ps) -> int {
void* mem = GFAllocateMemory(sizeof(GFGpgEncryptionResult));
if (mem == nullptr) {
*ps = nullptr;
return -1;
}

std::memset(mem, 0, sizeof(GFGpgEncryptionResult));
*ps = new (mem) GFGpgEncryptionResult{};
auto* s = *ps;

auto encrypt_key_ids = CharArrayToQStringList(key_ids, key_ids_size);

GpgFrontend::KeyArgsList encrypt_keys;
Expand All @@ -147,10 +163,6 @@ GFGpgEncryptData(int channel, char** key_ids, int key_ids_size, char* data,
GpgFrontend::GpgBasicOperator::GetInstance(channel).EncryptSync(
encrypt_keys, in_buffer, ascii != 0);

*ps = static_cast<GFGpgEncryptionResult*>(
GFAllocateMemory(sizeof(GFGpgEncryptionResult)));
auto* s = *ps;

if (GpgFrontend::CheckGpgError(err) != GPG_ERR_NO_ERROR) {
s->error_string = GFStrDup(GpgFrontend::DescribeGpgErrCode(err).second);
return -1;
Expand All @@ -172,16 +184,22 @@ GFGpgEncryptData(int channel, char** key_ids, int key_ids_size, char* data,

auto GPGFRONTEND_MODULE_SDK_EXPORT
GFGpgDecryptData(int channel, char* data, GFGpgDecryptResult** ps) -> int {
void* mem = GFAllocateMemory(sizeof(GFGpgDecryptResult));
if (mem == nullptr) {
*ps = nullptr;
return -1;
}

std::memset(mem, 0, sizeof(GFGpgDecryptResult));
*ps = new (mem) GFGpgDecryptResult{};
auto* s = *ps;

auto in_buffer = GpgFrontend::GFBuffer(GFUnStrDup(data).toUtf8());

auto [err, data_object] =
GpgFrontend::GpgBasicOperator::GetInstance(channel).DecryptSync(
in_buffer);

*ps = static_cast<GFGpgDecryptResult*>(
GFAllocateMemory(sizeof(GFGpgDecryptResult)));
auto* s = *ps;

if (GpgFrontend::CheckGpgError(err) != GPG_ERR_NO_ERROR) {
s->error_string = GFStrDup(GpgFrontend::DescribeGpgErrCode(err).second);
return -1;
Expand All @@ -203,17 +221,23 @@ GFGpgDecryptData(int channel, char* data, GFGpgDecryptResult** ps) -> int {

auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgVerifyData(
int channel, char* data, char* signature, GFGpgVerifyResult** ps) -> int {
void* mem = GFAllocateMemory(sizeof(GFGpgVerifyResult));
if (mem == nullptr) {
*ps = nullptr;
return -1;
}

std::memset(mem, 0, sizeof(GFGpgVerifyResult));
*ps = new (mem) GFGpgVerifyResult{};
auto* s = *ps;

auto in_buffer = GpgFrontend::GFBuffer(GFUnStrDup(data).toUtf8());
auto sig_buffer = GpgFrontend::GFBuffer(GFUnStrDup(signature).toUtf8());

auto [err, data_object] =
GpgFrontend::GpgBasicOperator::GetInstance(channel).VerifySync(
in_buffer, sig_buffer);

*ps = static_cast<GFGpgVerifyResult*>(
GFAllocateMemory(sizeof(GFGpgVerifyResult)));
auto* s = *ps;

if (GpgFrontend::CheckGpgError(err) != GPG_ERR_NO_ERROR) {
s->error_string = GFStrDup(GpgFrontend::DescribeGpgErrCode(err).second);
return -1;
Expand Down

0 comments on commit c48aaf4

Please sign in to comment.