Vulnerability Development mailing list archives

Re: Secure coding in C (was Re: Administrivia #4883)


From: crowland () CISCO COM (Craig H. Rowland)
Date: Mon, 17 Jan 2000 13:44:24 -0600


Brian Masney wrote:

]     char *a = something();
]     char *b = something_else();
]     int len = strlen(a) + strlen(b);
]     char *c = malloc(len + 1) || die("malloc");
]     (void) strcat(strcpy(c, a), b);

I'm partial to strncpy(); strcpy is a known hobgoblin to secure
programming.

strncpy() has its problems too (i.e. it doesn't null terminate if the src is
too long). I wrote my own SafeStrncpy() function but I think a better choice
is the strlcpy() and strlcat() functions that come with OpenBSD. Theo
DeRaadt has a presentation on this as well. You should search the OpenBSD
site and source tree for more information. The strlcpy() and strlcat()
functions take care of a lot of nonsense present in the default C string
handling libraries.

-- Craig


Current thread: