path-util: use path_find_first_component() in path_is_valid()

This commit is contained in:
Yu Watanabe
2021-05-28 13:41:23 +09:00
parent 0ee54dd4e2
commit 6636883564
+5 -12
View File
@@ -1038,28 +1038,21 @@ bool filename_is_valid(const char *p) {
}
bool path_is_valid(const char *p) {
if (isempty(p))
return false;
for (const char *e = p;;) {
size_t n;
int r;
r = path_find_first_component(&e, /* accept_dot_dot= */ true, NULL);
if (r < 0)
return false;
/* Skip over slashes */
e += strspn(e, "/");
if (e - p >= PATH_MAX) /* Already reached the maximum length for a path? (PATH_MAX is counted
* *with* the trailing NUL byte) */
return false;
if (*e == 0) /* End of string? Yay! */
return true;
/* Skip over one component */
n = strcspn(e, "/");
if (n > NAME_MAX) /* One component larger than NAME_MAX? (NAME_MAX is counted *without* the
* trailing NUL byte) */
return false;
e += n;
}
}