Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Coverity Scan fixes #594

Merged
merged 6 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/confd/src/ietf-syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ static const char *fxlate(const char *facility)
{
const char *f = facility;

if (!facility)
return "";

if (!strncmp(facility, "ietf-syslog:", 12))
f = &facility[12];
if (!strncmp(facility, "infix-syslog:", 13))
Expand All @@ -75,6 +78,9 @@ static const char *fxlate(const char *facility)
/* handle general syslog excpetions */
static const char *sxlate(const char *severity)
{
if (!severity)
return "";

if (!strcmp(severity, "all"))
return "*";
if (!strcmp(severity, "emergency"))
Expand All @@ -91,6 +97,7 @@ static size_t selector(sr_session_ctx_t *session, struct action *act)
char xpath[strlen(act->xpath) + 32];
sr_val_t *list = NULL;
size_t count = 0;
size_t num = 0;
int rc;

snprintf(xpath, sizeof(xpath), "%s/facility-filter/facility-list", act->xpath);
Expand All @@ -105,17 +112,24 @@ static size_t selector(sr_session_ctx_t *session, struct action *act)
char *facility, *severity;

facility = srx_get_str(session, "%s/facility", entry->xpath);
if (!facility)
continue;
severity = srx_get_str(session, "%s/severity", entry->xpath);
if (!severity) {
free(facility);
continue;
}

fprintf(act->fp, "%s%s.%s", i ? ";" : "", fxlate(facility), sxlate(severity));
num++;

free(facility);
free(severity);
}

sr_free_values(list, count);

return count;
return num;
}

