Skip to content

Commit

Permalink
Fixed truncation warning in path_join.
Browse files Browse the repository at this point in the history
../../src/host/path_join.c:164:9: warning: 'strncpy' output truncated
before terminating nul copying as many bytes from a string as its length
[-Wstringop-truncation]
  • Loading branch information
tritao committed Oct 3, 2024
1 parent fe388b5 commit c7a457d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/host/path_join.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "premake.h"
#include <assert.h>
#include <string.h>
#include "path_isabsolute.h"

Expand Down Expand Up @@ -150,6 +151,17 @@ int path_has_deferred_join(lua_State* L)
return 1;
}

// Copy string "in" with at most "insz" chars to buffer "out", which
// is "outsz" bytes long. The output is always 0-terminated. Unlike
// strncpy(), strncpy_t() does not zero fill remaining space in the
// output buffer:
// Credit: https://stackoverflow.com/a/58237928
static char* strncpy_t(char* out, size_t outsz, const char* in, size_t insz){
assert(outsz > 0);
while(--outsz > 0 && insz > 0 && *in) { *out++ = *in++; insz--; }
*out = 0;
return out;
}

int path_resolve_deferred_join(lua_State* L)
{
Expand All @@ -161,8 +173,7 @@ int path_resolve_deferred_join(lua_State* L)
size_t len = strlen(path);
int i;
int numParts = 0;
strncpy(inBuffer, path, len);
inBuffer[len] = '\0';
strncpy_t(inBuffer, sizeof(inBuffer), path, len);
char *parts[0x200];
// break up the string into parts and index the start of each part
nextPart = strchr(inBuffer, DEFERRED_JOIN_DELIMITER);
Expand Down

0 comments on commit c7a457d

Please sign in to comment.