]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
path-util: add new path_is_valid() helper
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Oct 2018 16:28:14 +0000 (18:28 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 17 Oct 2018 19:13:02 +0000 (21:13 +0200)
src/basic/path-util.c
src/basic/path-util.h

index f60cf003e75c2db9a9a62f028933484dbc373d9d..2deb176240e55d44d4dee3ef53f5a306e22a221a 100644 (file)
@@ -779,24 +779,32 @@ 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_normalized(const char *p) {
+bool path_is_valid(const char *p) {
 
         if (isempty(p))
                 return false;
 
-        if (dot_or_dot_dot(p))
+        if (strlen(p) >= PATH_MAX) /* PATH_MAX is counted *with* the trailing NUL byte */
                 return false;
 
-        if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
+        return true;
+}
+
+bool path_is_normalized(const char *p) {
+
+        if (!path_is_valid(p))
+                return false;
+
+        if (dot_or_dot_dot(p))
                 return false;
 
-        if (strlen(p)+1 > PATH_MAX)
+        if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
                 return false;
 
         if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
index 49604eab800728358d3caa0437870eb3e1dbab6c..17d31bbd89b2d104ace8c5e529a3eeac23000139 100644 (file)
@@ -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);