*** stack smashing detected ***

Hi everybody,

I have a program that reads the info into a file, check if this is in a mysql table and finally creates a new line or update it. Below it's a similar structure of my program (there are missing an structure and other variables...don't worry about them)

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
58
#include <iostream>
#include <string>
#include <mysql.h>
using namespace std;
int mysql_verify(int i);
int main()
{
    MYSQL *my;
    string id,info;
     int j;
    ifstream infile("mytext.txt");
    getline(infile,id);
    getline(infile,info);
    infile.close(infile);
    j = mysql_verify(id.c_str());

    .
    .
    .
    .
   /** and so on;**/
}
int mysql_verify(int id)
{
	MYSQL *my;
	my = mysql_init(NULL);
		if(my == NULL)
		{ 
			sprintf(error,"%s", mysql_error(my));
			cout<<"mysql: "<< error<<"\n";
	       exit (1);
	       
		}
		if(mysql_real_connect(my,myserver,myuser,mycpass,mydatabase,myport,NULL,0) == NULL )
		{
	   		sprintf(error,"%s",mysql_error(my));
			cout<<"mysql: "<<error<<"\n";
			exit (1);
		}
		char que[30];
		sprintf(que,"select * from smytable where id = \"%s%s\"\n",(mydcID).c_str(),id);
		if(mysql_query(my,que)){
					sprintf(error,"Mysql: %s\n",mysql_error(my));
					cout<<error;
					mysql_close(my);
				exit (1);
		}
		MYSQL_RES *res = mysql_store_result(my);
		i = mysql_num_rows(res);
		if( i > 1 )
		{
			cout<<"There are "<<i << " elements with ID: "<<myconn.dcID<<id<<"\n";
			mysql_close(my);
			exit (1);
		}
		mysql_close(my);
		return i;/***GDB says here is the problem**/
}/*****and also GDB says that the problem is here******/

My programs work from the command line or it's possible to give it the name of the file that will be read.

When I compile there is no any warning and of course error messages but when i run the program this give this output

*** stack smashing detected ***: ./dbw terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x50)[0x82f390]
/lib/tls/i686/cmov/libc.so.6(+0xe233a)[0x82f33a]
./dbw(_Z12mysql_verifyPKc+0x338)[0x804e52d]
./dbw(main+0x442)[0x804a346]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x763bd6]
./dbw[0x8049e71]
.
.
.
.
.

I have been using GDB for finding where the problem is: from the gdb output frame :

(gdb) frame 6
#6  0x0804e52d in mysql_verify (id=0x806356c "20110312135645") at dbw.cpp:409
409	}
(gdb) print }
A syntax error in expression, near `}'.


and also there an issue in the return i;

I have checked out many times mysql_verify function and I don't find anything to suspect.

Any Ideas about those problems.

Thank you


Billy
Last edited on
You likely have invalid memory accesses somewhere, probably writing beyond array bounds.
Run the debug build of your program with valgrind, it will likely be able to tell you more.
Hi Athar,

I have tried but there are too much information that I don't undertand completly.

Below it's an extract what i got when I run valgrind in this way

valgrind --tool=memcheck --leak-check=yes .-myprogram


==8378== LEAK SUMMARY:
==8378==    definitely lost: 0 bytes in 0 blocks
==8378==    indirectly lost: 0 bytes in 0 blocks
==8378==      possibly lost: 526 bytes in 19 blocks
==8378==    still reachable: 69,811 bytes in 21 blocks
==8378==         suppressed: 0 bytes in 0 blocks
==8378== Reachable blocks (those to which a pointer was found) are not shown.
==8378== To see them, rerun with: --leak-check=full --show-reachable=yes
==8378== 
==8378== For counts of detected and suppressed errors, rerun with: -v
==8378== ERROR SUMMARY: 19 errors from 19 contexts (suppressed: 27 from 10)
Aborted


any other idea please...

Thank you again,


Billy
That part of the valgrind output isn't particularly interesting, since you're not looking for memory leaks.
What you need to watch out for are messages like "Invalid write of size X at..." before the summary.

If that doesn't bring up anything, you could try defining the global macro _GLIBCXX_DEBUG.
However, that is only effective if you use the STL containers throughout your program, but you don't. Don't use sprintf or anything of the sort in a C++ program.

Ah right, talking about sprintf... here's your error:
1
2
char que[30];
sprintf(que,"select * from smytable where id = \"%s%s\"\n",(mydcID).c_str(),id);


This couldn't have happened if you had used std::string.
Last edited on
By i, do you mean id?
Hi Athar,

You're right...those sprintf functions have brought me only problem but I have a lot in my program and many are passed before to get this issue. Also I could check that the mysql query has been executed well.

I have changed to string type and it's the same problem and in the lines.........
I have solved the problem changing the old c functions and using strings.

The lesson learnt is don't use old c function into a c++ program 'cause they can give you a lot of segmentation fault and other stuffs.

Topic archived. No new replies allowed.