tcpdrop: ensure NUL termination of a string

strncpy did not guarantee NUL termination of the "stack" string.
Use strlcpy instead.  While I'm here, avoid unnecessary memset
and strnlen calls.

Reported by:	Coverity
CID:		1381035
Sponsored by:	Dell EMC
This commit is contained in:
Eric van Gyzen 2018-05-28 01:58:49 +00:00
parent 38cfc8c393
commit 1de11cd4e1

View File

@ -73,7 +73,7 @@ main(int argc, char *argv[])
dropall = false;
dropallstack = false;
memset(stack, 0, TCP_FUNCTION_NAME_LEN_MAX);
stack[0] = '\0';
state = -1;
while ((ch = getopt(argc, argv, "alS:s:")) != -1) {
@ -86,7 +86,7 @@ main(int argc, char *argv[])
break;
case 'S':
dropallstack = true;
strncpy(stack, optarg, TCP_FUNCTION_NAME_LEN_MAX);
strlcpy(stack, optarg, sizeof(stack));
break;
case 's':
dropallstack = true;
@ -260,7 +260,7 @@ tcpdropall(const char *stack, int state)
continue;
/* If requested, skip sockets not having the requested stack. */
if (strnlen(stack, TCP_FUNCTION_NAME_LEN_MAX) > 0 &&
if (stack[0] != '\0' &&
strncmp(xtp->xt_stack, stack, TCP_FUNCTION_NAME_LEN_MAX))
continue;