alleged mis-form with strcat

P

Pieter Droogendijk

On 12 Sep 2003 06:44:30 -0700
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.
 
J

Jon

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.
 
J

Jirka Klaue

Pieter said:
[email protected] (Jon) said:
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
 
T

Trevor Walker

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
 
L

Lew Pitcher

Jon said:
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)
 
T

Tom Zych

Jon said:
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 ;)
 
L

LibraryUser

Trevor said:
(e-mail address removed) 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/>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top