log REFACTOR create EMEM messages w/o problems with freeing error structures
Message in ly_err_item is supposed to be dynamically allocated and it is
freed when freeing the structure. But in case we face memory allocation
failures, it is necessary to try to create the structure with a simple
static string without issues with freeing the structure.
diff --git a/src/common.h b/src/common.h
index 1bdec36..28959d9 100644
--- a/src/common.h
+++ b/src/common.h
@@ -102,6 +102,11 @@
# define LOGDBG(dbg_group, str, ...) ly_log_dbg(dbg_group, str, ##__VA_ARGS__);
#endif
+/**
+ * Simple EMEM message, it can be safely stored in ::ly_err_item structures without problems when freeing.
+ */
+#define LY_EMEM_MSG "Memory allocation failed."
+
#define LOGMEM(CTX) LOGERR(CTX, LY_EMEM, "Memory allocation failed (%s()).", __func__)
#define LOGINT(CTX) LOGERR(CTX, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__)
#define LOGARG(CTX, ARG) LOGERR(CTX, LY_EINVAL, "Invalid argument %s (%s()).", #ARG, __func__)
diff --git a/src/log.c b/src/log.c
index 45f1750..5208c94 100644
--- a/src/log.c
+++ b/src/log.c
@@ -162,7 +162,9 @@
/* clean the error list */
for (i = (struct ly_err_item *)ptr; i; i = next) {
next = i->next;
- free(i->msg);
+ if (i->msg && strcmp(i->msg, LY_EMEM_MSG)) {
+ free(i->msg);
+ }
free(i->path);
free(i->apptag);
free(i);
diff --git a/src/plugins_types.c b/src/plugins_types.c
index a2c7e1f..9b6625a 100644
--- a/src/plugins_types.c
+++ b/src/plugins_types.c
@@ -324,7 +324,7 @@
error:
if (rc == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -363,7 +363,7 @@
error:
if (rc == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -452,7 +452,7 @@
/* prepare value string without decimal point to easily parse using standard functions */
valcopy = malloc(size * sizeof *valcopy);
if (!valcopy) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
}
@@ -496,14 +496,14 @@
/* match_data needs to be allocated each time because of possible multi-threaded evaluation */
match_data = pcre2_match_data_create_from_pattern(patterns[u]->code, NULL);
if (!match_data) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
}
rc = pcre2_match(patterns[u]->code, (PCRE2_SPTR)str, str_len, 0, PCRE2_ANCHORED | PCRE2_ENDANCHORED, match_data, NULL);
if (rc == PCRE2_ERROR_NOMATCH) {
if (asprintf(&errmsg, "String \"%.*s\" does not conform to the pattern \"%s\".", (int)str_len, str, patterns[u]->expr) == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
ret = LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_ESYS, 0, errmsg, NULL, NULL);
@@ -591,7 +591,7 @@
error:
if ((rc == -1) || !errmsg) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, range->eapptag ? strdup(range->eapptag) : NULL);
@@ -715,7 +715,7 @@
error:
if ((rc == -1) || !msg) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, msg, NULL, NULL);
@@ -996,7 +996,7 @@
error:
if (!*err) {
if ((rc == -1) || !errmsg) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
}
@@ -1176,7 +1176,7 @@
cleanup:
if (rc == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
ret = LY_EMEM;
} else if (errmsg) {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -1269,7 +1269,7 @@
error:
if (rc == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -1300,7 +1300,7 @@
} else {
char *errmsg;
if (asprintf(&errmsg, "Invalid boolean value \"%.*s\".", (int)value_len, value) == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -1336,7 +1336,7 @@
if (value_len) {
char *errmsg;
if (asprintf(&errmsg, "Invalid empty value \"%.*s\".", (int)value_len, value) == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -1518,7 +1518,7 @@
error:
if ((rc == -1) || !errmsg) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -1650,7 +1650,7 @@
if (!*err) {
if (rc == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
ret = LY_EMEM;
} else if (errmsg) {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -1684,7 +1684,7 @@
ret = ly_path_eval(storage->target, tree, NULL);
if (ret) {
if (asprintf(&errmsg, "Invalid instance-identifier \"%s\" value - required instance not found.", storage->canonical) == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
ret = LY_EMEM;
} else {
*err = ly_err_new(LY_LLERR, LY_EVALID, LYVE_DATA, errmsg, NULL, NULL);
@@ -2112,7 +2112,7 @@
if (u == LY_ARRAY_COUNT(types)) {
if (asprintf(&errmsg, "Invalid union value \"%s\" - no matching subtype found.", subvalue->original) == -1) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
ret = LY_EMEM;
goto cleanup;
}
@@ -2144,7 +2144,7 @@
/* prepare subvalue storage */
subvalue = calloc(1, sizeof *subvalue);
if (!subvalue) {
- *err = ly_err_new(LY_LLERR, LY_EMEM, 0, "Memory allocation failed.", NULL, NULL);
+ *err = ly_err_new(LY_LLERR, LY_EMEM, 0, LY_EMEM_MSG, NULL, NULL);
return LY_EMEM;
}