public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/12511] New: When building with -O2 getsockname may fail
@ 2003-10-04 23:22 john dot phelps at solipsys dot com
  2003-10-04 23:38 ` [Bug c++/12511] " pinskia at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: john dot phelps at solipsys dot com @ 2003-10-04 23:22 UTC (permalink / raw)
  To: gcc-bugs

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>


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

end of thread, other threads:[~2004-04-18 17:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-04 23:22 [Bug c++/12511] New: When building with -O2 getsockname may fail john dot phelps at solipsys dot com
2003-10-04 23:38 ` [Bug c++/12511] " pinskia at gcc dot gnu dot org
2003-10-06 12:46 ` john dot phelps at solipsys dot com
2003-12-15 22:18 ` dhazeghi at yahoo dot com
2004-02-06  1:31 ` [Bug c++/12511] [3.3 Regression] " pinskia at gcc dot gnu dot org
2004-02-15 12:35 ` gdr at gcc dot gnu dot org
2004-03-10 12:21 ` [Bug optimization/12511] " giovannibajo at libero dot it
2004-04-18 17:29 ` gdr at gcc dot gnu dot org

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 read-only IMAP folder(s) and NNTP newsgroup(s).