aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-04-02 15:51:25 +0200
committerRobin Krahl <robin.krahl@ireas.org>2020-04-02 15:53:15 +0200
commit2a7b3f4e2ae09d665f9783030323dfb1a4c5ee9f (patch)
treeeaeae91f91b70580b2ca8dab38a9a9908be6d626
parentb482abe3bf62ef46816e81ccdab68bc24f498ce6 (diff)
downloadlibnitrokey-2a7b3f4e2ae09d665f9783030323dfb1a4c5ee9f.tar.gz
libnitrokey-2a7b3f4e2ae09d665f9783030323dfb1a4c5ee9f.tar.bz2
Add missing free functions to C API
This patch adds two missing free functions, NK_free_config and NK_free_password_safe_slot_status, to enable memory-safe use of the C API. Fixes #173.
-rw-r--r--NK_C_API.cc8
-rw-r--r--NK_C_API.h14
-rw-r--r--unittest/test_memory.c4
3 files changed, 24 insertions, 2 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc
index 1d3fa3a..0b7f5f7 100644
--- a/NK_C_API.cc
+++ b/NK_C_API.cc
@@ -226,6 +226,10 @@ extern "C" {
});
}
+ NK_C_API void NK_free_config(uint8_t* config) {
+ delete[] config;
+ }
+
NK_C_API enum NK_device_model NK_get_device_model() {
auto m = NitrokeyManager::instance();
@@ -448,6 +452,10 @@ extern "C" {
}
+ NK_C_API void NK_free_password_safe_slot_status(uint8_t* status) {
+ delete[] status;
+ }
+
NK_C_API uint8_t NK_get_user_retry_count() {
auto m = NitrokeyManager::instance();
return get_with_result([&]() {
diff --git a/NK_C_API.h b/NK_C_API.h
index d5c54a3..c4cb448 100644
--- a/NK_C_API.h
+++ b/NK_C_API.h
@@ -451,6 +451,7 @@ extern "C" {
/**
* Get currently set config - status of function Numlock/Capslock/Scrollock OTP sending and is enabled PIN protected OTP
+ * The return value must be freed using NK_free_config.
* @see NK_write_config
* @return uint8_t general_config[5]:
* uint8_t numlock;
@@ -462,6 +463,12 @@ extern "C" {
*/
NK_C_API uint8_t* NK_read_config();
+ /**
+ * Free a value returned by NK_read_config. May be called with a NULL
+ * argument.
+ */
+ NK_C_API void NK_free_config(uint8_t* config);
+
//OTP
/**
@@ -634,10 +641,17 @@ extern "C" {
/**
* Get password safe slots' status
+ * The return value must be freed using NK_free_password_safe_slot_status.
* @return uint8_t[16] slot statuses - each byte represents one slot with 0 (not programmed) and 1 (programmed)
*/
NK_C_API uint8_t * NK_get_password_safe_slot_status();
+ /**
+ * Free a value returned by NK_get_password_safe_slot_status. May be
+ * called with a NULL argument.
+ */
+ NK_C_API void NK_free_password_safe_slot_status(uint8_t* status);
+
/**
* Get password safe slot name
* @param slot_number password safe slot number, slot_number<16
diff --git a/unittest/test_memory.c b/unittest/test_memory.c
index 34ea7d5..20b11b2 100644
--- a/unittest/test_memory.c
+++ b/unittest/test_memory.c
@@ -44,7 +44,7 @@ int main() {
uint8_t *config = NK_read_config();
if (config == NULL)
return 1;
- free(config);
+ NK_free_config(config);
result = NK_enable_password_safe("123456");
if (result != 0)
@@ -54,7 +54,7 @@ int main() {
if (slot_status == NULL) {
return 1;
}
- free(slot_status);
+ NK_free_password_safe_slot_status(slot_status);
NK_logout();