View Issue Details

IDProjectCategoryView StatusLast Update
00004831003.1(2008)/Issue 7System Interfacespublic2019-06-10 08:55
Reportereblake Assigned Toajosey  
PrioritynormalSeverityObjectionTypeEnhancement Request
Status ClosedResolutionAccepted 
NameEric Blake
OrganizationRed Hat
User Referenceebb.socketpair
Sectionsocketpair
Page Number1970
Line Number62593
Interp Status---
Final Accepted Text
Summary0000483: socketpair should not modify socket_vector on failure
DescriptionThe standard is currently silent on the contents of socket_vector[0]
and socket_vector[1] if socketpair( ) fails, however, in the
implementations that I surveyed, these values were unchanged. Adding
a requirement to enforce this behavior can simplify some coding
styles. Currently, I have to use:

int fd[2] = { -1, -1 };
...
//do some work, which might goto error
if (socketpair(dom, type, protocol, fd) < 0) {
  fd[0] = fd[1] = -1;
  goto error;
}
//do some more work, which might goto error
...
error:
  if (fd[0] != -1)
    close(fd[0]);
  if (fd[1] != -1)
    close(fd[1]);
  //more cleanup;

But if we guarantee that socket_vector is unchanged on error, then
the implementation can rely on the previous contents, and skip the
(re-)assignment of the fd contents to -1 on error, using:

if (socketpair(dom, type, protocol, fd) < 0)
  goto error;

This change is comparable to 0000467 on pipe( ).
Desired ActionAt line 62593 [XSH socketpair RETURN VALUE], change:

otherwise, −1 shall be returned and errno set to indicate the error.

to:

otherwise, −1 shall be returned, the contents of socket_vector left
unmodified, and errno set to indicate the error.
Tagstc2-2008

Relationships

related to 0000467 Closedajosey pipe should not modify fd on failure 
related to 0000520 Closedajosey posix_memalign should not modify memptr on failure 
child of 0000623 Closedajosey poll should not modify fds[i].fd and fds[i].events 

Activities

eblake

2011-08-03 22:57

manager   bugnote:0000918

For the record, I tested with this program:

#define _POSIX_C_SOURCE 200811L
#define __EXTENSIONS__ 1
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/socket.h>
int
main (int argc, char **argv)
{
  int last;
  int fd[2] = {-2,-3};
  int err;
  /* Get to an EMFILE condition. */
  while (1) {
    int fd = open("/dev/null", O_RDONLY);
    if (fd < 0) {
      printf ("after fd %d, open failed with errno %d %s\n",
          last, errno, strerror(errno));
      break;
    }
    last = fd;
  }
  /* Probe behavior */
  err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
  if (err)
    printf ("try 1, socketpair returned %d errno %d %s, fds %d %d\n",
        err, errno, strerror(errno), fd[0], fd[1]);
  else
    printf ("try 1, socketpair succeeded, fds %d %d\n", fd[0], fd[1]);
  if (close(last))
    return 1;
  err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
  if (err)
    printf ("try 2, socketpair returned %d errno %d %s, fds %d %d\n",
        err, errno, strerror(errno), fd[0], fd[1]);
  else
    printf ("try 2, socketpair succeeded, fds %d %d\n", fd[0], fd[1]);
  if (close(0))
    return 1;
  err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
  if (err)
    printf ("try 3, socketpair returned %d errno %d %s, fds %d %d\n",
        err, errno, strerror(errno), fd[0], fd[1]);
  else
    printf ("try 3, socketpair succeeded, fds %d %d\n", fd[0], fd[1]);
  return 0;
}

And on all of AIX, HP-UX, IRIX, Linux, Solaris, Tru64, FreeBSD, OpenBSD,
and MacOS, the output indicated EMFILE with fds still -2 and -3, showing
that it is common implementation practice to leave socket_vector
untouched on error.

Issue History

Date Modified Username Field Change
2011-08-03 22:54 eblake New Issue
2011-08-03 22:54 eblake Status New => Under Review
2011-08-03 22:54 eblake Assigned To => ajosey
2011-08-03 22:54 eblake Name => Eric Blake
2011-08-03 22:54 eblake Organization => Red Hat
2011-08-03 22:54 eblake User Reference => ebb.socketpair
2011-08-03 22:54 eblake Section => socketpair
2011-08-03 22:54 eblake Page Number => 1970
2011-08-03 22:54 eblake Line Number => 62593
2011-08-03 22:54 eblake Interp Status => ---
2011-08-03 22:57 eblake Note Added: 0000918
2011-08-03 22:58 eblake Relationship added related to 0000467
2011-08-11 16:04 jim_pugsley Status Under Review => Resolved
2011-08-11 16:04 jim_pugsley Resolution Open => Accepted
2011-08-11 16:04 jim_pugsley Tag Attached: tc2-2008
2011-11-29 14:16 eblake Relationship added related to 0000520
2013-02-07 16:53 eblake Relationship added child of 0000623
2019-06-10 08:55 agadmin Status Resolved => Closed