privilege: factor out a null check

This commit is contained in:
Ed Kellett 2021-01-29 13:13:53 +00:00
parent 02690ad5b2
commit 8b8320596c
4 changed files with 13 additions and 7 deletions

View file

@ -62,6 +62,7 @@ struct PrivilegeSet {
}; };
bool privilegeset_in_set(const struct PrivilegeSet *set, const char *priv); bool privilegeset_in_set(const struct PrivilegeSet *set, const char *priv);
const char **privilegeset_privs(const struct PrivilegeSet *set);
struct PrivilegeSet *privilegeset_set_new(const char *name, const char *privs, PrivilegeFlags flags); struct PrivilegeSet *privilegeset_set_new(const char *name, const char *privs, PrivilegeFlags flags);
struct PrivilegeSet *privilegeset_extend(const struct PrivilegeSet *parent, const char *name, const char *privs, PrivilegeFlags flags); struct PrivilegeSet *privilegeset_extend(const struct PrivilegeSet *parent, const char *name, const char *privs, PrivilegeFlags flags);
struct PrivilegeSet *privilegeset_get(const char *name); struct PrivilegeSet *privilegeset_get(const char *name);

View file

@ -233,6 +233,13 @@ privilegeset_in_set(const struct PrivilegeSet *set, const char *priv)
return found != NULL; return found != NULL;
} }
const char **
privilegeset_privs(const struct PrivilegeSet *set)
{
static const char *no_privs[] = { NULL };
return set->privs != NULL ? set->privs : no_privs;
}
struct PrivilegeSet * struct PrivilegeSet *
privilegeset_set_new(const char *name, const char *privs, PrivilegeFlags flags) privilegeset_set_new(const char *name, const char *privs, PrivilegeFlags flags)
{ {
@ -355,10 +362,8 @@ privilegeset_diff(const struct PrivilegeSet *old, const struct PrivilegeSet *new
{ {
const char *oldpriv = NULL, *newpriv = NULL; const char *oldpriv = NULL, *newpriv = NULL;
int ord = 0; int ord = 0;
if (i < old->size) oldpriv = privilegeset_privs(old)[i];
oldpriv = old->privs[i]; newpriv = privilegeset_privs(new)[j];
if (j < new->size)
newpriv = new->privs[j];
if (oldpriv && newpriv) if (oldpriv && newpriv)
ord = strcmp(oldpriv, newpriv); ord = strcmp(oldpriv, newpriv);
@ -443,7 +448,7 @@ privilegeset_report(struct Client *source_p)
set->name); set->name);
send_multiline_remote_pad(source_p, &me); send_multiline_remote_pad(source_p, &me);
send_multiline_remote_pad(source_p, source_p); send_multiline_remote_pad(source_p, source_p);
for (const char **s = set->privs; s && *s; s++) for (const char **s = privilegeset_privs(set); *s != NULL; s++)
send_multiline_item(source_p, "%s", *s); send_multiline_item(source_p, "%s", *s);
send_multiline_fini(source_p, NULL); send_multiline_fini(source_p, NULL);
} }

View file

@ -1496,7 +1496,7 @@ oper_up(struct Client *source_p, struct oper_conf *oper_p)
sendto_one(source_p, form_str(RPL_YOUREOPER), me.name, source_p->name); sendto_one(source_p, form_str(RPL_YOUREOPER), me.name, source_p->name);
sendto_one_notice(source_p, ":*** Oper privilege set is %s", oper_p->privset->name); sendto_one_notice(source_p, ":*** Oper privilege set is %s", oper_p->privset->name);
send_multiline_init(source_p, " ", ":%s NOTICE %s :*** Oper privs are ", me.name, source_p->name); send_multiline_init(source_p, " ", ":%s NOTICE %s :*** Oper privs are ", me.name, source_p->name);
for (const char **s = oper_p->privset->privs; s && *s; s++) for (const char **s = privilegeset_privs(oper_p->privset); *s != NULL; s++)
send_multiline_item(source_p, "%s", *s); send_multiline_item(source_p, "%s", *s);
send_multiline_fini(source_p, NULL); send_multiline_fini(source_p, NULL);
send_oper_motd(source_p); send_oper_motd(source_p);

View file

@ -95,7 +95,7 @@ static void show_privs(struct Client *source_p, struct Client *target_p)
send_multiline_remote_pad(source_p, source_p); send_multiline_remote_pad(source_p, source_p);
if (target_p->user->privset) if (target_p->user->privset)
for (const char **s = target_p->user->privset->privs; *s != NULL; s++) for (const char **s = privilegeset_privs(target_p->user->privset); *s != NULL; s++)
send_multiline_item(source_p, "%s", *s); send_multiline_item(source_p, "%s", *s);
if (IsOper(target_p)) if (IsOper(target_p))