alleged mis-form with strcat

Discussion in 'C Programming' started by Pieter Droogendijk, Sep 12, 2003.

  1. On 12 Sep 2003 06:44:30 -0700
    (Jon) wrote:
    > using Borland Compiler.


    Doesn't matter, I hope, or this post is off-topic.

    > x[32] = "this is a string"
    >
    > strcat(x,x)
    > strcat(x,x)
    >
    > will produce "this is a stringthis is a stringthis is a stringthis is
    > a stringt"
    >
    > but should produce "this is a stringthis is a stringthis is a
    > stringthis is a string"
    >
    > does anyone know why the additional character from the beginning of
    > the string is being appended at the end?


    The arguments to strcat may not overlap. The way you call it leads to undefined
    behaviour. Anything could happen.

    > in addition does anyone know the source for the strcat function? I am
    > curious as to how they did it.


    www.gnu.org. Download the glibc source package. You'll see why the strings may
    not overlap.

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
     
    Pieter Droogendijk, Sep 12, 2003
    #1
    1. Advertising

  2. Pieter Droogendijk

    Jon Guest

    using Borland Compiler.

    x[32] = "this is a string"

    strcat(x,x)
    strcat(x,x)

    will produce "this is a stringthis is a stringthis is a stringthis is
    a stringt"

    but should produce "this is a stringthis is a stringthis is a
    stringthis is a string"

    does anyone know why the additional character from the beginning of
    the string is being appended at the end?


    in addition does anyone know the source for the strcat function? I am
    curious as to how they did it.
     
    Jon, Sep 12, 2003
    #2
    1. Advertising

  3. Pieter Droogendijk

    Jirka Klaue Guest

    Pieter Droogendijk wrote:
    > (Jon) wrote:
    >>x[32] = "this is a string"
    >>
    >>strcat(x,x)
    >>strcat(x,x)
    >>
    >>will produce "this is a stringthis is a stringthis is a stringthis is
    >>a stringt"
    >>
    >>but should produce "this is a stringthis is a stringthis is a
    >>stringthis is a string"

    ....
    > The arguments to strcat may not overlap. The way you call it leads to undefined
    > behaviour. Anything could happen.


    Furthermore 32 wouldn't be enough, even for *two* copies of "this is a string".

    Jirka
     
    Jirka Klaue, Sep 12, 2003
    #3
  4. In article <>,
    says...
    > using Borland Compiler.
    >
    > x[32] = "this is a string"
    >
    > strcat(x,x)


    "this is a string" requires 17 bytes of storage. Making two copies of
    it requires 33 bytes of storage, so you have not enough space allocated
    even for this first strcat. But worse than that is that the arguments
    to strcat may not overlap. On many machines, I would expect this to
    continue writing to memory until it crashed.

    > strcat(x,x)


    Now we are trying to fit 65 bytes into 32. But anyway, the overlap
    problem still applies.

    >
    > will produce "this is a stringthis is a stringthis is a stringthis is
    > a stringt"
    >
    > but should produce "this is a stringthis is a stringthis is a
    > stringthis is a string"


    No, it should not. See above. You are lucky (some would say unlucky)
    that this did not crash your program.

    Trevor

    >
    > does anyone know why the additional character from the beginning of
    > the string is being appended at the end?
    >
    >
    > in addition does anyone know the source for the strcat function? I am
    > curious as to how they did it.
    >
     
    Trevor Walker, Sep 12, 2003
    #4
  5. Pieter Droogendijk

    Lew Pitcher Guest

    Jon wrote:

    > using Borland Compiler.
    >
    > x[32] = "this is a string"
    >
    > strcat(x,x)
    > strcat(x,x)
    >
    > will produce "this is a stringthis is a stringthis is a stringthis is
    > a stringt"
    >
    > but should produce "this is a stringthis is a stringthis is a
    > stringthis is a string"


    Wrong. Such an action isn't guaranteed to produce anything. The source and
    target strings overlap, and because of this, the results are undefined by
    the standard.

    From the C standard:

    "The strcat function appends a copy of the string pointed to by s2
    (including the terminating null character) to the end of the string
    pointed to by s1. The initial character of s2 overwrites the null
    character at the end of s1.
    => If copying takes place between objects that overlap, the behavior is
    => undefined."

    > does anyone know why the additional character from the beginning of
    > the string is being appended at the end?


    See above

    > in addition does anyone know the source for the strcat function? I am
    > curious as to how they did it.


    There's no one source for strcat. In your case, you should ask Borland for a
    copy of the source code of their standard C library.

    --

    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
     
    Lew Pitcher, Sep 12, 2003
    #5
  6. Pieter Droogendijk

    Tom Zych Guest

    Jon wrote:

    > using Borland Compiler.


    > x[32] = "this is a string"


    > strcat(x,x)
    > strcat(x,x)


    > will produce "this is a stringthis is a stringthis is a stringthis is
    > a stringt"


    Using Linux. man 3 strcat says "The strings may not overlap". Your
    code has just entered a dimension of incorrectness, a dimension of
    undefined behavior. Welcome to the twilight zone ;)

    --
    Tom Zych
    This email address will expire at some point to thwart spammers.
    Permanent address: echo '' | rot13
     
    Tom Zych, Sep 12, 2003
    #6
  7. Pieter Droogendijk

    LibraryUser Guest

    Trevor Walker wrote:
    > says...
    >
    > > using Borland Compiler.
    > >
    > > x[32] = "this is a string"
    > >
    > > strcat(x,x)

    >
    > "this is a string" requires 17 bytes of storage. Making two
    > copies of it requires 33 bytes of storage, so you have not
    > enough space allocated even for this first strcat. But worse
    > than that is that the arguments to strcat may not overlap. On
    > many machines, I would expect this to continue writing to
    > memory until it crashed.
    >
    > > strcat(x,x)

    >
    > Now we are trying to fit 65 bytes into 32. But anyway, the
    > overlap problem still applies.
    >

    .... snip ...
    > >
    > > but should produce "this is a stringthis is a stringthis is a
    > > stringthis is a string"

    >
    > No, it should not. See above. You are lucky (some would say
    > unlucky) that this did not crash your program.
    > >

    .... snip ...
    > >
    > > in addition does anyone know the source for the strcat
    > > function? I am curious as to how they did it.


    It is very simple, and you should be able to generate such with
    no problem. First you find where to start copying to, and then
    you copy.

    A safer function to use is strlcpy and strlcat, which is
    available in the BSD distribution and other places. They specify
    the size of the destination in the calls, which avoids the silly
    overflows you have perpetrated above. You can find one
    implementation of them at:

    <http://cbfalconer.home.att.net/download/>

    --
    Replies should be to the newsgroup
    Chuck Falconer, on vacation.
     
    LibraryUser, Sep 13, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VFdFQg==?=

    IIS mis-Configuration? SSI referencing .aspx file

    =?Utf-8?B?VFdFQg==?=, Jul 27, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    3,353
    Steven Cheng[MSFT]
    Jul 29, 2005
  2. Ben_
    Replies:
    16
    Views:
    684
    Neomorph
    Aug 5, 2003
  3. Replies:
    1
    Views:
    306
    Grant Edwards
    Jul 20, 2004
  4. Jacob
    Replies:
    4
    Views:
    421
    Chris Uppal
    Dec 3, 2006
  5. Replies:
    4
    Views:
    313
    Alf P. Steinbach
    Feb 21, 2007
Loading...

Share This Page