commit 0095352e537f5987d868b5807edc2034b250d7b9
parent 4688751e4ecf04b3af939d761097c679edd9dac2
Author: Naveen Narayanan <zerous@nocebo.space>
Date: Sat, 25 Sep 2021 16:53:55 +0200
Use a static buffer
We don't need to dynamically allocate a buffer in this case as we can
decide the number of bytes we are going to read beforehand.
Diffstat:
1 file changed, 3 insertions(+), 11 deletions(-)
diff --git a/finger.c b/finger.c
@@ -12,6 +12,7 @@
#include <unistd.h>
#define BUFSZ 128
+char buf[BUFSZ];
void
usage(void)
@@ -51,7 +52,7 @@ main(int argc, char **argv)
struct addrinfo hints = { 0 };
struct addrinfo *res, *p;
char user[32], hostname[MAXHOSTNAMELEN+1];
- char *msg;
+ char *msg = buf;
int status, sockfd, byt, len, err;
err = 0;
@@ -71,13 +72,6 @@ main(int argc, char **argv)
goto err1;
}
- msg = malloc(BUFSZ);
- if (!msg) {
- err = 1;
- goto err2;
- }
- memset(msg, 0, BUFSZ);
-
for (p = res; p != NULL; p = p->ai_next) {
sockfd = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
@@ -104,7 +98,7 @@ main(int argc, char **argv)
if (byt == -1) {
fprintf(stderr, "send failed: %s\n", strerror(errno));
err = 1;
- goto err4;
+ goto err3;
}
do {
@@ -114,8 +108,6 @@ main(int argc, char **argv)
} while (byt > 0);
}
-err4:
- free(msg);
err3:
close(sockfd);
err2: