Skip to content

Commit

Permalink
Replace strncpy() with snprintf().
Browse files Browse the repository at this point in the history
This commit (hopefully) fixes an issue with `strncpy()` that Codacy
pointed out.  While it's safe, because I'm using a constant string,
let's write good C code and avoid using `strncpy()`.  This commit
replaces that with a call to `snprintf()` instead, and simplifies
some of the code around that point.
  • Loading branch information
rhymeswithmogul committed Nov 1, 2024
1 parent 03cebdc commit 0b676eb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/aprs-is.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ sendPacket (const char* const restrict server,

/* Connect */
char* timeoutText = malloc(BUFSIZE);
sprintf(timeoutText, " (timeout %ld seconds)", timeout);

if (timeout <= 0)
if (timeout > 0)
{
snprintf(timeoutText, BUFSIZE - 1, " (timeout %ld seconds)", timeout);
}
else
{
strncpy(timeoutText, " (no timeout)", BUFSIZE);
snprintf(timeoutText, 14, " (no timeout)");
}

switch (addressinfo->sa_family)
Expand Down

0 comments on commit 0b676eb

Please sign in to comment.