472,983 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

strcat and overwritten strings

For starters, I am a C novice. I don't fully understand how strings
work. I am running on a linux box. I compile w/ gcc. I have a string
called 'myabqcommand' that is being overwritten when i use strcat() on
some unrelated strings.

For this code:

char *myabqcommand;

myabqcommand = strtok(x, "-");
printf("%s\n", myabqcommand);

char str2[4] = "cp ";
strcat(str2, ideck);
strcat(str2, ".inp ");
strcat(str2, myfiles);
strcat(str2, " /homecluster1/");
strcat(str2, uname);
printf("%s\n", str2);
system(str2);

//INCORRECT STRING
printf("%s\n", myabqcommand);

I get this output:
rsh cluster1 abaqus j=A user=new.f
cp A.inp new.f dir1.csv dir2.csv dir3.csv /homecluster1/user1
//RESULT OF INCORRECT STRING
dir3.csv /homecluster1/cicalese
I expected to get this output:
rsh cluster1 abaqus j=A user=new.f
cp A.inp new.f dir1.csv dir2.csv dir3.csv /homecluster1/user1
rsh cluster1 abaqus j=A user=new.f
Can anyone help? Thanks

Feb 23 '06 #1
9 3381
ci******@gmail.com wrote:
[...]
char str2[4] = "cp ";
Here, you create a char array "str2" consisting of 4 characters, and
you initialize it to the 4 characters 'c', 'p', ' ', and '\0';.
strcat(str2, ideck);
strcat(str2, ".inp ");
strcat(str2, myfiles);
strcat(str2, " /homecluster1/");
strcat(str2, uname);


Here, you do nasty things to the memory beyond the 4 characters that
you set aside for str2.

It's called "buffer overrun", and invokes undefined behavior. You
need to make sure that your buffer is large enough to hold the text
that you are going to store in it. One way would to use malloc(),
based on the combined length of all of the strings you are going
to concatenate into it. (Don't forget to free it when you're done
if you use that method.)

[...]

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Feb 23 '06 #2
ci******@gmail.com wrote:
For starters, I am a C novice. I don't fully understand how strings
work. I am running on a linux box. I compile w/ gcc. I have a string
called 'myabqcommand' that is being overwritten when i use strcat() on
some unrelated strings.

For this code:

char *myabqcommand;

myabqcommand = strtok(x, "-");
printf("%s\n", myabqcommand);

char str2[4] = "cp ";
"Map of memory" at this point

--------XXXX--------
^^^^
The X's mark the amount of memory reserved for str2 ('c', 'p', ' ', and
the terminating NUL).
What is to either side of that (if anything) is reserved for something
else.

strcat(str2, ideck);


--------XXXX--------
^^^^^
ideck

If ideck is not an empty sring, You've just tried to use memory that
you don't know exists (or maybe that is reserved for the Operating
System, or that signals demons to start oozing out of your nostrils).

