Why does this crash on return?

I read up these forums but didn't see an answer...and when searching with google I'm over my head a bit with that info.
I've no luck in figuring out why this is crashing, any help? This should be so simple:)

When normally compiling a c++ program on my linux box it works perfectly all the time. Now I wanted to add qt support, so I used qmake to create a makefile. When I run the resulting code, I get stack smashing errors in several places. This probably means that I'm being lucky with normal g++ compiling?

An example is below. The program crashes on return on line 51, before it can print the next line back in the calling program ("I'm here 1" is right before returning. Back in the calling sub is "I'm here 2", which it never gets to).

A gdb trace is at the end of this message. Note that I changed all personal directories to "A" since it contains personal name info.


Calling routine (inside a sub called from main)
1
2
	status = s.changeACKdelay( 1, 32.0 );
	printf("I'm here 2\n");


Called function; it exits on line 51. There's a bunch of stuff I left in the routine that's probably not relevant (it's marked as such).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int serialClass::changeACKdelay(int ID, float delay) {

        //===============================================
        //STUFF THAT'S PROBABLY NOT RELEVANT:

	const int cmdID = 173;
	const int numDigs = 3;
	int dig[ numDigs ];
	int num = cmdID;
	for(int i = numDigs - 1; i >= 0 ; i--)
	{
		dig[ i ] = NUMSTART + num % 10;
		num /= 10;
	}
	char output[8];
	output[0] = 64;		//@
	output[1] = NUMSTART + ID;	//device ID
	output[2] = 32;		// space	  
	int startindex = 3;
	for (int i = startindex; i < startindex + numDigs; i++){
		output[i] = dig[ i-startindex ];
	}
	output[startindex + numDigs] = 32; 		//SPC
	output[startindex + numDigs + 1] = 49;	//1
	output[startindex + numDigs + 2] = 48;	//0
	output[startindex + numDigs + 3] = 13;	//CR
	output[startindex + numDigs + 4] = 0;	//null	
	
	if (!writeport(fd, output)) {
		printf("Err: ADL write failed\n");
		return 1;
	}
	else{
		printf("ADL written:%s\n", output);
	}	
	
	//~~~~~~~~ reply ~~~~~~~~~
	char replyData[50];
	int numChars;
	int reply;

        //END STUFF THAT'S PROBABLY NOT RELEVANT
        //===============================================

	
	reply = getReply(fd, replyData, numChars);	
	reply = checkForACK( ID, replyData );
	if ( reply == 0) {
			printf("I'm here 1\n");
			return 0;
	}else{
		printf("Err: bad ADL response\n");
		return -1;
	}
	return -1;		//should never be here
}//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 




I'm here 1
*** glibc detected *** /A/stage/stage: munmap_chunk(): invalid pointer: 0x00007fffffffe0f0 ***
======= Backtrace: =========
/lib64/libc.so.6(cfree+0x166)[0x35eda72886]
/A/stage/stage[0x410577]
/A/stage/stage[0x414b15]
/A/stage/stage[0x4157d0]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x35eda1d994]
/A/stage/stage(__gxx_personality_v0+0x121)[0x402a09]
======= Memory map: ========
00400000-00421000 r-xp 00000000 00:19 1830423342                         /A/stage/stage
00621000-00622000 rw-p 00021000 00:19 1830423342                         /A/stage/stage
00622000-00643000 rw-p 00622000 00:00 0                                  [heap]
334e600000-334e67f000 r-xp 00000000 08:01 336483                         /usr/lib64/libfreetype.so.6.3.10
334e67f000-334e87f000 ---p 0007f000 08:01 336483                         /usr/lib64/libfreetype.so.6.3.10
334e87f000-334e884000 rw-p 0007f000 08:01 336483                         /usr/lib64/libfreetype.so.6.3.10
334ea00000-334ea29000 r-xp 00000000 08:01 334972                         /usr/lib64/libfontconfig.so.1.1.0
334ea29000-334ec29000 ---p 00029000 08:01 334972                         /usr/lib64/libfontconfig.so.1.1.0
334ec29000-334ec33000 rw-p 00029000 08:01 334972                         /usr/lib64/libfontconfig.so.1.1.0
334ec33000-334ec34000 rw-p 334ec33000 00:00 0 
334ee00000-334f67a000 r-xp 00000000 08:01 2823048                        /usr/lib64/qt-3.3/lib/libqt-mt.so.3.3.6
334f67a000-334f879000 ---p 0087a000 08:01 2823048                        /usr/lib64/qt-3.3/lib/libqt-mt.so.3.3.6
334f879000-334f8f6000 rw-p 00879000 08:01 2823048                        /usr/lib64/qt-3.3/lib/libqt-mt.so.3.3.6
334f8f6000-334f8fc000 rw-p 334f8f6000 00:00 0 
3350400000-3350413000 r-xp 00000000 08:01 340797                         /usr/lib64/libXft.so.2.1.2
3350413000-3350612000 ---p 00013000 08:01 340797                         /usr/lib64/libXft.so.2.1.2
3350612000-3350613000 rw-p 00012000 08:01 340797                         /usr/lib64/libXft.so.2.1.2
35ed600000-35ed61c000 r-xp 00000000 08:01 454403                         /lib64/ld-2.5.so
35ed81b000-35ed81c000 r--p 0001b000 08:01 454403                         /lib64/ld-2.5.so
35ed81c000-35ed81d000 rw-p 0001c000 08:01 454403                         /lib64/ld-2.5.so
35eda00000-35edb4e000 r-xp 00000000 08:01 454404                         /lib64/libc-2.5.so
35edb4e000-35edd4d000 ---p 0014e000 08:01 454404                         /lib64/libc-2.5.so
35edd4d000-35edd51000 r--p 0014d000 08:01 454404                         /lib64/libc-2.5.so
35edd51000-35edd52000 rw-p 00151000 08:01 454404                         /lib64/libc-2.5.so
35edd52000-35edd57000 rw-p 35edd52000 00:00 0 
35ede00000-35ede82000 r-xp 00000000 08:01 454406                         /lib64/libm-2.5.so
35ede82000-35ee081000 ---p 00082000 08:01 454406                         /lib64/libm-2.5.so
35ee081000-35ee082000 r--p 00081000 08:01 454406                         /lib64/libm-2.5.so
35ee082000-35ee083000 rw-p 00082000 08:01 454406                         /lib64/libm-2.5.so
35ee200000-35ee202000 r-xp 00000000 08:01 454407                         /lib64/libdl-2.5.so
35ee202000-35ee402000 ---p 00002000 08:01 454407                         /lib64/libdl-2.5.so
35ee402000-35ee403000 r--p 00002000 08:01 454407                         /lib64/libdl-2.5.so
35ee403000-35ee404000 rw-p 00003000 08:01 454407                         /lib64/libdl-2.5.so
35ee600000-35ee616000 r-xp 00000000 08:01 454462                         /lib64/libpthread-2.5.so
35ee616000-35ee815000 ---p 00016000 08:01 454462                         /lib64/libpthread-2.5.so
35ee815000-35ee816000 r--p 00015000 08:01 454462                         /lib64/libpthread-2.5.so
35ee816000-35ee817000 rw-p 00016000 08:01 454462                         /lib64/libpthread-2.5.so
35ee817000-35ee81b000 rw-p 35ee817000 00:00 0 
35eea00000-35eea14000 r-xp 00000000 08:01 338063                    
Program received signal SIGABRT, Aborted.
0x00000035eda30265 in raise () from /lib64/libc.so.6
Last edited on
Lines 26-28: You're overflowing output here. See the values of startindex, numDigs, and output's size.
Hello Helios,

What output is being overflown? I do not understand.
Ah. That would make sense, but the same thing happens when I change it to 80 instead of 8.

Everbeginner: I have a variable called "output". That's what he's referring to, versus a generic reference to an output.
Last edited on
Sorry, I can't think of anything else other to remove possible sources of bugs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int serialClass::changeACKdelay(int ID, float delay) {

	std::stringstream stream;
	stream <<"@"<<ID<<" 173 10\n";
	if (!writeport(fd, stream.str().c_str())) {
		printf("Err: ADL write failed\n");
		return 1;
	}
	else
		printf("ADL written:%s\n", stream.str().c_str());
	
	//~~~~~~~~ reply ~~~~~~~~~
	char replyData[50];
	int numChars;
	int reply;

	//Is getReply()? guaranteed not to write more than 50 characters?
	reply = getReply(fd, replyData, numChars);
	reply = checkForACK( ID, replyData );
	if ( reply == 0) {
			printf("I'm here 1\n");
			return 0;
	}else{
		printf("Err: bad ADL response\n");
		return -1;
	}
	return -1;		//should never be here
}
Note my comment before the call to getReply().
I lied...that wasn't it. It still crashes at the same place. Also, the code above doesn't compile...but I changed things to

1
2
3
std::stringstream stream;
stream <<"@"<<ID<<" 173 10\n";
strcpy(output, stream.str().c_str());


in the spirit of what you were after, with no success still. I get seg faults now instead of termination warnings from qt.
=========================
Awesome, that was it! Thank you. Basically I had forgotten that reply was changed from normal text to an xml string, so it was significantly longer. Why it worked before (i.e. without qt) was a mystery, but changing the length solved the issue.

Thanks again.
Last edited on
Then I'm totally stumped. My only suggestion is that you try running it through Valgrind.
I've never used Valgrind before. Any tutorials you usually recommend in particular? Otherwise I'll just look around online.
valgrind --help has everything you need to know.
Helios, guess what? I commented some parts out, compiled, then uncommented them back. After I compiled again it worked.

Go figure; I have no idea why it works now. It didn't work after a make clean, but it did after this commenting and uncommenting.
Topic archived. No new replies allowed.