Merge pull request #28095 from poettering/generic-valid-version

make version_is_valid() generic
This commit is contained in:
Lennart Poettering
2023-06-20 21:37:46 +02:00
committed by GitHub
11 changed files with 57 additions and 31 deletions
+15 -4
View File
@@ -1124,13 +1124,13 @@ int path_extract_directory(const char *path, char **ret) {
return 0;
}
bool filename_is_valid(const char *p) {
bool filename_part_is_valid(const char *p) {
const char *e;
if (isempty(p))
return false;
/* Checks f the specified string is OK to be *part* of a filename. This is different from
* filename_is_valid() as "." and ".." and "" are OK by this call, but not by filename_is_valid(). */
if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
if (!p)
return false;
e = strchrnul(p, '/');
@@ -1143,6 +1143,17 @@ bool filename_is_valid(const char *p) {
return true;
}
bool filename_is_valid(const char *p) {
if (isempty(p))
return false;
if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
return false;
return filename_part_is_valid(p);
}
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
if (isempty(p))
return false;
+1
View File
@@ -163,6 +163,7 @@ const char *last_path_component(const char *path);
int path_extract_filename(const char *path, char **ret);
int path_extract_directory(const char *path, char **ret);
bool filename_part_is_valid(const char *p) _pure_;
bool filename_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) {
+15
View File
@@ -16,6 +16,7 @@
#include "macro.h"
#include "memory-util.h"
#include "memstream-util.h"
#include "path-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
@@ -1296,3 +1297,17 @@ char *startswith_strv(const char *string, char **strv) {
return found;
}
bool version_is_valid(const char *s) {
if (isempty(s))
return false;
if (!filename_part_is_valid(s))
return false;
/* This is a superset of the characters used by semver. We additionally allow "," and "_". */
if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
return false;
return true;
}
+2
View File
@@ -275,3 +275,5 @@ char *startswith_strv(const char *string, char **strv);
#define STARTSWITH_SET(p, ...) \
startswith_strv(p, STRV_MAKE(__VA_ARGS__))
bool version_is_valid(const char *s);