PDA

View Full Version : segmentation fault in QT application



mahiapkum
12th April 2007, 12:27
Hi ,
I have an application developed in qt and when i run it i get segmentation fault the piece of code that generates the error is as follows:

void OffLineKDFWindow::ReadKAMALOffLineFile()
{
char str[256] , *ptr;
char *cptr;
QString s = QFileDialog::getOpenFileName(
"/home",
"Images (*.gsf)",
this,
"open file dialog",
"Choose a .gsf file to open" );
if(s != NULL)
{
strcpy(str,s);
int row = 0;
int col = 0;
GULAB_FILE_t *gfp = NULL;
QString KDFline;
int retval_size=1;
GULAB_PACKET_t gsf_row;
int i, PCount,err;

unsigned char formatted_mac_address[18];

table->setColumnWidth (0, 100 );
table->setColumnWidth (1, 100 );
table->setColumnWidth (2, 140 );
table->setColumnWidth (3, 160 );
table->setColumnWidth (4, 150 );
table->setColumnWidth (5, 150 );
table->setColumnWidth (6, 120 );
table->setColumnWidth (7, 200 );
table->setColumnWidth (8, 200 );
table->setColumnWidth (9, 120 );
table->setColumnWidth (10, 150 );
table->setColumnWidth (11,6000 );

int noRows , noColumns;
noRows = table->numRows();
noColumns = table->numCols();

char arr[] = "GULAB off line File reader:";
strcat(arr, s);
setCaption(tr(arr));
if((gfp = gsf_open((unsigned char*)str, READ, NULL, INIT_YES))== NULL)
{
printf("3 Compiler is here..%d\n",gfp->header.total_session_count);
return;
}
for(i=0;i<gfp->header.total_session_count;i++)
{
char tempData[12000] = { 0 };
if((err=gsf_read_next_packet(gfp,(GULAB_PACKET_t *)tempData)) == 0)
{
{
struct in_addr temp;
memcpy((char *)&gsf_row,tempData,sizeof(GULAB_PACKET_t));
temp.s_addr= gsf_row.gph.session_details.source_ip_address;
printf("Setting Text\n");
table->setText( row, 1, (char *)inet_ntoa(temp));
printf("Set text\n");
}
}
printf("Out of if\n");
}
}
printf("Out of 2nd if\n");}
}

There is no problem with gsf_read_next_packet() function the printfs "Setting Text" and "Set text" is getting printed it comes out of for loop but the statement "Out of 2nd if" does not get printed it is where i get segmentation fault.I have included all the required header files.The point of error is after "Out of if\n" statement.can any one help me:confused:

marcel
12th April 2007, 14:15
Most likely this happens because:



if(s != NULL)
{
strcpy(str,s);
int row = 0;
int col = 0;
...
You should modify it to:


if(!s.isNull())
{
strcpy(str,s.toAscii().toConstData());
int row = 0;
int col = 0;
...
QString does not have an ( const char* operator ).
It crashed when it tried to delete str ( at scope exit ).

Regards

wysota
12th April 2007, 14:22
You can't strcpy a QString onto a char*.

marcel
12th April 2007, 14:24
You can't strcpy a QString onto a char*.
Not a QString, but a QString::toAscii().constData will work. It's tested.

From Assistant ( about QByteArray::constData() ):


This function is mostly useful to pass a byte array to a function that accepts a const char *.

wysota
12th April 2007, 14:39
I meant the thread starter, not you :) Sorry for confusion.

Anyway mixing C and C++ this way is not a good idea...

Kumosan
12th April 2007, 14:49
Anyway mixing C and C++ this way is not a good idea...

And even in pure C using strcopy is a mortal sin.

marcel
12th April 2007, 14:50
No problem...
To mahiapkum (http://www.qtcentre.org/forum/u-mahiapkum-3299.html):

Another error:


strcat( arr, s );
You should also use s.toAscii.constData().

wysota
12th April 2007, 16:40
And even in pure C using strcopy is a mortal sin.

That's not true. It depends where the data comes from. If it doesn't come from user input (or some other external source), it's safe to use strcpy. And there is always strncpy...

Kumosan
12th April 2007, 21:17
That's not true. It depends where the data comes from.

Yeah, and later on the design changes, e.g. a data source is added or part of this code is reused somewhere else. Even if this cannot happen, I'd be very careful. Experienced programmers may be able to use strcopy safely under normal circumstances. But can they also use it safely when a bunch of crackers with criminal intend try to break your code on purpose? IMHO strcopy should not be used. Never.



And there is always strncpy...


Right. And this is the reason why strcopy not only should not be used, but why it is also not necessary to use it.

wysota
12th April 2007, 23:11
Yeah, and later on the design changes, e.g. a data source is added or part of this code is reused somewhere else. Even if this cannot happen, I'd be very careful. Experienced programmers may be able to use strcopy safely under normal circumstances. But can they also use it safely when a bunch of crackers with criminal intend try to break your code on purpose? IMHO strcopy should not be used. Never.
This is the same argument against using "goto". I say that if you know how and when to use a statement, you may safely use it regardless of what others say :)



Right. And this is the reason why strcopy not only should not be used, but why it is also not necessary to use it.
Sure, but it's there to use it so I don't see a reason not to use it. C++ is also not a must - you can code everything in assembly, but rarely people decide to do this now.

Kumosan
13th April 2007, 08:09
This is the same argument against using "goto". I say that if you know how and when to use a statement, you may safely use it regardless of what others say :)

I don't like this example. A single "goto" does not have the same potential security risks than a single "strcopy". A badly used "goto" might corrupt your design, making you program harder to read and to maintain. A badly used "strcopy" can compromise your whole system.

And just because it exists. it does not mean it should be used. Computer science evolved with time and "strcopy" is very very old.

wysota
13th April 2007, 09:36
I don't like this example. A single "goto" does not have the same potential security risks than a single "strcopy".
Sure it has, it can lead straight to stack corruption if you "goto" into another function.

mahiapkum
13th April 2007, 10:32
but when i do so i get a compilation error of 'toAscii() is not a member of QString'.

mahiapkum
13th April 2007, 10:37
my qt version is 3.3.3

guilugi
13th April 2007, 10:43
Yes, toAscii() is a Qt4 method ;-)

With Qt3, simply use this : str.ascii()

http://doc.trolltech.com/3.3/qstring.html#ascii

mahiapkum
13th April 2007, 11:12
Thanx i used ascii() and it worked fine.Thanks once again.

mahiapkum
13th April 2007, 11:13
Hi Marcel thanks 4 ur guidance now i am not getting segmentation fault.