static void action(sr_session_ctx_t *session, const char *name, const char *xpath, struct addr *addr)
Expand All @@ -138,7 +152,8 @@ static void action(sr_session_ctx_t *session, const char *name, const char *xpat
if (!selector(session, &act)) {
/* No selectors, must've been a delete operation after all. */
fclose(act.fp);
remove(act.path);
if (remove(act.path))
ERRNO("failed removing %s", act.path);
return;
}

Expand Down Expand Up @@ -221,10 +236,12 @@ static int file_change(sr_session_ctx_t *session, uint32_t sub_id, const char *m
const char *name;

name = getnm(node, path, sizeof(path));
if (op == LYDX_OP_DELETE)
remove(filename(name, false, path, sizeof(path)));
else
if (op == LYDX_OP_DELETE) {
if (remove(filename(name, false, path, sizeof(path))))
ERRNO("failed removing %s", path);
} else {
action(session, name, path, NULL);
}
}

srx_free_changes(tree);
Expand Down Expand Up @@ -255,7 +272,8 @@ static int remote_change(sr_session_ctx_t *session, uint32_t sub_id, const char

name = getnm(node, path, sizeof(path));
if (op == LYDX_OP_DELETE) {
remove(filename(name, true, path, sizeof(path)));
if (remove(filename(name, true, path, sizeof(path))))
ERRNO("failed removing %s", path);
} else {
struct addr addr;

Expand Down Expand Up @@ -316,7 +334,8 @@ static int server_change(sr_session_ctx_t *session, uint32_t sub_id, const char
return SR_ERR_OK;

if (!srx_enabled(session, "%s/enabled", xpath)) {
remove(SYSLOG_SERVER);
if (remove(SYSLOG_SERVER))
ERRNO("failed disabling syslog server");
goto done;
}

Expand Down
50 changes: 28 additions & 22 deletions src/confd/src/ietf-system.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static char *strip_quotes(char *str)
return str;
}

static void setvar(char *line, const char *key, char **var)
static void setvar(const char *line, const char *key, char **var)
{
char *ptr;

Expand Down Expand Up @@ -307,9 +307,9 @@ static int sys_reload_services(void)
static int change_clock(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
const char *xpath, sr_event_t event, unsigned request_id, void *priv)
{
char *timezone;
char *tz_utc_offset;
const char *tz_utc_offset;
char tz_name[14];
char *timezone;

switch (event) {
case SR_EV_ENABLED: /* first time, on register. */
Expand Down Expand Up @@ -369,7 +369,7 @@ static int change_clock(sr_session_ctx_t *session, uint32_t sub_id, const char *
}

static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
const char *xpath, sr_event_t event, unsigned request_id, void *priv)
const char *_, sr_event_t event, unsigned request_id, void *priv)
{
sr_change_iter_t *iter = NULL;
int rc, err = SR_ERR_OK;
Expand Down Expand Up @@ -439,7 +439,7 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo

for (size_t i = 0; i < cnt; i++) {
const char *xpath = val[i].xpath;
char *type, *ptr, *name;
char *ptr, *name;
int server = 0;
FILE *fp;

Expand All @@ -460,7 +460,8 @@ static int change_ntp(sr_session_ctx_t *session, uint32_t sub_id, const char *mo
/* Get /ietf-system:system/ntp/server[name='foo'] */
ptr = srx_get_str(session, "%s/udp/address", xpath);
if (ptr) {
type = srx_get_str(session, "%s/association-type", xpath);
char *type = srx_get_str(session, "%s/association-type", xpath);

fprintf(fp, "%s %s", type ?: "server", ptr);
server++;
if (type)
Expand Down Expand Up @@ -688,7 +689,8 @@ static int is_valid_username(const char *user)

static char *sys_find_usable_shell(sr_session_ctx_t *sess, char *name, bool is_admin)
{
char *shell = NULL, *conf = NULL;
const char *conf = NULL;
char *shell = NULL;
char xpath[256];
sr_data_t *cfg;

Expand Down Expand Up @@ -879,7 +881,6 @@ static int sys_add_user(sr_session_ctx_t *sess, char *name)
static char *change_get_user(struct sr_change *change)
{
sr_xpath_ctx_t state;
struct passwd *pw;
sr_val_t *val;
char *user;

Expand All @@ -895,7 +896,8 @@ static char *change_get_user(struct sr_change *change)
user = strdup(user);
sr_xpath_recover(&state);
if (user) {
pw = getpwnam(user);
const struct passwd *pw = getpwnam(user);

if (!pw) {
/* Skipping, user probably deleted. */
free(user);
Expand Down Expand Up @@ -1198,8 +1200,9 @@ static int lyx_list_is_empty(struct lyd_node *parent, const char *list)
struct lyd_node *elem;
int num = 0;

LYX_LIST_FOR_EACH(parent, elem, list)
LYX_LIST_FOR_EACH(parent, elem, list) {
num++;
}

return num == 0;
}
Expand Down Expand Up @@ -1480,8 +1483,7 @@ static int change_motd_banner(sr_session_ctx_t *session, uint32_t sub_id, const
const char *xpath, sr_event_t event, unsigned request_id, void *priv)
{
const char *fn = "/etc/motd";
unsigned char *raw, *txt;
size_t txt_len;
unsigned char *raw;
char *legacy;
int rc = 0;

Expand All @@ -1498,6 +1500,9 @@ static int change_motd_banner(sr_session_ctx_t *session, uint32_t sub_id, const

raw = (unsigned char *)srx_get_str(session, "%s", xpath);
if (raw) {
unsigned char *txt;
size_t txt_len;

txt = base64_decode(raw, strlen((char *)raw), &txt_len);
if (!txt) {
ERRNO("failed base64 decoding of %s", xpath);
Expand Down Expand Up @@ -1551,18 +1556,19 @@ static int change_editor(sr_session_ctx_t *session, uint32_t sub_id, const char
return SR_ERR_OK;

editor = srx_get_str(session, "%s", xpath);
if (editor) {
for (size_t i = 0; i < NELEMS(map); i++) {
if (strcmp(map[i].editor, editor))
continue;
if (!editor)
return SR_ERR_OK;

erase(alt);
rc = systemf("ln -s %s %s", map[i].path, alt);
if (rc)
ERROR("Failed setting system editor '%s'", map[i].editor);
}
free(editor);
for (size_t i = 0; i < NELEMS(map); i++) {
if (strcmp(map[i].editor, editor))
continue;

erase(alt);
rc = systemf("ln -s %s %s", map[i].path, alt);
if (rc)
ERROR("Failed setting system editor '%s'", map[i].editor);
}
free(editor);

if (rc)
return SR_ERR_SYS;
Expand Down