This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/12511] New: When building with -O2 getsockname may fail


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12511

           Summary: When building with -O2 getsockname may fail
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: john dot phelps at solipsys dot com
                CC: gcc-bugs at gcc dot gnu dot org

For some odd reason using the htons function causes the getsockname call to 
fail under RedHat 9. A code snippet is included. The mere presence of the 
htons call for the sockaddr_in.port member causes the problem with getsockname 
to occur. Compiling with -O0 will allow things to function properly.

Not sure if this is HTMLized...

<pre>


# include <byteswap.h>
# include <errno.h>
# include <sys/socket.h>
# include <netinet/in.h>

#include <iostream>

// Compile with: g++ -O2 testGetsockname.C -o testGetsockname
//
// Set FIX_THE_PROBLEM to 1 in order to make things work.
//
# define FIX_THE_PROBLEM 0


//-------------------------------------------------------------------------
//
int
main()
{

# if FIX_THE_PROBLEM
  if( false )
  {
    u_short junk = htons( 1234 );
  }
# endif

  // Create a socket to work with.
  //
  int sd = socket( AF_INET, SOCK_DGRAM, 0 );

  // Create a sockaddr structure to work with.
  //
  struct sockaddr saddr;
  socklen_t slen;

  // The getsockname call will fail if htons is used with the sockaddr_in
  // struture below. Note that the mere presence of the htons invocation
  // will cause the getsockname call to fail.
  //
  if( getsockname( sd, &saddr, &slen ) < 0 )
  {
    std::cerr << "getsockname failed: " << errno << std::endl;
    return(-1);
  }
  else
  {
    std::cerr << "getsockname SUCCESS" << std::endl;
  }

  // Create a sockaddr_in to cause the issue.
  //
  struct sockaddr_in sin;
  sin.sin_family      = AF_INET;
  sin.sin_port        = htons( 8000 );      // This is the problem line?
  sin.sin_addr.s_addr = htonl( INADDR_ANY );

  std::cerr << "sin.sin_port = " << sin.sin_port << std::endl;

  return(0);
}


</pre>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]