ref: abeca2d3050957879a9e388dd697610c65469b91
parent: a7dc86001c892d0e2af9d6d3a95c6869fd9acf75
author: Anthony J. Bentley <[email protected]>
date: Fri Jan 26 20:21:23 EST 2018
Prefer snprintf to strncpy when outputting C strings 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.
--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -225,12 +225,8 @@
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)