On 11 March 2014 23:43, Lawrence Teo <[email protected]> wrote:
> This changes a few malloc()+memset() calls to calloc().
>
> OK?
>
>
> Index: gencode.c
> ===================================================================
> RCS file: /cvs/src/lib/libpcap/gencode.c,v
> retrieving revision 1.36
> diff -u -p -r1.36 gencode.c
> --- gencode.c   9 Oct 2010 08:14:36 -0000       1.36
> +++ gencode.c   3 Feb 2014 21:05:40 -0000
> @@ -191,11 +191,10 @@ newchunk(n)
>                 if (k >= NCHUNKS)
>                         bpf_error("out of memory");
>                 size = CHUNK0SIZE << k;
> -               cp->m = (void *)malloc(size);
> +               cp->m = (void *)calloc(1, size);
>                 if (cp->m == NULL)
>                         bpf_error("out of memory");
> -
> -               memset((char *)cp->m, 0, size);
> +
>                 cp->n_left = size;
>                 if (n > size)
>                         bpf_error("out of memory");
> Index: pcap-bpf.c
> ===================================================================
> RCS file: /cvs/src/lib/libpcap/pcap-bpf.c,v
> retrieving revision 1.22
> diff -u -p -r1.22 pcap-bpf.c
> --- pcap-bpf.c  3 Dec 2013 00:25:34 -0000       1.22
> +++ pcap-bpf.c  3 Feb 2014 21:03:10 -0000
> @@ -895,13 +895,12 @@ pcap_create(const char *device, char *eb
>  {
>         pcap_t *p;
>
> -       p = malloc(sizeof(*p));
> +       p = calloc(1, sizeof(*p));
>         if (p == NULL) {
>                 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
>                     pcap_strerror(errno));
>                 return (NULL);
>         }
> -       memset(p, 0, sizeof(*p));
>         p->fd = -1;     /* not opened yet */
>
>         p->opt.source = strdup(device);
> Index: pcap.c
> ===================================================================
> RCS file: /cvs/src/lib/libpcap/pcap.c,v
> retrieving revision 1.13
> diff -u -p -r1.13 pcap.c
> --- pcap.c      25 May 2012 01:58:08 -0000      1.13
> +++ pcap.c      3 Feb 2014 21:07:35 -0000
> @@ -612,10 +612,9 @@ pcap_open_dead(int linktype, int snaplen
>  {
>         pcap_t *p;
>
> -       p = malloc(sizeof(*p));
> +       p = calloc(1, sizeof(*p));
>         if (p == NULL)
>                 return NULL;
> -       memset (p, 0, sizeof(*p));
>         p->snapshot = snaplen;
>         p->linktype = linktype;
>         p->fd = -1;
> Index: savefile.c
> ===================================================================
> RCS file: /cvs/src/lib/libpcap/savefile.c,v
> retrieving revision 1.10
> diff -u -p -r1.10 savefile.c
> --- savefile.c  25 May 2012 01:58:08 -0000      1.10
> +++ savefile.c  3 Feb 2014 21:07:59 -0000
> @@ -129,13 +129,12 @@ pcap_fopen_offline(FILE *fp, char *errbu
>         struct pcap_file_header hdr;
>         int linklen;
>
> -       p = (pcap_t *)malloc(sizeof(*p));
> +       p = (pcap_t *)calloc(1, sizeof(*p));
>         if (p == NULL) {
>                 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
>                 return (NULL);
>         }
>
> -       memset((char *)p, 0, sizeof(*p));
>         /*
>          * Set this field so we don't double-close in pcap_close!
>          */
>

As long as you're there, why not eliminate the pointless casts? I
especially like the cast of malloc/calloc to (void *). :-)

..... Ken

Reply via email to