public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c/9557: gcc  2.95.3 20010315 (SuSE) problem with place of variable
@ 2003-02-03 23:16 jeronimo
  0 siblings, 0 replies; 2+ messages in thread
From: jeronimo @ 2003-02-03 23:16 UTC (permalink / raw)
  To: gcc-gnats


>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:


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

* Re: c/9557: gcc  2.95.3 20010315 (SuSE) problem with place of variable
@ 2003-02-03 23:42 bangerth
  0 siblings, 0 replies; 2+ messages in thread
From: bangerth @ 2003-02-03 23:42 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, jeronimo, nobody

Synopsis: gcc  2.95.3 20010315 (SuSE) problem with place of variable

State-Changed-From-To: open->closed
State-Changed-By: bangerth
State-Changed-When: Mon Feb  3 23:42:38 2003
State-Changed-Why:
    User error -- read sscanf man page:
    -----------------------
      struct lien *con;
      short  port;
        
      con=(struct lien *)malloc(sizeof(struct lien));
      sscanf(n_service, "%d", &port);
      con->adr4.sin_port =1;
    --------------------------
    Using a short with %d is bound to write beyond its bounds.
    You overwrite the value of the con pointer, which afterwards
    points to no-go land. Use an integer with %d and your
    program works again. The fact that it worked by exchanging
    variable declarations just means that instead of the con
    pointer, you overwrote the ok flag.
    
    W.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9557


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

end of thread, other threads:[~2003-02-03 23:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-03 23:16 c/9557: gcc 2.95.3 20010315 (SuSE) problem with place of variable jeronimo
2003-02-03 23:42 bangerth

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).