]> The Tcpdump Group git mirrors - tcpslice/commitdiff
Prefer calloc over malloc 21/head
authorRose <[email protected]>
Sun, 4 Jun 2023 17:13:40 +0000 (13:13 -0400)
committerRose <[email protected]>
Wed, 21 Jun 2023 16:21:36 +0000 (12:21 -0400)
malloc + memset can be better expressed as calloc.

sessions.c
tcpslice.c

index 8aae452bd7dc7d6b91330a937bebfe4440017117..06221fd27fe903bd4d0605acc084a82404895ba3 100644 (file)
@@ -442,7 +442,7 @@ sessions_add(const uint8_t t, const struct tuple4 *addr, const struct session *p
 
   if (!(t & sessions_track_types))
     return NULL;
-  elt = calloc(1, sizeof (struct session));
+  elt = (struct session *) calloc(1, sizeof(struct session));
   elt->addr = *addr;
   elt->type = t;
   elt->id = ++counter;
index fb836eeb46b3bc00a402b328e6d39e8257afb21f..d8582af853fb4985e5c6009a69ab0a7ef58a35db 100644 (file)
@@ -771,10 +771,9 @@ open_files(char *filenames[], const int numfiles)
                error("no input files specified");
 
        /* allocate memory for all the files */
-       states = (struct state *) malloc(sizeof(struct state) * numfiles);
+       states = (struct state *) calloc(numfiles, sizeof(struct state));
        if (! states)
                error("unable to allocate memory for %d input files", numfiles);
-       memset(states, 0, sizeof(struct state) * numfiles);
 
        for (i = 0; i < numfiles; ++i) {
                s = &states[i];
@@ -891,13 +890,11 @@ extract_slice(struct state *states, const int numfiles, const char *write_file_n
        last_state = 0;
        last_hdr.ts.tv_sec = last_hdr.ts.tv_usec = 0;
        last_hdr.caplen = last_hdr.len = 0;
-       last_pkt = (u_char*) malloc(snaplen);
+       last_pkt = (u_char *) calloc(1, snaplen);
 
        if (! last_pkt)
                error("out of memory");
 
-       memset(last_pkt, 0, snaplen);
-
        timersub(start_time, base_time, &relative_start);
        timersub(stop_time, base_time, &relative_stop);