From 429230ced13d5e29db10183ffa79f314c6c2fadd Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 17 Jan 2024 22:18:19 +0100 Subject: [PATCH] saving: fix resource leak found by coverity fp_get_data() returns a copy of a string that must be freed. Fix this in save-git.c. The analogous function in save-xml.c has already been fixed. However, change the code to be more idiomatic: since we own the pointer, make it "char *" instead of "const char *". Then we don't have to cast on free(). Ultimately, we really should change string manipulation code to C++. Signed-off-by: Berthold Stoeger --- core/save-git.c | 4 +++- core/save-xml.c | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/save-git.c b/core/save-git.c index a6f3e25cf..9bbf883e1 100644 --- a/core/save-git.c +++ b/core/save-git.c @@ -871,12 +871,14 @@ static void save_one_device(struct membuffer *b, const struct device *d) static void save_one_fingerprint(struct membuffer *b, unsigned int i) { + char *fp_data = fp_get_data(&fingerprint_table, i); put_format(b, "fingerprint model=%08x serial=%08x deviceid=%08x diveid=%08x data=\"%s\"\n", fp_get_model(&fingerprint_table, i), fp_get_serial(&fingerprint_table, i), fp_get_deviceid(&fingerprint_table, i), fp_get_diveid(&fingerprint_table, i), - fp_get_data(&fingerprint_table, i)); + fp_data); + free(fp_data); } static void save_settings(git_repository *repo, struct dir *tree) diff --git a/core/save-xml.c b/core/save-xml.c index 463f7372a..43b4ed30f 100644 --- a/core/save-xml.c +++ b/core/save-xml.c @@ -609,14 +609,14 @@ static void save_one_device(struct membuffer *b, const struct device *d) static void save_one_fingerprint(struct membuffer *b, int i) { - const char *data = fp_get_data(&fingerprint_table, i); + char *data = fp_get_data(&fingerprint_table, i); put_format(b, "\n", fp_get_model(&fingerprint_table, i), fp_get_serial(&fingerprint_table, i), fp_get_deviceid(&fingerprint_table, i), fp_get_diveid(&fingerprint_table, i), data); - free((void *)data); + free(data); } int save_dives(const char *filename)