]> The Tcpdump Group git mirrors - libpcap/commitdiff
Use strdup to make mallocated copies of strings.
authorGuy Harris <[email protected]>
Fri, 27 Mar 2020 21:06:55 +0000 (14:06 -0700)
committerGuy Harris <[email protected]>
Fri, 27 Mar 2020 21:06:55 +0000 (14:06 -0700)
pcap-sita.c

index 992b5b59fe48e1941a00cbe17a996ed235db082d..c7fdf378b0e39b0798728e47704e6bf4d5115196 100644 (file)
@@ -292,12 +292,12 @@ int acn_parse_hosts_file(char *errbuf) {                          /* returns: -1 = error, 0 = OK */
                        snprintf(errbuf, PCAP_ERRBUF_SIZE, "Invalid ACN name in '/etc/hosts'.");        /* warn the user */
                        continue;                                                                                                                                       /* and ignore the entry */
                }
-               if ((ptr2 = (char *)malloc(strlen(ptr) + 1)) == NULL) {
+               ptr2 = strdup(ptr);                                     /* copy the IP address into our malloc'ed memory */
+               if (ptr2 == NULL) {
                        pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
                            errno, "malloc");
                        continue;
                }
-               strcpy(ptr2, ptr);                                                              /* copy the IP address into our malloc'ed memory */
                u = &units[chassis][geoslot];
                u->ip = ptr2;                                                                   /* and remember the whole shebang */
                u->chassis = chassis;
@@ -419,7 +419,6 @@ static void unified_IOP_port_name(char *buf, size_t bufsize, const char *proto,
 
 static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 iftype) {
        iface_t         *iface_ptr, *iface;
-       char            *name;
        char            buf[32];
        char            *proto;
        char            *port;
@@ -434,15 +433,12 @@ static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 if
 
        iface->iftype = iftype;                                 /* remember the interface type of this interface */
 
-       name = malloc(strlen(IOPname) + 1);             /* get memory for the IOP's name */
-        if (name == NULL) {    /* oops, we didn't get the memory requested     */
+       iface->IOPname = strdup(IOPnam);                        /* copy it and stick it into the structure */
+        if (iface->IOPname == NULL) {    /* oops, we didn't get the memory requested     */
                 fprintf(stderr, "Error...couldn't allocate memory for IOPname...value of errno is: %d\n", errno);
                 return NULL;
         }
 
-       strcpy(name, IOPname);                                  /* and copy it in */
-       iface->IOPname = name;                                  /* and stick it into the structure */
-
        if (strncmp(IOPname, "lo", 2) == 0) {
                IOPportnum = atoi(&IOPname[2]);
                switch (iftype) {
@@ -478,15 +474,12 @@ static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 if
                return NULL;
        }
 
-       name = malloc(strlen(buf) + 1);                 /* get memory for that name */
-        if (name == NULL) {    /* oops, we didn't get the memory requested     */
+       iface->name = strdup(buf);                                      /* make a copy and stick it into the structure */
+        if (iface->name == NULL) {    /* oops, we didn't get the memory requested     */
                 fprintf(stderr, "Error...couldn't allocate memory for IOP port name...value of errno is: %d\n", errno);
                 return NULL;
         }
 
-       strcpy(name, buf);                                              /* and copy it in */
-       iface->name = name;                                             /* and stick it into the structure */
-
        if (u->iface == 0) {                                    /* if this is the first name */
                u->iface = iface;                                       /* stick this entry at the head of the list */
        } else {