简体   繁体   中英

copying data to string inside structure in C gives segmentation fault

I am trying to copy a string (myipaddr) into another string (middle) which is declared inside a structure. I cannot adopt any other way as I have to use this in another larger code.

I am getting segmentation fault on line of memcpy while same string (middle) if I declare inside char *my_ip function, it works fine.

main.c

 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>

 #include <sys/ioctl.h>
 #include <net/if.h>
 #include "private.h"

char *my_ip(char *myniccard, char *myipaddr) {

  address_t *accessor = NULL;

      int fd;
      struct ifreq ifr;

      myipaddr[0]=0;

      fd = socket(AF_INET, SOCK_DGRAM, 0);

      /* I want to get an IPv4 IP address */
      ifr.ifr_addr.sa_family = AF_INET;

      /* I want IP address attached to "eth0" */
      //strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
      strncpy(ifr.ifr_name, myniccard, IFNAMSIZ-1);

      ioctl(fd, SIOCGIFADDR, &ifr);

      close(fd);


      /* display result */
      sprintf(myipaddr,"%s"
        , inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

      memcpy(accessor->middle, myipaddr, 16 * sizeof (char));   // here is segmentation fault.
      printf("Buffer :%s\n",accessor->middle);

      return (myipaddr);
      }   // my_ip



 int main (){

     char addr[22];
     char *card = "wl1";

     char *ip = (char *)malloc(sizeof (char));
     ip = my_ip(card, addr);
     free (ip);

     return 0;
     }

private.h

#ifndef FILE_PRIVATE_HEADER
#define FILE_PRIVATE_HEADER

#include <sys/socket.h> // inet_aton
#include <netinet/in.h> // inet_aton
#include <arpa/inet.h>  // inet_aton
#include "queue.h"
#include "bstrlib.h"

#define PGW_NUM_UE_POOL_MAX 16

typedef struct data {


char middle[PGW_NUM_UE_POOL_MAX];


}address_t;

#endif

The function my_ip returns a pointer to the first character of addr . That pointer is not something you can free .

Lets take these three lines:

 char *ip = (char *)malloc(sizeof (char));
 ip = my_ip(card, addr);
 free (ip);

And take them one by one:

  1. char *ip = (char *)malloc(sizeof (char));

    This line defines the variable ip and initialize it to point to the single byte returned by the malloc call.

  2. ip = my_ip(card, addr);

    This reassigns the pointer, so it no longer points to the memory returned by malloc . Instead it will now point to the first character of addr (since that's what my_ip returns).

  3. free (ip);

    Now you effectively try to do free(&addr[0]) which is invalid.

The simple solution is to not call either malloc nor free .

accessor is a NULL pointer of type address_t :

address_t *accessor = NULL;

and here you are trying to access its member middle using a NULL pointer:

  memcpy(accessor->middle, myipaddr, 16 * sizeof (char));   // here is segmentation fault.

Hence, you are getting segmentation fault on this statement.

Also, there are few other issue's in your code.
Here you allocating memory to ip :

     char *ip = (char *)malloc(sizeof (char));

and in very next statement:

     ip = my_ip(card, addr);

you are assigning my_ip function return value to ip . So, you are loosing the reference of memory allocated in previous statement which is a memory leak.
After this, you are doing

free (ip);

ip will point to myipaddr returned from my_ip function which is nothing but address of local array variable addr passed to my_ip function. Which means you are calling free on a local variable address. You can call only free() something you got from malloc() , calloc() or realloc() function otherwise it causes undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM