Mail Archives: geda-user/2023/02/02/12:54:01
"karl AT aspodata DOT se [via geda-user AT delorie DOT com]" <geda-user AT delorie DOT com>
writes:
> Generally, it would be best to use the form:
> strncpy(dst,src,sizeof(dst)-1)
The problem with strncpy is that it doesn't always NUL-terminate the
destination, even in the case above. If the source string length
happens to be the same as the specified size, no NUL is written, you
have a non-terminated string, and it's a security issue assuming it
doesn't just crash.
What is needed is a function that:
1. Copies the whole string, including NUL, if it fits, or
2. Fails safely if it doesn't.
strncpy can't be made to do that. Neither can strlcpy for that matter.
strcpy_s can but it isn't generally available yet.
What we need is something like:
pcb_strcpy (s, d, l)
{
i = strlen(s);
if (i+1 <= l)
memcpy (s, d, i+1)
else
abort()
}
- Raw text -