mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [Bug?] getaddrinfo() fills .ai_canonname when AI_CANONNAME isn't passed
@ 2024-01-27 15:03 Michał Górny
  2024-01-27 17:05 ` Markus Wichmann
  0 siblings, 1 reply; 2+ messages in thread
From: Michał Górny @ 2024-01-27 15:03 UTC (permalink / raw)
  To: musl

[-- 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 --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] [Bug?] getaddrinfo() fills .ai_canonname when AI_CANONNAME isn't passed
  2024-01-27 15:03 [musl] [Bug?] getaddrinfo() fills .ai_canonname when AI_CANONNAME isn't passed Michał Górny
@ 2024-01-27 17:05 ` Markus Wichmann
  0 siblings, 0 replies; 2+ messages in thread
From: Markus Wichmann @ 2024-01-27 17:05 UTC (permalink / raw)
  To: musl; +Cc: Michał Górny

Hi,

I just had a look at the POSIX specification again and noticed that it
doesn't breathe a word about the setting of .ai_canonname in case
AI_CANONNAME is not set. Nor does it say anything about .ai_canonname in
any returned structure other than the first. The case is entirely
unspecified.

Therefore your application must not make assumptions about it in that
case. The test is wrong to assume anything about .ai_canonname if
AI_CANONNAME is not set.

Ciao,
Markus

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-27 17:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-27 15:03 [musl] [Bug?] getaddrinfo() fills .ai_canonname when AI_CANONNAME isn't passed Michał Górny
2024-01-27 17:05 ` Markus Wichmann

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).