Segmentation fault when debugging in C


 
Thread Tools Search this Thread
Top Forums Programming Segmentation fault when debugging in C
# 8  
Old 09-25-2012
So, you're getting another error. If you want help, you need to tell us what the error is and give us a way to track it down. This is your program we're trying to debug. Most of us don't have access to your system (or any system running contiki os) and don't have (or want) tinyECC packages on our systems. The comments I've given you are just based on reading your code, knowing what generates a segmentation fault, and a few decades of experience writing C programs.

As to your last comment. If I understand correctly, you now have the following four lines of code in your source:
Code:
int xxx = 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20+ 20;
uint8_t *C = malloc(xxx*sizeof(uint8_t));
int C_len = 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20 + 20;
int oct_len = ecc_point2octet(C, C_len, &pbkey_alice, 0);

Wouldn't it make more sense to replace this with:
Code:
int C_len = 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20 + 20;
uint8_t *C = malloc(C_len*sizeof(uint8_t));
int oct_len = ecc_point2octet(C, C_len, &pbkey_alice, 0);

(since xxx is a duplicate of C_len) or, if C_len isn't used anywhere else, with:
Code:
uint8_t *C = malloc((2*KEYDIGITS*NN_DIGIT_LEN+1+20+20)*sizeof(uint8_t));
int oct_len = ecc_point2octet(C, sizeof(C), &pbkey_alice, 0);

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 09-26-2012
thanks for your advice and did up to some level.

In my case I have to deal with 3 different parties. TinyECC(nesc), native c and contiki. What I am doing is convert nesc in to c then use it in contiki. And I don't have much knowledge in c. Smilie . I can't even found it's a segmentation fault or what in contiki. ( they call contiki as embeded os and we don't need to install it, only download package and go in to it and run simulator using 'ant run'). So I extract some codes out and create above source file to get the error.( I don't know way to debug in cooja simulator in contiki) .

But with your help I ported most of codes and correct errors. And I need to check my code is doing right things,,,if you don't mind please look at this code and give me idea how to compare two unsigned integers


Code:
  static void bacast_signed_message()
    {
      uint8_t *M = malloc(MAX_M_LEN*sizeof(uint8_t));//uint8_t M[MAX_M_LEN];//uint8_t *M;
      int M_len = MAX_M_LEN;;
      
      uint8_t *C = malloc((2*KEYDIGITS*NN_DIGIT_LEN + 1 + MAX_M_LEN + HMAC_LEN)*sizeof(uint8_t));	
      int C_len; 
      //uint8_t C[2*KEYDIGITS*NN_DIGIT_LEN + 1 + MAX_M_LEN + HMAC_LEN]; 
      
      uint8_t *dM = malloc(MAX_M_LEN*sizeof(uint8_t));  //uint8_t dM[MAX_M_LEN];
      int dM_len = MAX_M_LEN;
      
      random_data(M, MAX_M_LEN);
    
      printf("C before encrypt %p\n",*C);
      printf("M before encrypt %p\n",*M);
      printf("dM before encrypt %p\n",*dM);
     	
      C_len = encrypt(C, (2*KEYDIGITS*NN_DIGIT_LEN + 1 + M_len + HMAC_LEN), M, M_len, &pbkey_alice);
      //encrypt(uint8_t *C, int C_len, uint8_t *M, int M_len, Point *PublicKey);
      
      printf("C after encrypt %p\n",*C);
      printf("M after encrypt %p\n",*M);
      printf("dM after encrypt %p\n",*dM);
    	
      dM_len = decrypt(dM, dM_len, C, C_len, prKey_alice);   
      //decrypt(uint8_t *M, int M_len, uint8_t *C, int C_len, NN_DIGIT *d);
      
      printf("C after decrypt %p\n",*C);
      printf("M after decrypt %p\n",*M);
      printf("dM after decrypt %p\n",*dM);
    	
      printf("C_len = %i , M_len = %i\n",C_len,M_len);
      
      if (dM == M){printf("Works\n");}
      else{printf("Not Works\n");}
    }

and this is the out put I got

Code:
  C before encrypt 0x40
    M before encrypt 0x28
    dM before encrypt 0x70
    C after encrypt 0x4
    M after encrypt 0x28
    dM after encrypt 0x70
    C after decrypt 0x4
    M after decrypt 0x28
    dM after decrypt 0x28
    C_len = 102 , M_len = 41
    Not Works

And if I changed private_key(no change of public key) then It looks like this

Code:
  C before encrypt 0x40
    M before encrypt 0x28
    dM before encrypt 0x70
    C after encrypt 0x4
    M after encrypt 0x28
    dM after encrypt 0x70
    C after decrypt 0x4
    M after decrypt 0x28
    dM after decrypt 0x70
    C_len = 102 , M_len = 41
    Not Works

That mean dM has not changed no. But I am not sure about this. If I use %u instead of %p it will give me totally different answers (it says if we use %u it'll convert to decimal. Please give me some advice, and tell me wrong whats with.

Thanks in advance sorry for bother you.
# 10  
Old 09-26-2012
Quote:
Originally Posted by chap
thanks for your advice and did up to some level.

... ... ...

That mean dM has not changed no. But I am not sure about this. If I use %u instead of %p it will give me totally different answers (it says if we use %u it'll convert to decimal. Please give me some advice, and tell me wrong whats with.

Thanks in advance sorry for bother you.
You are not bothering me. It is just hard to help when there isn't much debugging data to help analyze the problems. Smilie However, the output you've provided helps. It is also clear from the debugging code you've added and the output you've displayed that you are having problems understanding how pointers work in C. Let me walk you through some of your code and explain what it means.

First, the declarations:
Code:
uint8_t *M = malloc(MAX_M_LEN*sizeof(uint8_t));
uint8_t *C = malloc((2*KEYDIGITS*NN_DIGIT_LEN + 1 + MAX_M_LEN + HMAC_LEN)*sizeof(uint8_t));	
uint8_t *dM = malloc(MAX_M_LEN*sizeof(uint8_t));

create M, C, and dM as pointers to objects of type uint8_t and the calls to malloc() allocate buffers to hold data to be stored into the areas pointed to by those pointers. Then you have:
Code:
printf("C before encrypt %p\n",*C);
printf("M before encrypt %p\n",*M);
printf("dM before encrypt %p\n",*dM);

The %p in these printf format strings are used to convert the value of a pointer (the address to which the pointer points) to a printable value. But the operands you're providing for these conversions are objects of type uint8_t, not pointers. (C is a pointer, *C is the uint8_t object pointed to by that pointer; M is a pointer, *M is the uint8_t object pointed to by M; dM is a pointer, *dM is the uint8_t object pointed to by dM.) If you want to print the contents of the pointers, the above code needs to be changed to:
Code:
printf("C before encrypt %p\n",C);
printf("M before encrypt %p\n",M);
printf("dM before encrypt %p\n",dM);

If you want to print the unsigned byte pointed to be these pointers (both as an unsigned decimal value and as a printable character), the above code needs to be changed to something like:
Code:
printf("*C before encrypt %hhu(%c)\n",*C,*C);
printf("M before encrypt %hhu(%c)\n",*M,*M);
printf("dM before encrypt %hhu(%c)\n",*dM,*dM);

Then your code:
Code:
if (dM == M){printf("Works\n");}
else{printf("Not Works\n");}

is comparing the pointers (not the unsigned 8-bit integers to which they point). Since these pointers were initialized by separate calls to malloc() and not changed since they were initialized, they can't have the same value. They might point to bytes that have the same value, but that would be coded as:
Code:
if (*dM == *M){printf("Works\n");}
else{printf("Not Works\n");}

or as:
Code:
printf("%sWorks\n", (*dM == *M) ? "" : "Not ");

While we're talking about pointers, you may also see an ampersand followed by an object (i.e., &object). This gives you a pointer to the object. So, for example, the following code segment:
Code:
char *ptr = "abcd";
printf("&ptr=%p, ptr=%p, *p=%c\n", &ptr, ptr, *ptr);

will print the address in memory where ptr is allocated (&ptr), the address in memory of the first byte in the string "abcd" (ptr), and the character pointed to by ptr (*ptr) which in this case is 'a'.

I hope this helps.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 09-26-2012
To elaborate what DonCragun explained:

A pointer is a memory address where a certain object is stored. Suppose the line:

Code:
x = "abcd"

Most compilers (not only C-compilers) will do something similar to this: first, allocate 4 bytes to hold "abcd", then label this 4-byte-part of the memory with "x". Subsequent usages of "x" will be dereferenced to this location.

Now suppose you want to pass this variable to a function, but you want the function to modify it somehow. If you just pass the variable as an argument the compiler creates a copy of the data and the function will use this copy. Changes made to this copy are naturally lost once the function ends. Therefore C can pass a pointer instead of the variable itself. By passing the pointer the function gains access not to the copy, but the original and can modify it lastingly.

It is also possible to find out the memory address of an object (the "&" operator DonCragun told you about) and it is possible to get the variable from a pointer address: the "*" operator.

Now, all pointers are of the same format - addresses in memory - and this begs the question why C pointers have types. Regardless of the types all pointers would look the same, no? Yes and no: yes, they look the same. No, they need to be typed because pointers in C are "intelligent": as they know which data they point to, it is possible to use pointer arithmetics.

Suppose you have an array of 32-bit-values. When the program runs this is a series of 4-byte long memory-locations. Now suppose you have a pointer to the first element. If you create a pointer in C and tell it the correct data type the pointer will "know" that the data "unit" it points to is 4 bytes long. an operation like ++ on the pointer will then not increment the address it points to by one (which would be the address of the second byte of the same unit - rather senseless) but by 4, so that the pointer now points to the next units first byte. This way it is easy to go from one to the next element of the array. This would not be possible if your pointer wouldn't know which size the data it points to is.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 12  
Old 09-27-2012
thanks both of you,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Programming

Segmentation fault

I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me. For example this code #include <stdio.h> main() { unsigned long a=0; unsigned long b=0; int z; { printf("Enter two... (2 Replies)
Discussion started by: sizzler786
2 Replies

3. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

4. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

5. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

6. Programming

segmentation fault

What is segmentation fault(core dumped) (1 Reply)
Discussion started by: gokult
1 Replies

7. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies

8. Programming

segmentation fault

ive written my code in C for implementation of a simple lexical analyser using singly linked list hence am making use of dynamic allocation,but when run in linux it gives a segmentation fault is it cause of the malloc function that ive made use of????any suggestions as to what i could do??? thank... (8 Replies)
Discussion started by: rockgal
8 Replies

9. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies

10. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies
Login or Register to Ask a Question