]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
env-util: modernize *_is_valid()
authorMike Yuan <me@yhndnzj.com>
Wed, 28 May 2025 17:57:01 +0000 (19:57 +0200)
committerMike Yuan <me@yhndnzj.com>
Thu, 29 May 2025 19:01:43 +0000 (21:01 +0200)
src/basic/env-util.c
src/basic/env-util.h

index 112c93c181317536ee8c91df41eb56c45b93a651..8a841f057a44ac92ca5060437df9ccb186cc3e78 100644 (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;
index 07426d97146e7278f86416d431419794726711dd..6c25ca3804894f7fcee3b11482a9adba0a6e1853 100644 (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)