LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Don't know why C program esgfaults after get opt()? (https://www.linuxquestions.org/questions/programming-9/dont-know-why-c-program-esgfaults-after-get-opt-4175464591/)

methodtwo 06-03-2013 01:43 PM

Don't know why C program esgfaults after get opt()?
 
Hi
This is, i think, the relevant part of my C program that segfaults straight after processing options:

Code:

char *usr_nam=0;
char *host=0;

int main(int argc, char *argv[])
{
 int len, l;
 int n=0, c;
 char *cp, *service = "12349";
 char send_auth[128], auth_ok[1];
 int err, err2, zero;
 struct passwd *pwd; 
 sigset_t childmask, oldsigmask;

 memset(&hints, 0, sizeof(hints));
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_family = AF_INET;
 printf("Starting getopt: \n");
 
        opterr = 0;                /* don't want getopt() writing to stderr */
        while ((c = getopt(argc, argv, "h:l:")) != -1) {
                switch (c) {
                case 'l':
                        usr_nam = optarg;
                        printf("usr_nam is %s\n", usr_nam);
                        break;
                case 'h':
                        host = optarg;
                        printf("Host is %s\n", host);
                        break;
               
                }
        }
        /*if (optind >= argc)
                perror("usage: pty [ -d driver -einv ] program [ arg ... ]");*/
 
  argc -= optind;
  argv += optind;

  printf("We are here");
   
  /* We will be writing to sockets so block SIG_PIPE */
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = SIG_IGN;
  if (sigaction(SIGPIPE, &sa, NULL) < 0)
                perror("sigaction failed");

The output that i get from this is:Starting getopt:
Code:

Host is localhost
usr_nam is chuck
Segmentation fault: 11

The code always segfaults whether or not i initialise *host and *usr_nam to NULL or not!
Please could someone let me know or help me regarding why this programming is segfaulting on the printf statement following the option processing code?
Thankx in advance for any help/replies!

johnsfine 06-03-2013 01:49 PM

I don't see how you have concluded the seg fault occurs there.

I would put a \n at the end of the string "We are here" before I would conclude the seg fault was not somewhere far past that point. Without the \n, the text is buffered.

methodtwo 06-03-2013 02:31 PM

I put the \n in the printf statement. I also put this after it:

Code:

if(signal(SIGPIPE, SIG_IGN) == SIG_ERR)
        perror("Can't ignore sigpipe");
 
 childpid = getpid();
 printf("We got as far as here!");

The output was the same. Indicating that the second printf never got executed. Got me puzzled!

output:

Code:

Starting getopt:
Host is localhost
usr_nam is chuck
usage: pty [ -d driver -einv ] program [ arg ... ]: Undefined error: 0
We are here
Segmentation fault: 11

I also fixed the line with the port number on it to:

Code:

char *cp;
char *service = "12349";


dwhitney67 06-03-2013 02:38 PM

Quote:

Originally Posted by methodtwo (Post 4964755)
Got me puzzled!

What puzzles me is that apparently you have not bothered to run the program through a debugger. Step through your code, examining variables (particularly pointers) to see if you are attempting to access a null (or uninitialized) pointer.

ogod 06-03-2013 03:20 PM

I would recommend you always to run the program through valgrind or similar, that is usually really helpful unless you mix e.g. C and Fortran. Furthermore, you have left out the declaration of hints as well. But finally you should allocate space for usr_nam and host an fill these using strcpy or sprintf.

dwhitney67 06-03-2013 03:29 PM

Quote:

Originally Posted by ogod (Post 4964784)
But finally you should allocate space for usr_nam and host an fill these using strcpy or sprintf.

Not really necessary. Space is already allocated on the stack for the strings which are passed in as args. Assigning a pointer to these strings is ok.

methodtwo 06-03-2013 03:44 PM

I made sockfd a global variable, instead of a member of a global struct. I also put \n in all the printf()s. After this the program progresses a lot further. Just a few more things to fix. Thank you very much for all the replies. Yes i will run it through GDB(i usually do this as a last resort)
Thankx very much


All times are GMT -5. The time now is 10:40 AM.