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
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:
which after one round of evaluating macros expands to:
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:
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:
First of all thanks very much for your explanation, I did most of them blindly. I initialized C and it works now.
uint8_t *C = malloc(sizeof(uint8_t));
First of all thanks very much for your explanation, I did most of them blindly. I initialized C and it works now.
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:
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?
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:
which initializes C to be a pointer to a buffer with space to hold 1 byte.
You then pass that pointer to
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)