]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/chkname.c: Take NUL byte into account
authorTobias Stoeckmann <tobias@stoeckmann.org>
Sat, 3 Feb 2024 00:07:58 +0000 (01:07 +0100)
committerSerge Hallyn <serge@hallyn.com>
Sun, 4 Feb 2024 23:03:12 +0000 (17:03 -0600)
The _SC_LOGIN_NAME_MAX value includes space for the NUL byte. The length
of name must smaller than this value to be valid.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
lib/chkname.c
tests/unit/test_chkname.c

index cbfbff51a04a7cf2c5ec8409ba7f1fb59013ca8f..1c596349623cd2f1b3dace797e48e6b2ef849dc0 100644 (file)
@@ -80,7 +80,7 @@ bool is_valid_user_name (const char *name)
         * User names length are limited by the kernel
         */
        maxlen = sysconf(_SC_LOGIN_NAME_MAX);
-       if (strlen(name) > maxlen)
+       if (strlen(name) >= maxlen)
                return false;
 
        return is_valid_name (name);
index af982940de3d498645b9bf33d6c35f77156285f7..e0f9f84b45f39ecb3c4357d8b24e47cf135ee699 100644 (file)
@@ -134,15 +134,15 @@ test_is_valid_user_name_long(void **state)
        char    *name;
 
        max = sysconf(_SC_LOGIN_NAME_MAX);
-       name = MALLOC(max + 2, char);
+       name = MALLOC(max + 1, char);
        assert_true(name != NULL);
 
-       memset(name, '_', max + 1);
+       memset(name, '_', max);
 
-       name[max + 1] = '\0';
+       name[max] = '\0';
        assert_true(false == is_valid_user_name(name));
 
-       name[max] = '\0';
+       name[max - 1] = '\0';
        assert_true(is_valid_user_name(name));
 
        free(name);