RE: [ILUG] C string concat question ??

From: Glen Gray (glen at domain antefacto.com)
Date: Wed 02 May 2001 - 18:57:40 IST


On 02 May 2001 18:09:29 +0100, Kenn Humborg wrote:
> > Looking for a simple way to cat two strings into a third.
><SNIP>
> And you are, of course, sure that the memory allocated to strvar1
> has enough space for strvar2? And this is consipicously commented
> in the source code so that the next guy doesn't break it (or, worse
> still, use the same code elsewhere without ensuring this)?
> And you'll check that strdup() doesn't return NULL?
>

Yes, and no 'cause its not there anymore. When it wasn't doing what I
thought I chopped for a
more sensible snprintf. And yes the result of strdup is duely checked.

> See, it's more than just a one-liner in any case... Now stop
> being lazy and write _good_ code as opposed to _cute_ code
> (which is very often broken, fragile, unmaintainable or downright
> insecure):
>
> msg = malloc(strlen(strvar1) + strlen(strvar2) + 1);
> if (msg == NULL) {
> /* argh */
> } else {
> strcpy(msg, strvar1);
> strcat(msg, strvar2);
> }
>

[Bows head] "Yes sir"
Missing the question slightly though. What I was asking was there a
function that did this already that I wasn't aware of, something along
the lines of. I guess not :-)

Thanks for the programming tips though !
Glen



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:10:09 GMT