Segmentation fault (core dumped)

The following is part of the code I'm working on -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "aes.h"
#include <string.h>
#define CCHMAXPATH 100
 
void encrypt(const char *fileIn, const char *fileOut,
    const unsigned char *key);
 
int main(int argc, char *argv[]) 
{
  unsigned char key[100];
  char buffer[CCHMAXPATH];
  memcpy(key,argv[2],100);
  srand(time(NULL));
 
  aes_init();
  snprintf(key,100,"home/ankita/bin/python/encrypt/%s.encrypted",argv[2]);
  encrypt(argv[1], key, argv[2]);
   
  return 0;
}
 


It compiles properly but when I try to run it like this
./a.out trial.doc abc ('trial.doc' - argv[1] filename and 'abc' - argv[2] key)
it gives the following error -
Segmentation fault (core dumped)

Now, I know this happens when we try to access memory that we can't. So, where am I going wrong here? I'm unable to figure out.


Last edited on
Before accessing argv[n], check the value of argc to see how many parameters are actually present.
1
2
3
4
5
    if (argc < 3)
    {
        printf("only %d parameters present\n", argc);
        return 1;
    } 
Ok, so turns out I have only 1 parameter but I don't get it!

what i type at the command line is -
 
./a.out 

--> 1 parameter
 
./a.out trial.doc

--> Segmentation fault (core dumped)

./a.out trial.doc abc
--> Segmentation fault (core dumped)

Why is this happening? I thought program name is argv[0], trial.doc is argv[1], abc is argv[2]. Am I wrong?
I can't test that right now, I'm on a windows system. But it works according to your expectations for me,

you can list the parameters like this:
1
2
    for (int i=0; i<argc; i++)
        printf("%d:  %s\n", i, argv[i]);
but if there is apparently only one, it will just output the program name.
http://www.cplusplus.com/forum/general/112111/


> Ok, so turns out I have only 1 parameter
I wonder how did you reach that conclusion...
I don't see you printing the number of parameters in your second and third test.
This is what I have done :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "aes.h"
#include <string.h>
#define CCHMAXPATH 100
 
void encrypt(const char *fileIn, const char *fileOut,
    const unsigned char *key);
 
int main(int argc, char *argv[]) 
{
  unsigned char key[100];
  char buffer[CCHMAXPATH];
  int i;
  srand(time(NULL));

 for (i=0; i<argc; i++)
        printf("%d:  %s\n", i, argv[i]);

  aes_init();
  snprintf(key,100,"/home/ankita/bin/python/project/encrypt/%s.encrypted", argv[1]);
  encrypt(argv[1], key, argv[2]);
     
  return 0;
}


Now this is what happens at the commandline :

1
2
3
4
5
$./amaze /home/ankita/Documents/txt/2mb.txt anky
0:  ./amaze
1:  /home/ankita/Documents/txt/2mb.txt
2:  anky
Segmentation fault (core dumped)


So, as expected, argv[1] is the filepath along with filename, argv[2] is the key. Then what is wrong? Which part of memory is inaccessible? Why am I still getting Segmentation fault(core dumped)?
debugger, backtrace
valgrind is a good tool too.


one thing I see is that your output file name (that for some obfuscated reason you put it in the `key' variable) does not refer to a valid location.
Last edited on
Topic archived. No new replies allowed.