<snip>
For a first newbie approach (I'm a newbie too) try reserving more space
for str2

#define MAXIMUM_STR2_LENGTH 2000

char str2[MAXIMUM_STR2_LENGTH] = "cp ";

if (strlen(str2) + strlen(ideck) < MAXIMUM_STR2_LENGTH) {
strcat(str2, ideck);
} else {
fprintf(stderr, "%d bytes isn't enough for the `myabqcommad'.\n",
MAXIMUM_STR2_LENGTH);
exit(EXIT_FAILURE);
}
--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Feb 23 '06 #3
ci******@gmail.com wrote:
For starters, I am a C novice. I don't fully understand how strings
work. I am running on a linux box. I compile w/ gcc. I have a string
called 'myabqcommand' that is being overwritten when i use strcat() on
some unrelated strings. char str2[4] = "cp ";
So you allocated space for 4 characters, and used all four.
strcat(str2, ideck);

This overwrote the last (null) character, then started writing off into
some other memory.

At that point, you have undefined behavior. Don't do it. You have to
have enough memory allocated ahead of time for the new combined string.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Feb 23 '06 #4

<ci******@gmail.com> wrote in message
news:11*********************@t39g2000cwt.googlegro ups.com...
For starters, I am a C novice. I don't fully understand how strings
work. I am running on a linux box. I compile w/ gcc. I have a string
called 'myabqcommand' that is being overwritten when i use strcat() on
some unrelated strings.

For this code:

char *myabqcommand;

myabqcommand = strtok(x, "-");
printf("%s\n", myabqcommand);

char str2[4] = "cp ";
You have specified storage for a maximum of 4 characters for variable str2.
strcat(str2, ideck);
What is ideck?
strcat(str2, ".inp ");
This will definitely overflow the 4 characters. It can clobber almost
anything. strcat(str2, myfiles); More writing into space you do not own.
strcat(str2, " /homecluster1/"); Same strcat(str2, uname); Same printf("%s\n", str2);
system(str2);

<snip>
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Feb 23 '06 #5
"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.individual.net...
ci******@gmail.com wrote:
For starters, I am a C novice. I don't fully understand how strings
work. I am running on a linux box. I compile w/ gcc. I have a string
called 'myabqcommand' that is being overwritten when i use strcat() on
some unrelated strings.

For this code:

char *myabqcommand;

myabqcommand = strtok(x, "-");
printf("%s\n", myabqcommand);

char str2[4] = "cp ";
For a first newbie approach (I'm a newbie too) try reserving more space
for str2

#define MAXIMUM_STR2_LENGTH 2000

char str2[MAXIMUM_STR2_LENGTH] = "cp ";

if (strlen(str2) + strlen(ideck) < MAXIMUM_STR2_LENGTH)
More correct would be:
if (strlen(str2) + strlen(ideck) +1 < MAXIMUM_STR2_LENGTH)remembering that
the newly created string will need to terminate with '\0'.
{
strcat(str2, ideck);
} else {
fprintf(stderr, "%d bytes isn't enough for the `myabqcommad'.\n",
MAXIMUM_STR2_LENGTH);
exit(EXIT_FAILURE);
}
--
If you're posting through Google read <http://cfaj.freeshell.org/google>

Feb 24 '06 #6
stathis gotsis wrote:
"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.individual.net...

#define MAXIMUM_STR2_LENGTH 2000

char str2[MAXIMUM_STR2_LENGTH] = "cp ";

if (strlen(str2) + strlen(ideck) < MAXIMUM_STR2_LENGTH)


More correct would be:
if (strlen(str2) + strlen(ideck) +1 < MAXIMUM_STR2_LENGTH)remembering that
the newly created string will need to terminate with '\0'.


Why?

#define MAX_LEN 5

char c1[] = "12";
char c2[] = "42";
if (strlen(str2) + strlen(ideck) < MAX_LEN) { /* ok */ }
/* sizeof "1242" == 5 */
char c1[] = "123";
char c2[] = "42";
if (strlen(str2) + strlen(ideck) < MAX_LEN) { /* not ok */ }
/* sizeof "12342" == 6 */
--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Feb 24 '06 #7
"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.individual.net...
stathis gotsis wrote:
"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.individual.net...

#define MAXIMUM_STR2_LENGTH 2000

char str2[MAXIMUM_STR2_LENGTH] = "cp ";

if (strlen(str2) + strlen(ideck) < MAXIMUM_STR2_LENGTH)


More correct would be:
if (strlen(str2) + strlen(ideck) +1 < MAXIMUM_STR2_LENGTH)remembering that the newly created string will need to terminate with '\0'.


Why?

#define MAX_LEN 5

char c1[] = "12";
char c2[] = "42";
if (strlen(str2) + strlen(ideck) < MAX_LEN) { /* ok */ }
/* sizeof "1242" == 5 */
char c1[] = "123";
char c2[] = "42";
if (strlen(str2) + strlen(ideck) < MAX_LEN) { /* not ok */ }
/* sizeof "12342" == 6 */


Sorry my mistake. i meant: strlen(str2)+strlen(ideck)+1<=MAX_LEN, which is
the same as your version.
Feb 24 '06 #8
issue solved. thanks very much to all.

Feb 24 '06 #9
On 23 Feb 2006 12:45:26 -0800, ci******@gmail.com wrote:
For starters, I am a C novice. I don't fully understand how strings
work. I am running on a linux box. I compile w/ gcc. I have a string
called 'myabqcommand' that is being overwritten when i use strcat() on
some unrelated strings.

For this code:

char *myabqcommand;

myabqcommand = strtok(x, "-");
printf("%s\n", myabqcommand);

char str2[4] = "cp ";
strcat(str2, ideck);
strcat(str2, ".inp ");
strcat(str2, myfiles);
strcat(str2, " /homecluster1/");
strcat(str2, uname);
printf("%s\n", str2);
system(str2);

//INCORRECT STRING
printf("%s\n", myabqcommand);

I get this output:
rsh cluster1 abaqus j=A user=new.f
cp A.inp new.f dir1.csv dir2.csv dir3.csv /homecluster1/user1
//RESULT OF INCORRECT STRING
dir3.csv /homecluster1/cicalese
I expected to get this output:
rsh cluster1 abaqus j=A user=new.f
cp A.inp new.f dir1.csv dir2.csv dir3.csv /homecluster1/user1
rsh cluster1 abaqus j=A user=new.f

Post a complete function that exhibits the unexpected behavior.
Remove del for email
Feb 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ian Stanley | last post by:
Hi, Having not done any C programming for a while I am trying to get back into it by converting an old java assignment into C. The problem is I am getting a segmentation fault at runtime which I...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
1
by: alternativa | last post by:
Hello, I'm dealing with a problem concerning strcat.. I would like to concatenate 3 strings, which in fact are integer numbers. My idea is to write something like this: void main (void); {...
6
by: santosh | last post by:
The following code is giving run-time error..... char buf = "Hello" ; strcat(buf,buf) ; Second line is giving the run-time error. Any comments please............
16
by: Lilith | last post by:
I'm working on a simple piece of code under VC++ 6.0. I've got a char Buffer array into which I copy the contents of an MFC control. The string is properly nul terminated. I use strcat (Buffer,...
20
by: ramubdvt | last post by:
hi, i have written this strcat but sometime it is giving problem, while handeling some strings containing binary and if string containing zero , funtion which takes string 1 and its length...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.