src/confd: refactor srx_val.c to use sr_get_items() to silence log

Calling sr_get_time() for an xpath that has no data causes sysrepo to
always log the access as an error.  With sr_get_items() it is left to
the callee to determine if the missing data should cause an error.

This patch is a refactor that silences the syslog message and also
fixes one or tow memory leaks.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-04-17 14:09:24 +02:00
parent fdb8e56461
commit 36d4e1b92b
+27 -10
View File
@@ -3,11 +3,12 @@
#include <stdarg.h>
#include "core.h"
static int srx_vaget(sr_session_ctx_t *session, const char *fmt, va_list ap, sr_val_t **val, sr_val_type_t type)
static int srx_vaget(sr_session_ctx_t *session, const char *fmt, va_list ap, sr_val_type_t type, sr_val_t **val, size_t *cnt)
{
va_list apdup;
char *xpath;
int len;
int rc;
va_copy(apdup, ap);
len = vsnprintf(NULL, 0, fmt, apdup) + 1;
@@ -21,13 +22,26 @@ static int srx_vaget(sr_session_ctx_t *session, const char *fmt, va_list ap, sr_
vsnprintf(xpath, len, fmt, apdup);
va_end(apdup);
if (sr_get_item(session, xpath, 0, val)) {
ERROR("Failed reading xpath %s", xpath);
rc = sr_get_items(session, xpath, 0, 0, val, cnt);
if (rc) {
ERROR("Failed reading xpath %s: %s", xpath, sr_strerror(rc));
return -1;
}
if (type != SR_UNKNOWN_T && (*val)->type != type)
if (*cnt == 0) {
errno = ENODATA;
return -1;
} else if (*cnt > 1) {
sr_free_values(*val, *cnt);
errno = EOVERFLOW;
return -1;
}
if (type != SR_UNKNOWN_T && val[0]->type != type) {
sr_free_values(*val, *cnt);
errno = EINVAL;
return -1;
}
return 0;
}
@@ -35,18 +49,19 @@ static int srx_vaget(sr_session_ctx_t *session, const char *fmt, va_list ap, sr_
static int get_vabool(sr_session_ctx_t *session, int *result, const char *fmt, va_list ap)
{
sr_val_t *val = NULL;
size_t cnt = 0;
va_list apdup;
int rc;
va_copy(apdup, ap);
rc = srx_vaget(session, fmt, apdup, &val, SR_BOOL_T);
rc = srx_vaget(session, fmt, apdup, SR_BOOL_T, &val, &cnt);
va_end(apdup);
if (rc)
return rc;
*result = val->data.bool_val;
sr_free_val(val);
sr_free_values(val, cnt);
return 0;
}
@@ -79,11 +94,12 @@ int srx_enabled(sr_session_ctx_t *session, const char *fmt, ...)
int srx_get_int(sr_session_ctx_t *session, int *result, sr_val_type_t type, const char *fmt, ...)
{
sr_val_t *val = NULL;
size_t cnt = 0;
va_list ap;
int rc;
va_start(ap, fmt);
rc = srx_vaget(session, fmt, ap, &val, type);
rc = srx_vaget(session, fmt, ap, type, &val, &cnt);
va_end(ap);
if (rc)
@@ -121,7 +137,7 @@ int srx_get_int(sr_session_ctx_t *session, int *result, sr_val_type_t type, cons
rc = 0;
fail:
sr_free_val(val);
sr_free_values(val, cnt);
return rc;
}
@@ -129,14 +145,15 @@ char *srx_get_str(sr_session_ctx_t *session, const char *fmt, ...)
{
sr_val_t *val = NULL;
char *str = NULL;
size_t cnt = 0;
va_list ap;
va_start(ap, fmt);
if (srx_vaget(session, fmt, ap, &val, SR_STRING_T))
if (srx_vaget(session, fmt, ap, SR_UNKNOWN_T, &val, &cnt))
goto fail;
str = sr_val_to_str(val);
sr_free_val(val);
sr_free_values(val, cnt);
fail:
va_end(ap);
return str;