json parser REFACTOR remove strndup
There is no need to duplicate the string, just pass the responsibility
for releasing to another pointer.
diff --git a/src/parser_json.c b/src/parser_json.c
index 1f463aa..3ac0542 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -20,7 +20,6 @@
#include <string.h>
#include "common.h"
-#include "compat.h"
#include "context.h"
#include "dict.h"
#include "in_internal.h"
@@ -78,6 +77,35 @@
}
/**
+ * @brief Submit the responsibility for releasing the dynamic values to @p dst.
+ *
+ * @param[in] jsonctx JSON context which contains the dynamic value.
+ * @param[in,out] dst Pointer to which the responsibility will be submited.
+ * If the pointer is already pointing to some allocated memory,
+ * it is released beforehand.
+ */
+static void
+lyjson_ctx_submit_dynamic_value(struct lyjson_ctx *jsonctx, char **dst)
+{
+ assert(jsonctx && dst);
+
+ if (!jsonctx->dynamic) {
+ return;
+ }
+
+ if (dst) {
+ free(*dst);
+ }
+ *dst = NULL;
+
+ /* Submit the dynamic value. */
+ *dst = (char *)jsonctx->value;
+
+ /* Responsibility for the release is now passed to @p dst. */
+ jsonctx->dynamic = 0;
+}
+
+/**
* @brief Parse JSON member-name as [\@][prefix:][name]
*
* \@ - metadata flag, maps to 1 in @p is_meta_p
@@ -746,26 +774,8 @@
LOG_LOCSET(snode, NULL, NULL, NULL);
while (status != LYJSON_OBJECT_CLOSED) {
- if (dynamic_prefname) {
- free(dynamic_prefname);
- dynamic_prefname = NULL;
- }
- if (lydctx->jsonctx->dynamic) {
- /* Duplicate ::lyjson_ctx.value because it is dynamically
- * allocated and later ::lyjson_ctx_next() will release it
- * which will cause local pointers 'name' and 'prefix'
- * to be invalid.
- */
- dynamic_prefname = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
- if (!dynamic_prefname) {
- LOGMEM(lydctx->jsonctx->ctx);
- ret = LY_EMEM;
- goto cleanup;
- }
- lydjson_parse_name(dynamic_prefname, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
- } else {
- lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
- }
+ lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
+ lyjson_ctx_submit_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
if (!prefix) {
LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
@@ -1263,18 +1273,9 @@
assert(parent || first_p);
assert(status == LYJSON_OBJECT);
- /* Duplicate ::lyjson_ctx.value because it can be dynamically allocated and later
- * ::lyjson_ctx_next() will release it although this string is needed for the ::lydjson_parse_opaq().
- */
- value = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
- if (!value) {
- LOGMEM(lydctx->jsonctx->ctx);
- ret = LY_EMEM;
- goto cleanup;
- }
-
/* process the node name */
- lydjson_parse_name(value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta);
+ lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta);
+ lyjson_ctx_submit_dynamic_value(lydctx->jsonctx, &value);
if (!is_meta || name_len || prefix_len) {
/* get the schema node */