mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: musl@lists.openwall.com
Subject: [musl] [Bug?] getaddrinfo() fills .ai_canonname when AI_CANONNAME isn't passed
Date: Sat, 27 Jan 2024 16:03:32 +0100	[thread overview]
Message-ID: <2ee42b57d19761973d1bd12e529a97cc00d829f3.camel@gentoo.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 1804 bytes --]

Hello,

While debugging a test suite failure in dnspython [1], we've discovered
that the musl implementation of getaddrinfo() fills .ai_canonname field
even if flags do not include AI_CANONNAME.  We think that this could
incorrect.  Per POSIX:

> If nodename is not null, and if requested by the AI_CANONNAME flag,
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> the ai_canonname field of the first returned addrinfo structure shall
> point to a null-terminated string containing the canonical name
> corresponding to the input nodename; [...]
>
> All fields in socket address structures returned by getaddrinfo() that
> are not filled in through an explicit argument (for example,
> sin6_flowinfo) shall be set to zero.  [2]

I think the correct behavior would be to set the field to zero (i.e.
null) when AI_CANONNAME is not present in flags.


I've reproduced the problem on Gentoo Linux amd64 with musl 1.2.4, using
the following test program:

```
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

int main(void)
{
    struct addrinfo hints = {0};
    struct addrinfo *res;
    struct addrinfo *p;

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;

    if (getaddrinfo("example.com", NULL, &hints, &res) == 0) {
        printf("%s\n", res->ai_canonname ? res->ai_canonname
                                         : "(null)");
        freeaddrinfo(res);
    }
    return 0;
}
```

On glibc system, it produces "(null)".  On musl system, it produces
"example.com".


[1] https://github.com/rthalley/dnspython/issues/1035
[2] https://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html

-- 
Best regards,
Michał Górny


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 512 bytes --]

             reply	other threads:[~2024-01-27 16:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-27 15:03 Michał Górny [this message]
2024-01-27 17:05 ` Markus Wichmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2ee42b57d19761973d1bd12e529a97cc00d829f3.camel@gentoo.org \
    --to=mgorny@gentoo.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).