Skip to content

Commit d7f4657

Browse files
committed
Fix #107 (ugh, I thought changing from strncpy was a bad idea)
1 parent 8fe4b21 commit d7f4657

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

ini.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static char* lskip(const char* s)
5252
}
5353

5454
/* Return pointer to first char (of chars) or inline comment in given string,
55-
or pointer to null at end of string if neither found. Inline comment must
55+
or pointer to NUL at end of string if neither found. Inline comment must
5656
be prefixed by a whitespace character to register as a comment. */
5757
static char* find_chars_or_comment(const char* s, const char* chars)
5858
{
@@ -71,12 +71,15 @@ static char* find_chars_or_comment(const char* s, const char* chars)
7171
return (char*)s;
7272
}
7373

74-
/* Version of strncpy that ensures dest (size bytes) is null-terminated. */
74+
/* Similar to strncpy, but ensures dest (size bytes) is
75+
NUL-terminated, and doesn't pad with NULs. */
7576
static char* strncpy0(char* dest, const char* src, size_t size)
7677
{
77-
/* Use memcpy instead of strncpy to avoid gcc warnings (see issue #91) */
78-
memcpy(dest, src, size - 1);
79-
dest[size - 1] = '\0';
78+
/* Could use strncpy internally, but it causes gcc warnings (see issue #91) */
79+
size_t i;
80+
for (i = 0; i < size - 1 && src[i]; i++)
81+
dest[i] = src[i];
82+
dest[i] = '\0';
8083
return dest;
8184
}
8285

0 commit comments

Comments
 (0)