简体   繁体   中英

C program causing segmentation fault

I have a C program below written on UNIX. I am getting segmentation fault. I am not getting where I am missing something. Can anyone please help.

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
char*                   app_name = NULL;
char*           pInFile = NULL;
int main(int argc, char *argv[])
{
   char*                arg0 = argv[0];
   char*                pdebug = "miecf";
   char*                        pLogfile = NULL;
   char*                pUserid = NULL;
   char*                pOutFile = NULL;
   int            c;

   while( (c = getopt(argc, argv, ":a:d:i:l:u:o")) != EOF)
   {
      switch (c)
      {
         case 'a':
            app_name = optarg;
            break;

         case 'd':
            pdebug = optarg;
            break;

         case 'i':
            pInFile = optarg;
            break;

         case 'l':
            pLogfile = optarg;
            break;

         case 'u':
            pUserid = optarg;
            break;

         case 'o':
            pOutFile = optarg;
            break;

        default:
                fprintf( stderr, "unknown option \'%c\'\n", optopt );
                break;
      } /* switch(c) */
   } /* while( getopt()) */


        printf("app_name is [%s]\n",app_name);
        printf("pdebug is [%s]\n",pdebug);
        printf("pInFile is [%s]\n",pInFile);
        printf("pLogfile is [%s]\n",pLogfile);
        printf("pUserid is [%s]\n",pUserid);
        printf("pOutFile is [%s]\n",pOutFile);

        return 0;
}

Running command

-a test -d deimf -i input.txt -l log.txt -u bc@abc -o out.txt

Output

app_name is [test]
pdebug is [deimf]
pInFile is [input.txt]
pLogfile is [log.txt]
pUserid is [bc@abc]
run[2]: 10448 Segmentation Fault(coredump)

Dbx Report

program terminated by signal SEGV (no mapping at the fault address)
0xff232370: strlen+0x0050:      ld       [%o2], %o1
(dbx) where
=>[1] strlen(0x0, 0xfffffaf0, 0x0, 0xffbff1a8, 0x0, 0x2b), at 0xff232370
  [2] _ndoprnt(0x10f77, 0xffbff26c, 0xffbfe8e9, 0x0, 0x0, 0x0), at 0xff29e4d4
  [3] printf(0x10f68, 0x21100, 0x0, 0x2111e, 0xff3303d8, 0x14), at 0xff2a0680
  [4] main(0xc, 0xffbff304, 0xffbff4ad, 0xffbff4b8, 0x0, 0xffffffff), at 0x10e8

The problem is that pOutFile is NULL when you try and print it. Many OSes (libc) doesn't handle this and you're trying to get it to print a variable that doesn't have a value.

Try this:

if (pOutFile != NULL)
    printf("pOutFile is [%s]\n",pOutFile);
else
    printf("pOutFile is NULL\n");

Added:

pOutFile doesn't have a value even when you specified the -o switch because you didn't put a: after the o in the getopt call. Specifically the:s come after the letter. It should be this:

while( (c = getopt(argc, argv, "a:d:i:l:u:o:")) != EOF)

It looks like you're segfaulting on this line:

    printf("pOutFile is [%s]\n",pOutFile);

Judging by your commandline, you're not using a -o switch, so pOutFile remains NULL, but you're trying to printf it.

Missing : is the problem:

while( (c = getopt(argc, argv, ":a:d:i:l:u:o:")) != EOF)
                                            ^

"Running command -a test -d deimf -i input.txt -l log.txt -u bc@abc out.txt"

You simply forgot to give the -o option:

Running command -a test -d deimf -i input.txt -l log.txt -u bc@abc -o out.txt

You didn't pass -o before "out.txt", so you are dereferencing a null pointer in pOutFile's printf. That's what I noticed on first glance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM