Segmentation fault when debugging in C


 
Thread Tools Search this Thread
Top Forums Programming Segmentation fault when debugging in C
# 1  
Old 09-24-2012
Segmentation fault when debugging in C

Dear all,

Currently I am implementing ECC protocols. I used tinyECC package to port ecc to contiki os. This giving me a segmentation fault.

I followed some article Debugging Segmentation Faults and Pointer Problems - Cprogramming.com but I failed to detect the error. Please help me to find that error. Here is my code(part of a code). Error happens in NN_Encode function.

this is some errors occur when debugging

Program received signal SIGSEGV, Segmentation fault.
0x080484c4 in ecc_point2octet (octet=0x80485d0 "UWVS\350i", octet_len=73 'I',
P=0x804a040, compress=0) at octtest.c:108
108 octet[0] = 0x04;

(gdb) info s
#0 0x080484c4 in ecc_point2octet (octet=0x80485d0 "UWVS\350i",
octet_len=73 'I', P=0x804a040, compress=0) at octtest.c:108
#1 0x080485c5 in main () at octtest.c:133

Code:
#include <stdio.h>
#include <stdint.h>
typedef uint32_t NN_DIGIT;
typedef uint64_t NN_DOUBLE_DIGIT;

/* Types for length */
typedef uint8_t NN_UINT;
typedef uint16_t NN_UINT2;

#if defined (SECP128R1) || defined (SECP128R2)
#define KEY_BIT_LEN 128
#else 
#if defined (SECP160K1) || defined (SECP160R1) || defined (SECP160R2) 
#define KEY_BIT_LEN 160
#else 
#if defined (SECP192K1) || defined (SECP192R1)
#define KEY_BIT_LEN 192
#else
#define KEY_BIT_LEN 128
#endif /* 192 */
#endif /* 160 */
#endif /* 128 */

/* Length of digit in bits */
#define NN_DIGIT_BITS 32

/* Length of digit in bytes */
#define NN_DIGIT_LEN (NN_DIGIT_BITS/8)

/* Maximum value of digit */
#define MAX_NN_DIGIT 0xffffffff

/* Number of digits in key
 * used by optimized mod multiplication (ModMultOpt) and optimized mod square (ModSqrOpt)
 *
 */
#define KEYDIGITS (KEY_BIT_LEN/NN_DIGIT_BITS) //5

/* Maximum length in digits */
#define MAX_NN_DIGITS (KEYDIGITS+1)

/* buffer size
 *should be large enough to hold order of base point
 */
#define NUMWORDS MAX_NN_DIGITS

/* the mask for ModSqrOpt */
#define MOD_SQR_MASK1 0x8000000000000000ll
#define MOD_SQR_MASK2 0x0000000100000000ll

typedef struct Point
{
    // point's coordinates
    NN_DIGIT x[NUMWORDS];
    NN_DIGIT y[NUMWORDS];
} Point;

Point pbkey_alice;
void 
NN_Encode(unsigned char *a, NN_UINT len, NN_DIGIT *b, NN_UINT digits)
{
  NN_DIGIT t;
  int j;
  unsigned int i, u;

  for(i = 0, j = len - 1; i < digits && j >= 0; i++) {
    t = b[i];
    for(u = 0; j >= 0 && u < NN_DIGIT_BITS; j--, u += 8) {
      a[j] = (unsigned char)(t >> u);
    }
  }

  for(; j >= 0; j--) {
    a[j] = 0;
  }
}

int ecc_point2octet(uint8_t *octet, NN_UINT octet_len, Point *P, int compress)
{
	if (compress){
		if(octet_len < KEYDIGITS*NN_DIGIT_LEN+1)
	  {//too small octet
		return -1;
    }
    else
    {
		  //compressed point representation
			if((1 & P->y[0]) == 0){
	  	octet[0] = 0x02;
			}
			else
				{
	  			octet[0] = 0x03;
				}
			NN_Encode(octet+1, KEYDIGITS*NN_DIGIT_LEN, P->x, KEYDIGITS);
			return KEYDIGITS*NN_DIGIT_LEN+1;
      }
	}
  else
  {//non compressed
	 if(octet_len < 2*KEYDIGITS*NN_DIGIT_LEN+1)
    {
    
		return -1;
    }
    else
    {
			octet[0] = 0x04;
			NN_Encode(octet+1, KEYDIGITS*NN_DIGIT_LEN, P->x, KEYDIGITS);
			NN_Encode(octet+1+KEYDIGITS*NN_DIGIT_LEN, KEYDIGITS*NN_DIGIT_LEN, P->y, KEYDIGITS);
			return 2*KEYDIGITS*NN_DIGIT_LEN+1;
		}
  }
}

