From 656552ebb354447531dc4a293c6a3da75757eb58 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Oct 2018 18:28:14 +0200 Subject: [PATCH] path-util: add new path_is_valid() helper --- src/basic/path-util.c | 18 +++++++++++++----- src/basic/path-util.h | 1 + 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/basic/path-util.c b/src/basic/path-util.c index f60cf003e7..2deb176240 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -779,7 +779,18 @@ bool filename_is_valid(const char *p) { if (*e != 0) return false; - if (e - p > FILENAME_MAX) + if (e - p > FILENAME_MAX) /* FILENAME_MAX is counted *without* the trailing NUL byte */ + return false; + + return true; +} + +bool path_is_valid(const char *p) { + + if (isempty(p)) + return false; + + if (strlen(p) >= PATH_MAX) /* PATH_MAX is counted *with* the trailing NUL byte */ return false; return true; @@ -787,7 +798,7 @@ bool filename_is_valid(const char *p) { bool path_is_normalized(const char *p) { - if (isempty(p)) + if (!path_is_valid(p)) return false; if (dot_or_dot_dot(p)) @@ -796,9 +807,6 @@ bool path_is_normalized(const char *p) { if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../")) return false; - if (strlen(p)+1 > PATH_MAX) - return false; - if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./")) return false; diff --git a/src/basic/path-util.h b/src/basic/path-util.h index 49604eab80..17d31bbd89 100644 --- a/src/basic/path-util.h +++ b/src/basic/path-util.h @@ -134,6 +134,7 @@ char* dirname_malloc(const char *path); const char *last_path_component(const char *path); bool filename_is_valid(const char *p) _pure_; +bool path_is_valid(const char *p) _pure_; bool path_is_normalized(const char *p) _pure_; char *file_in_same_dir(const char *path, const char *filename);