From 9d2b6d1d0438c3aa23bad5ad0335db21823eafb7 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 1 Mar 2024 22:53:43 +0100 Subject: [PATCH] core: turn extra_data key/value pair in parser to std::string Less troublesome memory management. Signed-off-by: Berthold Stoeger --- core/parse-xml.cpp | 14 ++++++-------- core/parse.cpp | 2 -- core/parse.h | 2 +- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/core/parse-xml.cpp b/core/parse-xml.cpp index 447f4cee7..dd3c3aa75 100644 --- a/core/parse-xml.cpp +++ b/core/parse-xml.cpp @@ -278,17 +278,15 @@ static void depth(const char *buffer, depth_t *depth, struct parser_state *state static void extra_data_start(struct parser_state *state) { - memset(&state->cur_extra_data, 0, sizeof(struct extra_data)); + state->cur_extra_data.key.clear(); + state->cur_extra_data.value.clear(); } static void extra_data_end(struct parser_state *state) { // don't save partial structures - we must have both key and value - if (state->cur_extra_data.key && state->cur_extra_data.value) - add_extra_data(get_dc(state), state->cur_extra_data.key, state->cur_extra_data.value); - free((void *)state->cur_extra_data.key); - free((void *)state->cur_extra_data.value); - state->cur_extra_data.key = state->cur_extra_data.value = NULL; + if (!state->cur_extra_data.key.empty() && !state->cur_extra_data.value.empty()) + add_extra_data(get_dc(state), state->cur_extra_data.key.c_str(), state->cur_extra_data.value.c_str()); } static void weight(const char *buffer, weight_t *weight, struct parser_state *state) @@ -829,9 +827,9 @@ static int match_dc_data_fields(struct divecomputer *dc, const char *name, char return 1; if (MATCH("salinity.water", salinity, &dc->salinity)) return 1; - if (MATCH("key.extradata", utf8_string, (char **)&state->cur_extra_data.key)) + if (MATCH("key.extradata", utf8_string_std, &state->cur_extra_data.key)) return 1; - if (MATCH("value.extradata", utf8_string, (char **)&state->cur_extra_data.value)) + if (MATCH("value.extradata", utf8_string_std, &state->cur_extra_data.value)) return 1; if (MATCH("divemode", get_dc_type, &dc->divemode)) return 1; diff --git a/core/parse.cpp b/core/parse.cpp index 89b07c911..bd48157d9 100644 --- a/core/parse.cpp +++ b/core/parse.cpp @@ -24,8 +24,6 @@ parser_state::~parser_state() free_dive(cur_dive); free_trip(cur_trip); free_dive_site(cur_dive_site); - free((void *)cur_extra_data.key); - free((void *)cur_extra_data.value); } /* diff --git a/core/parse.h b/core/parse.h index 661ad196e..ebd8af7a6 100644 --- a/core/parse.h +++ b/core/parse.h @@ -88,7 +88,7 @@ struct parser_state { int lastcylinderindex = 0, next_o2_sensor = 0; int o2pressure_sensor = 0; int sample_rate = 0; - struct extra_data cur_extra_data{ 0 }; + struct { std::string key; std::string value; } cur_extra_data; struct units xml_parsing_units; struct divelog *log = nullptr; /* non-owning */ struct fingerprint_table *fingerprints = nullptr; /* non-owning */