Skip to content

getpeername() returns 0 instead of failing with ENOTCONN on an unconnected socket #1673

Description

@lukkrusz

Description

When getpeername() is called on a socket that is not connected and has no pre-specified peer, it returns 0 instead of
failing with -1 and setting errno to ENOTCONN.

Expected behavior

getpeername() shall fail with ENOTCONN when the socket is not connected or otherwise has not had the peer pre-specified.

Actual behavior

getpeername() returns 0 and does not set errno.

Observed output:

getpeername(unconnected): ret=0 errno=0 (EOK)

POSIX requirement (if applicable)

POSIX.1-2017 (IEEE Std 1003.1-2017), getpeername(3p), section ERRORS:

"[ENOTCONN] The socket is not connected or otherwise has not had the peer pre-specified."

Minimal reproduction code

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

int main(void)
{
    int fd, ret;
    struct sockaddr_un peer;
    socklen_t len = sizeof(peer);

    fd = socket(AF_UNIX, SOCK_STREAM, 0);
    errno = 0;
    ret = getpeername(fd, (struct sockaddr *)&peer, &len);
    printf("getpeername(unconnected): ret=%d errno=%d (%s)\n", ret, errno, strerror(errno));
    close(fd);
    return 0;
}

Host behavior

getpeername(unconnected): ret=-1 errno=107 (Transport endpoint is not connected)

Revision

Discovered on 62b46f9

Target

Reproduced on ia32-generic-qemu, not tested on other targets yet.

Related tests

  • phoenix-rtos-tests/libc/socket/getpeername.c

These tests are currently skipped on the affected target. After applying a fix, re-enable them and re-run the suite to
verify the fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    POSIXmissing featurebugSomething isn't workinglibphoenix

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions