A bug in the function 'strcat'

G

Guest

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
 
J

Jochen Kalmbach [MVP]

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/
 

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

Similar Threads

how can I convert strcat to c# 3
Combinations... 1
USB:Pipe not Opening! 0
I am Shocked 4
Dual core optimizations 3
stat doesn't work in vc.net 2003? 6
Decoding function in vb.net 1
Decode function in vb.net 0

Top