NetBSD Problem Report #57390

From jschauma@netmeister.org  Thu May  4 03:32:52 2023
Return-Path: <jschauma@netmeister.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 9C9601A923A
	for <gnats-bugs@gnats.NetBSD.org>; Thu,  4 May 2023 03:32:52 +0000 (UTC)
Message-Id: <20230504033247.42DB78570E@panix.netmeister.org>
Date: Wed,  3 May 2023 23:32:47 -0400 (EDT)
From: jschauma@netmeister.org
Reply-To: jschauma@netmeister.org
To: gnats-bugs@NetBSD.org
Subject: misleading error message in mq_send(3) for empty message
X-Send-Pr-Version: 3.95

>Number:         57390
>Category:       kern
>Synopsis:       misleading error message in mq_send(3) for empty message
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu May 04 03:35:00 +0000 2023
>Last-Modified:  Thu May 04 20:30:01 +0000 2023
>Originator:     Jan Schaumann
>Release:        NetBSD 9.3
>Organization:

>Environment:


System: NetBSD panix.netmeister.org 9.3 NetBSD 9.3 (PANIX-VC) #1: Wed Aug 17 12:34:21 EDT 2022 root@juggler.panix.com:/misc/obj64/misc/devel/netbsd/9.3/src/sys/arch/amd64/compile/PANIX-VC amd64
Architecture: x86_64
Machine: amd64
>Description:

When submitting a message of zero length using mq_send(3), errno is set to EMSGSIZE
in (I think) src/sys/kern/sys_mqueue.c #830:

/* Check the message size limit */
if (msg_len <= 0 || msg_len > mqattr->mq_msgsize) {
        error = EMSGSIZE;
        goto error;
}

strerror(EMSGSIZE) yields "Message too long", but in this case the error is
actually that the message is _too short_.

>How-To-Repeat:

$ cat >m.c <<EOF
#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <mqueue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MQ_PATH             "/mq"
#define MQ_FLAGS            O_WRONLY | O_CREAT | O_EXCL | O_NONBLOCK
#define MQ_PERMS            S_IRUSR | S_IWUSR
#define MQ_MAXMSG           10
#define MQ_MSGSIZE          10
#define MQ_DEFAULT_PRIORITY 0

int
main(int argc, char **argv) {
	mqd_t mq;
	struct mq_attr attr;

	attr.mq_flags = 0;
	attr.mq_maxmsg = MQ_MAXMSG;
	attr.mq_msgsize = MQ_MSGSIZE;
	attr.mq_curmsgs = 0;

	(void)mq_unlink(MQ_PATH);

	if ((mq = mq_open(MQ_PATH, MQ_FLAGS, MQ_PERMS, &attr)) == (mqd_t)-1) {
		err(EXIT_FAILURE, "mq_open");
		/* NOTREACHED */
	}

	for (int i = 1; i < argc; i++) {
		if (mq_send(mq, argv[i], strlen(argv[i]), MQ_DEFAULT_PRIORITY) == -1) {
			(void)fprintf(stderr, "%s: Unable to send message %d: %s\n",
					getprogname(), i, strerror(errno));
		}
	}

	if (mq_close(mq) == -1) {
		err(EXIT_FAILURE, "mq_close");
		/* NOTREACHED */
	}

	(void)mq_unlink(MQ_PATH);

	return EXIT_SUCCESS;
}
EOF
$ cc m.c -lrt
$ ./a.out "" ok 012345678910
a.out: Unable to send message 1: Message too long
a.out: Unable to send message 3: Message too long
$

>Fix:

I don't know if this can be fixed, since EMSGSIZE sounds entirely reasonable,
and it's just the human readable representation that's misleading here, but
reporting this here just in case somebody has a clever idea.

>Audit-Trail:
From: David Holland <dholland-bugs@netbsd.org>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: kern/57390: misleading error message in mq_send(3) for empty
 message
Date: Thu, 4 May 2023 04:54:39 +0000

 On Thu, May 04, 2023 at 03:35:00AM +0000, jschauma@netmeister.org wrote:
  > /* Check the message size limit */
  > if (msg_len <= 0 || msg_len > mqattr->mq_msgsize) {
  >         error = EMSGSIZE;
  >         goto error;
  > }
  > 
  > strerror(EMSGSIZE) yields "Message too long", but in this case the error is
  > actually that the message is _too short_.

 https://pubs.opengroup.org/onlinepubs/007904975/functions/mq_send.html
 doesn't seem to leave room for zero failing. (Note that it can't
 actually be < 0 because it's unsigned.) So maybe it should not do that
 check.

 (That's from 2004 but as usual I can't find the newer ones by searching)

 -- 
 David A. Holland
 dholland@netbsd.org

From: Robert Elz <kre@munnari.OZ.AU>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: kern/57390: misleading error message in mq_send(3) for empty message
Date: Fri, 05 May 2023 03:29:24 +0700

     Date:        Thu,  4 May 2023 04:55:01 +0000 (UTC)
     From:        David Holland <dholland-bugs@netbsd.org>
     Message-ID:  <20230504045501.7D2CE1A923B@mollari.NetBSD.org>

   |  https://pubs.opengroup.org/onlinepubs/007904975/functions/mq_send.html
   |  doesn't seem to leave room for zero failing. (Note that it can't
   |  actually be < 0 because it's unsigned.) So maybe it should not do that
   |  check.
   |  
   |  (That's from 2004 but as usual I can't find the newer ones by searching)

 The latest drafts are no different, for both mq_send() and send() there
 seems to be no POSIX requirement that the length be > 0, and EMSGSIZE is
 (still) for a message too long for the socket (or message queue).

 So, if the rest of the system code can handle it, without causing other
 (kernel or application) failures (need to consider the effects of receiving
 a zero length message) then removing the test for >0 might not hurt.

 However, as long as it remains, EMSGSIZE is the obvious (and correct) error
 to return - despite the strerror(EMSGSIZE) result.   Those strings are based
 upon historical uses of the error codes, and there are several examples where,
 while the original use remains valid, and the associated string applicable for
 that case, more uses of the errno value have been added, where the string does
 not apply, or does not apply nearly as well.

 kre

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2023 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.