int main()
{
pbkey_alice.x[5] = 0x00000000;
  pbkey_alice.x[4] = 0x21961f69;
  pbkey_alice.x[3] = 0xf02d202b;
  pbkey_alice.x[2] = 0xa4b41f1a;
  pbkey_alice.x[1] = 0x0aa08a86;
  pbkey_alice.x[0] = 0xdf27908d;
    
  pbkey_alice.y[5] = 0x00000000;
  pbkey_alice.y[4] = 0x378e1278;
  pbkey_alice.y[3] = 0x62836d75;
  pbkey_alice.y[2] = 0x7acb7ca4;
  pbkey_alice.y[1] = 0x0dc0ad13;
  pbkey_alice.y[0] = 0x741e287c;
uint8_t *C;
int C_len = 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20 + 20;
int oct_len = ecc_point2octet(C, C_len, &pbkey_alice, 0);

}

# 2  
Old 09-24-2012
Let's start in main(). You initialize six elements of two arrays each of which are allocated five elements. (pbkey_alice is an object of type Point which is defined by:
Code:
typedef struct Point
{
    // point's coordinates
    NN_DIGIT x[NUMWORDS];
    NN_DIGIT y[NUMWORDS];
} Point;

Point pbkey_alice;

which after one round of evaluating macros expands to:
Code:
typedef struct Point
{
    // point's coordinates
    uint32_t x[MAX_NN_DIGITS];
    uint32_t y[MAX_NN_DIGITS];
} Point;

Point pbkey_alice;

and then MAX_NN_DIGITS which expands to (KEYDIGITS+1) which expands to ((KEY_BIT_LEN/NN_DIGIT_BITS)+1) (with a comment saying that KEY_BIT_LEN/NN_DIGIT_BITS is 5) which (since none of the following are defined: SECP128R1, SECP128R2, SECP160K1, SECP160R1, SECP160R2, SECP192K1, and SECP192R1) expands to ((128/32)+1) (note that 128/32 is 4; not 5) which finally evaluates to:
Code:
]typedef struct Point
{
    // point's coordinates
    uint32_t x[5];
    uint32_t y[5];
} Point;

Point pbkey_alice;

If you start off in main() writing into unallocated areas, you should expect that something bad is going to happen.

Then you allocate a pointer uint8_t *C; but do not initialize it. You then pass this uninitialized pointer to ecc_point2octet() which then stores data into the byte pointed to by this uninitialized pointer and then calls NN_Encode() to store more data into subsequent bytes using the same uninitialized pointer. Storing data into areas pointed to by uninitialized (or corrupted) pointers is a very common way to get a segmentation fault.

You're on your own from here.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-24-2012
MySQL

First of all thanks very much for your explanation, I did most of them blindly. I initialized C and it works now. Smilie
uint8_t *C = malloc(sizeof(uint8_t));

thanks in advance
# 4  
Old 09-24-2012
Quote:
Originally Posted by chap
First of all thanks very much for your explanation, I did most of them blindly. I initialized C and it works now. Smilie
uint8_t *C = malloc(sizeof(uint8_t));

thanks in advance
That is not sufficient. You need at least (C_len * sizeof(uint8_t)) bytes, and C_len isn't defined yet where you're initializing C.

Did you fix the fact that you're putting more data into pbkey_alice than it is allocated to hold?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-24-2012
in actual case I am using KEY_BIT_LEN as 160, and I used C_len like this
C_len = 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20 + 20; and use it in function,,,something wrong with it?

and I can't understand "Did you fix the fact that you're putting more data into pbkey_alice than it is allocated to hold?" I think I'm not. could you explain it to me?
# 6  
Old 09-24-2012
Quote:
Originally Posted by chap
in actual case I am using KEY_BIT_LEN as 160, and I used C_len like this
C_len = 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20 + 20; and use it in function,,,something wrong with it?

and I can't understand "Did you fix the fact that you're putting more data into pbkey_alice than it is allocated to hold?" I think I'm not. could you explain it to me?
As I explained in my first message on this thread, the source you showed us sets KEY_BIT_LEN to 128; not to 160.

You said you initialize C using the statement:
Code:
uint8_t *C = malloc(sizeof(uint8_t));

which initializes C to be a pointer to a buffer with space to hold 1 byte.
You then pass that pointer to
Code:
ecc_point2octet(C, C_len, &pbkey_alice, 0);

telling it that C is a pointer to a buffer containing 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20 + 20 bytes.

Do you not see that there is a problem when you tell ecc_point2octet() you are passing it a pointer to a buffer containing 73 bytes when you pass it a pointer to a buffer that has only been allocated 1 byte?
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 09-24-2012
yeah,,something wrong, I'm getting an another error

---------- Post updated at 09:59 PM ---------- Previous update was at 09:37 PM ----------

in nesc code it use like this
uint8_t C[2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20 + 20];

so I used like this,,,can we use like this? 73* 1 bytes?
int xxx = 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20+ 20;
uint8_t *C = malloc(xxx*sizeof(uint8_t));
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