Re: Difference between strcpy() and strcat()?

From: Leor Zolman (leor_at_bdsoft.com)
Date: 03/05/04


Date: Fri, 05 Mar 2004 14:03:52 GMT

On 5 Mar 2004 04:18:39 -0800, pascaldamian@icqmail.com (Pascal Damian)
wrote:
>Here's exactly where I read it, from a post at comp.lang.ruby:
>
>======================== start quote ============================
>> p = (char *)getenv("LD_LIBRARY_PATH");
>>> if ((env = (char *)malloc(512+1+(p==NULL ? 0:strlen(p)))) ==
>NULL) {
>>> fprintf(stderr, "malloc() failed: %s\n", strerror(errno));
>>> exit(1);
>>> }
>>> strcat(env, "LD_LIBRARY_PATH=");
>
>Use strcpy instead of strcat here, content of malloc'ed area is
>not guaranteed.
>======================== end quote ============================
>
>URL: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=200403041635.i24GZh4Y030395%40sharui.nakada.niregi.kanuma.tochigi.jp
>URL: http://tinyurl.com/393yz
>
>Now what does the poster mean by "content of malloc'ed area is not
>guaranteed"?

Along with what Jens said, I think this kinda goes along with what I was
saying. There's more "stuff" to think about when using strcat; Personally,
I would never dream of using it "in place of" strcpy; the question just
wouldn't arise because I'm totally acclimated to what each of them does and
would pick the one most suitable to the task in the first place [and this
would probably be the wrong group to mention that, these days, I'd use
std::string in C++ and try to avoid str*() whenever possible, so I won't
say it... ;-) ]. The question isn't "which one is safer"; the lesson is:
research well what /any/ function does before you use it!
        -leor

Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
           Decryptor at www.bdsoft.com/tools/stlfilt.html



Relevant Pages

  • Re: Undeclared identifier error in file "xlocnum"
    ... I had issues with strcat and strcpy in 1975, and nothing in the intervening 30 years has ... you have done something that includes the safe-string library; ... you should not be using sprintf any longer. ...
    (microsoft.public.vc.mfc)
  • Re: Undeclared identifier error in file "xlocnum"
    ... there is still a reason to use those crappy old functions. ... >your code is used on other platforms and by other compilers you will ... >>strcpy or strcat these days, it is better to adopt safe programming practices. ...
    (microsoft.public.vc.mfc)
  • Re: Difference between strcpy() and strcat()?
    ... Leor Zolman wrote in message news:> "strcpy is safer than strcat because it is easier to check programmatically ... > length of the source string. ... With strcat, on the other hand, you have to ...
    (comp.lang.c)
  • Re: Undeclared identifier error in file "xlocnum"
    ... there is still a reason to use those crappy old functions. ... >strcpy or strcat these days, it is better to adopt safe programming practices. ...
    (microsoft.public.vc.mfc)
  • Re: FindFirstFile, how much faster than FindNextFile?
    ... If you want to see really horrible, hair-raising code, open a recent CUJ ... and check how the guy is creating XML request strings. ... if you ever write strcpy or strcat, THINK REAL HARD ABOUT WHY YOU ...
    (microsoft.public.vc.mfc)