env-util: modernize *_is_valid()

This commit is contained in:
Mike Yuan
2025-05-28 19:57:01 +02:00
parent c51379e6d8
commit a9b053deb1
2 changed files with 15 additions and 13 deletions

View File

@@ -33,7 +33,7 @@ static bool env_name_is_valid_n(const char *e, size_t n) {
if (n == SIZE_MAX)
n = strlen_ptr(e);
if (n <= 0)
if (n == 0)
return false;
assert(e);
@@ -44,7 +44,7 @@ static bool env_name_is_valid_n(const char *e, size_t n) {
/* POSIX says the overall size of the environment block cannot be > ARG_MAX, an individual assignment
* hence cannot be either. Discounting the equal sign and trailing NUL this hence leaves ARG_MAX-2 as
* longest possible variable name. */
if (n > (size_t) sysconf(_SC_ARG_MAX) - 2)
if (_unlikely_(n > sc_arg_max() - 2))
return false;
for (const char *p = e; p < e + n; p++)
@@ -55,7 +55,7 @@ static bool env_name_is_valid_n(const char *e, size_t n) {
}
bool env_name_is_valid(const char *e) {
return env_name_is_valid_n(e, strlen_ptr(e));
return env_name_is_valid_n(e, SIZE_MAX);
}
bool env_value_is_valid(const char *e) {
@@ -72,7 +72,7 @@ bool env_value_is_valid(const char *e) {
/* POSIX says the overall size of the environment block cannot be > ARG_MAX, an individual assignment
* hence cannot be either. Discounting the shortest possible variable name of length 1, the equal
* sign and trailing NUL this hence leaves ARG_MAX-3 as longest possible variable value. */
if (strlen(e) > sc_arg_max() - 3)
if (_unlikely_(strlen(e) > sc_arg_max() - 3))
return false;
return true;
@@ -81,6 +81,8 @@ bool env_value_is_valid(const char *e) {
bool env_assignment_is_valid(const char *e) {
const char *eq;
assert(e);
eq = strchr(e, '=');
if (!eq)
return false;
@@ -93,13 +95,13 @@ bool env_assignment_is_valid(const char *e) {
/* POSIX says the overall size of the environment block cannot be > ARG_MAX, hence the individual
* variable assignments cannot be either, but let's leave room for one trailing NUL byte. */
if (strlen(e) > sc_arg_max() - 1)
if (_unlikely_(strlen(e) > sc_arg_max() - 1))
return false;
return true;
}
bool strv_env_is_valid(char **e) {
bool strv_env_is_valid(char * const *e) {
STRV_FOREACH(p, e) {
size_t k;
@@ -116,7 +118,7 @@ bool strv_env_is_valid(char **e) {
return true;
}
bool strv_env_name_is_valid(char **l) {
bool strv_env_name_is_valid(char * const *l) {
STRV_FOREACH(p, l) {
if (!env_name_is_valid(*p))
return false;
@@ -128,7 +130,7 @@ bool strv_env_name_is_valid(char **l) {
return true;
}
bool strv_env_name_or_assignment_is_valid(char **l) {
bool strv_env_name_or_assignment_is_valid(char * const *l) {
STRV_FOREACH(p, l) {
if (!env_assignment_is_valid(*p) && !env_name_is_valid(*p))
return false;

View File

@@ -22,12 +22,12 @@ static inline int replace_env(const char *format, char **env, ReplaceEnvFlags fl
int replace_env_argv(char **argv, char **env, char ***ret, char ***ret_unset_variables, char ***ret_bad_variables);
bool strv_env_is_valid(char **e);
#define strv_env_clean(l) strv_env_clean_with_callback(l, NULL, NULL)
char** strv_env_clean_with_callback(char **l, void (*invalid_callback)(const char *p, void *userdata), void *userdata);
bool strv_env_is_valid(char * const *e);
bool strv_env_name_is_valid(char * const *l);
bool strv_env_name_or_assignment_is_valid(char * const *l);
bool strv_env_name_is_valid(char **l);
bool strv_env_name_or_assignment_is_valid(char **l);
char** strv_env_clean_with_callback(char **l, void (*invalid_callback)(const char *p, void *userdata), void *userdata);
#define strv_env_clean(l) strv_env_clean_with_callback(l, NULL, NULL)
char** _strv_env_merge(char **first, ...);
#define strv_env_merge(first, ...) _strv_env_merge(first, __VA_ARGS__, POINTER_MAX)