strcpy -> strncpy (or strlcpy on BSD)
strcat -> strncat (or strlcat on BSD)
sprintf -> snprintf (but still watch out for printf format attacks)
gets -> something else entirely. The man page says "programs should NEVER use gets()".
strcpy -> strncpy (or strlcpy on BSD)
strcat -> strncat (or strlcat on BSD)
sprintf -> snprintf (but still watch out for printf format attacks)
gets -> something else entirely. The man page says "programs should NEVER use gets()".
They told me that... ˚sob˚ but they were wrong.
Programs (often) handle text. Apparently, that pretty much means you're fucked. So what is a reasonable way to write such programs?
Edited to add:
Apparently there is this: http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source/cordh...
The problem isn't necessarily the functions themselves, it's coders who make assumptions that don't pan out to be true.
struct string {
char *str;
unsigned length;
} "%.*s"
or possibly even "%*.*s"
if you want it space padded. In principle you can bound your space usage and avoid an snprintf with such constructs; in practice, it's probably better to still use snprintf (if you're using standard-library string functions at all).This is useful for copying a string into a fixed size buffer that is sent over the network, to give an example. It's not what the programmer generally means when using strncpy.
Many programmers are surprised when they learn that strncpy() really writes 1M-strlen("abc") zeros in the 1M char array every time it's called...
This is false.
From the man page: "The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated."
And the following code prints foo:ar on my system.
char buffer[10];
strcpy(buffer, "foobar");
strcpy(buffer, "foo");
printf("%s:%s\n", buffer, buffer+4);
Edited to add: huh, scratch that. Obvious error in above test :-P. Testing it with strncat like I had meant to, it seems it is in fact padded, not just (possibly) terminated. Interesting, and very worth knowing if you are trying to move a probably small string to a large buffer under time pressure.Not sure what you want to say with the example code. Maybe swap it for strncpy and strlcpy and see whether that matches your expectations?
However, despite the n functions being generally safer, you're still propagating misinformation by touting them as secure alternatives.
The original use of the n functions was to manipulate strings in matters of fixed size arrays. If you don't know what you're doing and just blindly use strncpy() as a strcpy() replacement, you could end up truncating your strings.
OpenBSD's l functions, on the other hand, were specifically designed with security in mind.