Skip to content

Instantly share code, notes, and snippets.

@lattera
Created July 10, 2019 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lattera/579685c0252673c3ad92d2536c3486c7 to your computer and use it in GitHub Desktop.
Save lattera/579685c0252673c3ad92d2536c3486c7 to your computer and use it in GitHub Desktop.
diff --git a/contrib/telnet/telnet/commands.c b/contrib/telnet/telnet/commands.c
index a2dcd26efd30..c6dc4ca064b7 100644
--- a/contrib/telnet/telnet/commands.c
+++ b/contrib/telnet/telnet/commands.c
@@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$");
#include <sys/socket.h>
#include <netinet/in.h>
+#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
@@ -1654,11 +1655,13 @@ env_init(void)
|| (strncmp((char *)ep->value, "unix:", 5) == 0))) {
char hbuf[256+1];
char *cp2 = strchr((char *)ep->value, ':');
+ size_t buflen;
gethostname(hbuf, sizeof(hbuf));
hbuf[sizeof(hbuf)-1] = '\0';
- unsigned int buflen = strlen(hbuf) + strlen(cp2) + 1;
+ buflen = strlen(hbuf) + strlen(cp2) + 1;
cp = (char *)malloc(sizeof(char)*buflen);
+ assert(cp != NULL);
snprintf((char *)cp, buflen, "%s%s", hbuf, cp2);
free(ep->value);
ep->value = (unsigned char *)cp;
@bsdimp
Copy link

bsdimp commented Jul 10, 2019

Any reason to not just use asprintf() instead of malloc + sprintf?

@lattera
Copy link
Author

lattera commented Jul 10, 2019

Because the existing code already used malloc + snprintf?

@lattera
Copy link
Author

lattera commented Jul 10, 2019

And this is telnet, which arguably should be rf -rfed? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment