From: Omar Polo Subject: libpcap: strdup/stravis instead of malloc+strlcpy To: tech@openbsd.org Date: Tue, 27 Aug 2024 22:05:34 +0200 spotted while doing something else. While here also remove the cast from a few malloc() calls. saves a few lines. (nit: bpf_error() doesn't return, but the old code called free after it anyway so i've kept it.) diffstat /usr/src M lib/libpcap/fad-getad.c | 4+ 9- M lib/libpcap/optimize.c | 2+ 2- M lib/libpcap/scanner.l | 7+ 6- 3 files changed, 13 insertions(+), 17 deletions(-) diff /usr/src commit - 4a3697ed1cd0722a9d30bc207fdb67effcbfdd54 path + /usr/src blob - 3693677ffdbf91f5a61e87d02b93efd71123f149 file + lib/libpcap/fad-getad.c --- lib/libpcap/fad-getad.c +++ lib/libpcap/fad-getad.c @@ -90,7 +90,6 @@ add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **all pcap_t *p; pcap_if_t *curdev, *prevdev, *nextdev; int this_instance; - size_t len; /* * Can we open this interface for live capture? @@ -137,26 +136,22 @@ add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **all * Fill in the entry. */ curdev->next = NULL; - len = strlen(name) + 1; - curdev->name = malloc(len); + curdev->name = strdup(name); if (curdev->name == NULL) { (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + "strdup: %s", pcap_strerror(errno)); goto fail; } - strlcpy(curdev->name, name, len); if (description != NULL) { /* * We have a description for this interface. */ - len = strlen(description) + 1; - curdev->description = malloc(len); + curdev->description = strdup(description); if (curdev->description == NULL) { (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + "strdup: %s", pcap_strerror(errno)); goto fail; } - strlcpy(curdev->description, description, len); } curdev->addresses = NULL; /* list starts out as empty */ curdev->flags = 0; blob - c0d3a86304c68bb67a3279097d4dbb0b3c77a95e file + lib/libpcap/optimize.c --- lib/libpcap/optimize.c +++ lib/libpcap/optimize.c @@ -1791,7 +1791,7 @@ opt_init(struct block *root) goto fail1; size1 *= sizeof(*space1); - space1 = (bpf_u_int32 *)malloc(size1); + space1 = malloc(size1); if (space1 == NULL) { fail1: bpf_error("malloc"); @@ -1805,7 +1805,7 @@ fail1: goto fail2; size2 *= sizeof(*space2); - space2 = (bpf_u_int32 *)malloc(size2); + space2 = malloc(size2); if (space2 == NULL) { fail2: free(space1); blob - 0f77ddfbecbb9b534b6ec10e98f80a23b06a3fd1 file + lib/libpcap/scanner.l --- lib/libpcap/scanner.l +++ lib/libpcap/scanner.l @@ -302,12 +302,13 @@ tcp-urg { yylval.i = 0x20; return NUM; } [A-Za-z] { yylval.s = sdup((char *)yytext); return ID; } "\\"[^ !()\n\t]+ { yylval.s = sdup((char *)yytext + 1); return ID; } [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ { - size_t len = strlen(yytext) * 4 + 1; - char *v = malloc(len); - if (v != NULL) - strnvis(v, yytext, len, 0); - bpf_error("illegal token: %s", v); - free(v); + char *v; + + if (stravis(&v, yytext, 0) != -1) { + bpf_error("illegal token: %s", v); + free(v); + } else + bpf_error("illegal token"); } . { char v[5];