473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

A bug in the function 'strcat'

rw
I have a Microsoft C/C++ compiler version 12.00.8168 installed on my PC
running Windows 2000.

I have no problem to use the MS C/C++ compiler to compile the following 'C'
program and get an executable.
*********************************
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

void initstr(char* istr, int len);
void testcat();

int main(int argc, char** argv) {
testcat();
return 0;
}
void testcat() {
int i=0,j=67;
char f11[13];
char recordIdentity[100];

for (i=0; i<j; i++) {
if (i==10) {
printf("i=%d, j=%d\n",i,j);
strcpy(f11,"1234567890ab");
strcpy(recordIdentity,"<urwg:TimeInstant urwg:type=\"queue\">");
strcat(recordIdentity,f11);
strcat(recordIdentity,"</urwg:TimeInstant>\n");
printf("before string concatenation: j=%d\n",j);
strcat(recordIdentity,"<urwg:TimeInstant urwg:type=\"ready\">");
strcat(recordIdentity,f11);
printf("after string concatenation: j=%d\n",j);
strcat(recordIdentity,"</urwg:TimeInstant>");
}
}
printf("j=%d\n",j);

}
*********************************

But when I run the executable, I get the following incorrect output and
the executable crashes.

Outputs:
****************
i=10, j=67
before string concatenation: j=67
after string concatenation: j=1042446692
j=1042446692

Any ideas?
Thanks for your attention.
Richard

Nov 17 '05 #1
1 1197
Hi rw!
I have no problem to use the MS C/C++ compiler to compile the following 'C'
program and get an executable.
Not all programs which can be compiled are free of bugs ;-)
char f11[13];
char recordIdentity[100];


Your buffer is too small, therefor you get an buffer-overrun which might
overwrite your j-var.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2

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...
2
by: ritchie | last post by:
Hi, Just wanted to ask if anybody has any idea what i'm doing wrong. I have a function which edits a linked list. It works fine. I pass in a pointer to the list plus the value of the item to...
14
by: Stegano | last post by:
I am learning C Programming after working with Java for 5 years. I want to know where can I find the source files for C language itself. For example strcat is a function, which concatenates two...
7
by: Prashanth Badabagni | last post by:
#include<string.h> void (*foobar)(); void main() { char *str; strcpy(str,"Fun1"); strcat(str,"method1");
2
by: collinm | last post by:
hi i got some error with parameter my searchFile function: char *tmp; int size; ....
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
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...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.