Cygwin: POSIX ipc: fix evaluation of naming rules

The function evaluating correctness of ipc object names was a
bit half-hearted.  Fix the tests to follow more closely the
desriptions in the Linux man pages.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2021-05-21 15:21:29 +02:00
parent 5f621bd38f
commit 5b380b1ca6
1 changed files with 21 additions and 10 deletions

View File

@ -67,22 +67,33 @@ check_path (char *res_name, ipc_type_t type, const char *name, size_t len)
set_errno (EINVAL); set_errno (EINVAL);
return false; return false;
} }
/* Name must not be empty, or just be a single slash, or start with more /* Apart from handling backslash like slash, the naming rules are identical
than one slash. Same for backslash.
Apart from handling backslash like slash, the naming rules are identical
to Linux, including the names and requirements for subdirectories, if to Linux, including the names and requirements for subdirectories, if
the name contains further slashes. */ the name contains further slashes. */
if (!name || (strchr ("/\\", name[0]) /* Name must not be empty and has to start with a slash (or backslash) */
&& (!name[1] || strchr ("/\\", name[1])))) if (!name || !strchr ("/\\", name[0]))
{ {
debug_printf ("Invalid %s name '%s'", ipc_names[type].description, name); debug_printf ("Invalid %s name '%s'", ipc_names[type].description, name);
set_errno (EINVAL); set_errno (EINVAL);
return false; return false;
} }
/* Skip leading (back-)slash. */ /* Name must not consist of just a single slash (or backslash) */
if (strchr ("/\\", name[0])) if (!name[1])
++name; {
if (len > PATH_MAX - ipc_names[type].prefix_len) debug_printf ("Invalid %s name '%s'", ipc_names[type].description, name);
set_errno (ENOENT);
return false;
}
/* Name must not contain slashes after the leading one */
if (strpbrk (name + 1, "/\\"))
{
debug_printf ("Invalid %s name '%s'", ipc_names[type].description, name);
set_errno (EACCES);
return false;
}
/* Length must be less than or equal to NAME_MAX, or NAME_MAX - 4 in
case of semaphores, due to the leading "sem." prefix */
if (len > NAME_MAX - (type == semaphore ? strlen ("sem.") : 0))
{ {
debug_printf ("%s name '%s' too long", ipc_names[type].description, name); debug_printf ("%s name '%s' too long", ipc_names[type].description, name);
set_errno (ENAMETOOLONG); set_errno (ENAMETOOLONG);
@ -90,7 +101,7 @@ check_path (char *res_name, ipc_type_t type, const char *name, size_t len)
} }
__small_sprintf (res_name, "%s/%s%s", ipc_names[type].prefix, __small_sprintf (res_name, "%s/%s%s", ipc_names[type].prefix,
type == semaphore ? "sem." : "", type == semaphore ? "sem." : "",
name); name + 1);
return true; return true;
} }