Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/ncrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ class DataPointer final {
bool isSecure() const { return secure_; }

private:
void free();

void* data_ = nullptr;
size_t len_ = 0;
bool secure_ = false;
Expand Down
13 changes: 11 additions & 2 deletions src/ncrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,18 @@ void DataPointer::zero() {
OPENSSL_cleanse(data_, len_);
}

void DataPointer::reset(void* data, size_t length) {
void DataPointer::free() {
if (data_ != nullptr) {
if (secure_) {
OPENSSL_secure_clear_free(data_, len_);
} else {
OPENSSL_clear_free(data_, len_);
}
}
}

void DataPointer::reset(void* data, size_t length) {
free();
data_ = data;
len_ = length;
}
Expand All @@ -258,7 +262,12 @@ DataPointer DataPointer::resize(size_t len) {
size_t actual_len = std::min(len_, len);
auto buf = release();
if (actual_len == len_) return DataPointer(buf.data, actual_len);
buf.data = OPENSSL_realloc(buf.data, actual_len);
auto new_data = OPENSSL_realloc(buf.data, actual_len);
if (new_data == nullptr) {
free();
return {};
}
buf.data = new_data;
buf.len = actual_len;
return DataPointer(buf);
}
Expand Down