diff -Naur whois-4.6.9/whois.c whois/whois.c --- whois-4.6.9/whois.c 2003-12-01 19:15:18.000000000 +0100 +++ whois/whois.c 2003-12-08 01:24:06.000000000 +0100 @@ -76,7 +76,7 @@ /* RIPE flags */ if (strchr(ripeflags, ch)) { for (p = fstring; *p; p++); - sprintf(p--, "-%c ", ch); + snprintf(p--, sizeof(fstring), "-%c ", ch); continue; } if (strchr(ripeflagsp, ch)) { @@ -89,7 +89,7 @@ /* program flags */ switch (ch) { case 'h': - server = q = malloc(strlen(optarg) + 1); + server = q = xmalloc(strlen(optarg) + 1); for (p = optarg; *p && *p != ':'; *q++ = tolower(*p++)); if (*p == ':') port = p + 1; @@ -125,7 +125,7 @@ usage(); /* On some systems realloc only works on non-NULL buffers */ - qstring = malloc(64); + qstring = xmalloc(64); *qstring = '\0'; /* parse other parameters, if any */ @@ -134,11 +134,11 @@ while (1) { qslen += strlen(*argv) + 1 + 1; - qstring = realloc(qstring, qslen); - strcat(qstring, *argv++); + qstring = xrealloc(qstring, qslen); + strncat(qstring, *argv++, qslen-1); if (argc == 1) break; - strcat(qstring, " "); + strncat(qstring, " ", qslen); argc--; } } @@ -423,10 +423,12 @@ { char *buf; int i, isripe = 0; - + /* buflen was always 0 in original patch and buf was allocated + twice /Aye */ /* +2 for \r\n; +1 for NULL */ - buf = malloc(strlen(flags) + strlen(query) + strlen(client_tag) + 4 - + 2 + 1); + int buflen = strlen(flags) + strlen(query) + strlen(client_tag) + 4 + 2 + 1; + buf = xmalloc(buflen); + *buf = '\0'; for (i = 0; ripe_servers[i]; i++) if (strcmp(server, ripe_servers[i]) == 0) { @@ -448,21 +450,21 @@ if (*flags) { if (!isripe && strcmp(server, "whois.corenic.net") != 0) puts(_("Warning: RIPE flags used with a traditional server.")); - strcat(buf, flags); + strncat(buf, flags, buflen); } /* FIXME: /e is not applied to .JP ASN */ if (!isripe && (strcmp(server, "whois.nic.mil") == 0 || strcmp(server, "whois.nic.ad.jp") == 0) && strncasecmp(query, "AS", 2) == 0 && isasciidigit(query[2])) - sprintf(buf, "AS %s", query + 2); /* fix query for DDN */ + snprintf(buf, buflen, "AS %s", query + 2); /* fix query for DDN */ else if (!isripe && strcmp(server, "whois.nic.ad.jp") == 0) { char *lang = getenv("LANG"); /* not a perfect check, but... */ if (!lang || (strncmp(lang, "ja", 2) != 0)) - sprintf(buf, "%s/e", query); /* ask for english text */ + snprintf(buf, buflen, "%s/e", query); /* ask for english text */ else - strcat(buf, query); + strncat(buf, query, buflen); } else - strcat(buf, query); + strncat(buf, query, buflen); return buf; } @@ -505,7 +507,7 @@ if (verb) printf(_("Detected referral to %s on %s.\n"), nq, nh); - strcat(nq, "\r\n"); + strncat(nq, "\r\n", sizeof(nq)); fd = openconn(nh, np); do_query(fd, nq); continue; @@ -531,7 +533,7 @@ FILE *fi; int state = 0; - temp = malloc(strlen(query) + 1 + 2 + 1); + temp = xmalloc(strlen(query) + 1 + 2 + 1); *temp = '='; strcpy(temp + 1, query); strcat(temp, "\r\n"); @@ -549,7 +551,7 @@ for (p = buf; *p != ':'; p++); /* skip until colon */ for (p++; *p == ' '; p++); /* skip colon and spaces */ - ret = malloc(strlen(p) + 1); + ret = xmalloc(strlen(p) + 1); for (q = ret; *p != '\n' && *p != '\r' && *p != ' '; *q++ = *p++) ; /*copy data*/ *q = '\0'; @@ -594,7 +596,7 @@ for (p = buf; *p != ':'; p++); /* skip until colon */ for (p++; *p != ':'; p++); /* skip until 2nd colon */ for (p++; *p == ' '; p++); /* skip colon and spaces */ - ret = malloc(strlen(p) + 1); + ret = xmalloc(strlen(p) + 1); for (q = ret; *p != '\n' && *p != '\r'; *q++ = *p++); /*copy data*/ *q = '\0'; state = 2; @@ -768,6 +770,24 @@ exit(0); } +/* Memory allocation routines */ +void *xmalloc(size_t size) +{ + void *ptr; + + if ((ptr = malloc(size)) == NULL) + err_sys("malloc"); + + return ptr; +} + +void *xrealloc(void *ptr, size_t size) +{ + if ((ptr = realloc(ptr, size)) == NULL) + err_sys("realloc"); + + return ptr; +} /* Error routines */ void err_sys(const char *fmt, ...) diff -Naur whois-4.6.9/whois.h whois/whois.h --- whois-4.6.9/whois.h 2003-12-01 18:21:59.000000000 +0100 +++ whois/whois.h 2003-12-08 01:06:28.000000000 +0100 @@ -24,6 +24,8 @@ char *normalize_domain(const char *); char *convert_6to4(const char *); +void *xmalloc(size_t); +void *xrealloc(void *, size_t); void err_quit(const char *,...); void err_sys(const char *,...);