path-util: introduce path_is_safe()
The function is similar to path_is_valid(), but it refuses paths which contain ".." component.
This commit is contained in:
@@ -1037,14 +1037,14 @@ bool filename_is_valid(const char *p) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool path_is_valid(const char *p) {
|
||||
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
|
||||
if (isempty(p))
|
||||
return false;
|
||||
|
||||
for (const char *e = p;;) {
|
||||
int r;
|
||||
|
||||
r = path_find_first_component(&e, /* accept_dot_dot= */ true, NULL);
|
||||
r = path_find_first_component(&e, accept_dot_dot, NULL);
|
||||
if (r < 0)
|
||||
return false;
|
||||
|
||||
|
||||
@@ -155,7 +155,13 @@ int path_extract_filename(const char *p, char **ret);
|
||||
int path_extract_directory(const char *p, char **ret);
|
||||
|
||||
bool filename_is_valid(const char *p) _pure_;
|
||||
bool path_is_valid(const char *p) _pure_;
|
||||
bool path_is_valid_full(const char *p, bool accept_dot_dot) _pure_;
|
||||
static inline bool path_is_valid(const char *p) {
|
||||
return path_is_valid_full(p, true);
|
||||
}
|
||||
static inline bool path_is_safe(const char *p) {
|
||||
return path_is_valid_full(p, false);
|
||||
}
|
||||
bool path_is_normalized(const char *p) _pure_;
|
||||
|
||||
char *file_in_same_dir(const char *path, const char *filename);
|
||||
|
||||
Reference in New Issue
Block a user