I get the SIGSEGV – Segmentation Fault on the run-time for the code below when the function (log_msg_send) is called. I read that it is about memory violation but I could not find the reason. I would appreciate for the any suggestions/help.
#define MAXSTRINGLENGTH 128
#define BUFSIZE 512
void log_msg_send(char *message, char *next_hop);
struct routing {
int hop_distance;
char sender_ID[16];
};
struct routing user_list[40] = { [0]={0,0,0,0}};
int main(int argc,char *argv[]){
strcpy(user_list[0].sender_ID,"192.168.001.102");
char message[1000];
strcpy(message,"123456123456");
log_msg_send(message, user_list[0].sender_ID);
return 0;
}
void log_msg_send(char *message, char *next_hop){
char *SRV_IP;
strcpy(SRV_IP, next_hop);
if (sizeof(SRV_IP) == 16){
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[60] ;
strcpy(buf, message);
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
fprintf(stderr, "socket() failed \n");
exit(1);
}
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(33333);
if (inet_aton(SRV_IP, &si_other.sin_addr) == 0) {
fprintf(stderr, "inet_aton() failed \n");
exit(1);
}
if (sendto(s, buf, BUFSIZE, 0,(struct sockaddr *) &si_other, slen)==-1){
fprintf(stderr, "sendto() failed \n");
exit(1);
}
close(s);
}
}
PS. For the people who have the SIGSEGV problem. Most common reasons for SIGSEV problem:
– attempting to execute a program that does not compile correctly. Note that most compilers will not output a binary given a compile-time error.
– a buffer overflow.
– using uninitialized pointers.
– dereferencing NULL pointers.
– attempting to access memory the program does not own.
– attempting to alter memory the program does not own (storage violation).
– exceeding the allowable stack size (possibly due to runaway recursion or an infinite loop)
You don’t allocate memory for
SRV_IPso the
strcpytries to access invalid memory.Then you check
but
SRV_IPis achar*, so its size is whatever sizecharpointers have, usually 8 or 4 bytes. You probably meant the length, so would have to usestrlen.