'How to Avoid "Wstringop-overflow" Warning When One Has to Rely on Source Length?

I'm getting the Wstringop-overflow warning when using strncat to pass command-line arguments to my program. I understand that the compiler is complaining because I'm using source length to append the destination, but how else can I implement this to prevent the warning? Here is my code:

static char ttyPort[MAX_NAME_SIZE];
bzero(ttyPort,MAX_NAME_SIZE);

strncat(ttyPort, argv[2], strlen(argv[2]) + 1);

Here is the Warning:

../tun/main.c:24:5: warning: ‘strncat’ specified bound depends on the length of the source argument [-Wstringop-overflow=]
     strncat(ttyPort, argv[2], argsLen + 1);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../tun/main.c:23:15: note: length computed here
     argsLen = strlen(argv[2]);
               ^~~~~~~~~~~~~~~

I already tried the following but the compiler still picks up on it:

uint8_t argsLen;
argsLen = strlen(argv[2]);

strncat(ttyPort, argv[2], argsLen + 1);


Solution 1:[1]

You can use the remaining size of the destination array like below, without using any source related lengths. -1 is for the null termination char.

strncat(ttyPort, argv[2], sizeof(ttyPort) - strlen(ttyPort) - 1);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nalaka Rajamanthri