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]

c/9557: gcc 2.95.3 20010315 (SuSE) problem with place of variable


>Number:         9557
>Category:       c
>Synopsis:       gcc  2.95.3 20010315 (SuSE) problem with place of variable
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Mon Feb 03 23:16:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     jerome Esteves
>Release:        unknown-1.0
>Organization:
>Environment:
Suse Linux 7.3 ("NO MODIFICATION")
>Description:
the problem is: 
when i put the variable port in his place
i get a segmentation fault. 
if i move his place no error given.
i give you the little code to reproduce the error, feel
free to move the declaration of the variable port or make it int to avoid the problem.

>How-To-Repeat:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>


#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define WARN(E) {perror(E); return NULL;}

struct lien{
    int sd;
    struct sockaddr_in adr4;
};
/*
	the problem is with the variable port
	if i move its location after the variable ok
	i dont get an error
*/

struct lien * cree_socketTCP(char *n_host, char * n_service)
{
    struct lien *con;
    short  port;
    int ok=1;
    struct hostent *host;
    
    con=(struct lien *)malloc(sizeof(struct lien));

    if(n_host!=NULL)
	if((host = gethostbyname(n_host)) == NULL)
	    WARN("gethostbyname:");
        
    if(con ==NULL) WARN("Malloc:");
    if((con->sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
	WARN("socket:");    
    memset(&con->adr4, 0, sizeof(struct sockaddr_in));
    con->adr4.sin_family = AF_INET;
    setsockopt(con->sd, SOL_SOCKET, SO_REUSEADDR, &ok, sizeof(int));
    if( n_service!=NULL)
    {
	if((sscanf(n_service, "%d", &port))==1)
	    con->adr4.sin_port =htons(port);
	
	else
	    WARN("bad port");
    }
    
    if(n_host != NULL)
	con->adr4.sin_addr.s_addr=((struct in_addr *)(host->h_addr))->s_addr;
    
    if(bind(con->sd, (struct sockaddr *)&con->adr4, sizeof(struct sockaddr_in))<0){
	close(con->sd);
	perror("bind:");
	return NULL;
    }
    return con;
}

int main()
{
    cree_socketTCP("0.0.0.0", "1235"); 
}

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


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