From: Tobias Stoeckmann Date: Sat, 3 Feb 2024 00:07:58 +0000 (+0100) Subject: lib/chkname.c: Take NUL byte into account X-Git-Tag: 4.15.0-rc2~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=403a2e3771be6b1c0c6fc16b2be65b4d36f759ab;p=thirdparty%2Fshadow.git lib/chkname.c: Take NUL byte into account 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 --- diff --git a/lib/chkname.c b/lib/chkname.c index cbfbff51a..1c5963496 100644 --- a/lib/chkname.c +++ b/lib/chkname.c @@ -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); diff --git a/tests/unit/test_chkname.c b/tests/unit/test_chkname.c index af982940d..e0f9f84b4 100644 --- a/tests/unit/test_chkname.c +++ b/tests/unit/test_chkname.c @@ -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);