From abeca2d3050957879a9e388dd697610c65469b91 Mon Sep 17 00:00:00 2001 From: "Anthony J. Bentley" Date: Sat, 27 Jan 2018 01:21:23 +0000 Subject: [PATCH] Prefer snprintf to strncpy when outputting C strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit strncpy is designed to output to fixed‐width buffers, not C strings (hence its weird null termination behavior). In this case it happens to work correctly due to the length check but, for style reasons, I would rather use snprintf. Especially in this case, where it shortens the code a bit. --- src/asm/fstack.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/asm/fstack.c b/src/asm/fstack.c index 199a2ed1..e6ec6bce 100644 --- a/src/asm/fstack.c +++ b/src/asm/fstack.c @@ -225,12 +225,8 @@ void fstk_AddIncludePath(char *s) if (NextIncPath == MAXINCPATHS) fatalerror("Too many include directories passed from command line"); - if (strlen(s) >= sizeof(IncludePaths[0])) + if (snprintf(IncludePaths[NextIncPath++], _MAX_PATH, "%s", s) >= _MAX_PATH) fatalerror("Include path too long '%s'", s); - - strncpy(IncludePaths[NextIncPath], s, sizeof(IncludePaths[0])); - - NextIncPath++; } FILE *fstk_FindFile(char *fname)