commit 3a1c73c2e97f4a8ef071f096bdd90297aa8e4048
parent ff4b3918668cd9572e783646bb3f8db7a188a7e5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Tue, 1 Sep 2020 22:08:11 +0200
dev: Improve error handling in namec()
Namec was missing some cases because next() was not
following the rule of setting errno just in
the place where the error is detected. The change
in next() allowed to simplify the error handing
in namec().
Change-Id: I11a7518980b0d92ba9e978f78f81857a8f508653
Diffstat:
1 file changed, 21 insertions(+), 20 deletions(-)
diff --git a/src/kernel/dev/dev.c b/src/kernel/dev/dev.c
@@ -125,8 +125,10 @@ next(const char *s, char *elem)
t = s;
if (*s != '\0') {
while (*t != '/' && *t != '\0') {
- if (n == NAMELEN)
+ if (n == NAMELEN) {
+ errno = EINVAL;
return NULL;
+ }
elem[n++] = *t++;
}
}
@@ -223,54 +225,53 @@ namec(const char *name, int mode)
int n, i;
const char *s;
Chan *mnt, *c;
- char elem[NAMELEN];
+ char el[NAMELEN];
+ c = NULL;
switch (name[0]) {
case '/':
c = clone(&slash, NULL);
s = name;
break;
case '#':
- if ((s = next(name+1, elem)) == NULL)
+ if ((s = next(name+1, el)) == NULL)
goto noent;
- for (n = 0, i = 1; isdigit(elem[i]); i++)
- n += elem[i] - '0';
+ for (n = 0, i = 1; isdigit(el[i]); i++)
+ n += el[i] - '0';
- if (elem[i] != '\0')
+ if (el[i] != '\0')
goto noent;
- c = attach(elem[0], n);
+ c = attach(el[0], n);
break;
default:
- errno = ENOENT;
- return NULL;
+ goto noent;
}
if (!c)
return NULL;
- for (s = next(s, elem); *elem; s = next(s, elem)) {
+ for (s = next(s, el); s && *el; s = next(s, el)) {
if (c->qid.type != CHDIR)
- goto notfound;
- if (devtab[c->type]->walk(c, elem) < 0) {
- delchan(c);
- return NULL;
- }
+ goto noent;
+ if (devtab[c->type]->walk(c, el) < 0)
+ goto err;
mnt = mntpoint(c->type, c->qid);
- if (mnt)
- clone(mnt, c);
+ if (mnt && clone(mnt, c) < 0)
+ goto err;
}
if (!s)
- goto notfound;
+ goto err;
/* TODO: check mode */
return c;
-notfound:
- delchan(c);
noent:
errno = ENOENT;
+err:
+ if (c)
+ delchan(c);
return NULL;
}