LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2005, 06:00 PM   #1
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Rep: Reputation: 30
Seg faults and slack pack parsing!


Here's my code for 'parsing' slack pack info from a file in /var/log/packages/


Code:
void treesel(GtkWidget *item){
gchar *name, *buf[60], *pkginfo[200];
FILE *pkg;
GtkLabel *label;
label = GTK_LABEL (GTK_BIN (item)->child);
gtk_label_get (label, &name);
char *path[strlen(name) + strlen("/var/log/packages/")];
memcpy(path, "/var/log/packages/",strlen(name));
strcat(path, name);
pkg = fopen(path,"r");
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
printf("\nPackage name: %s\n", buf);
memcpy(pkginfo, buf,strlen(buf));
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
printf("\nCompressed file size: %s\n", buf);
strcat(pkginfo, buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
printf("\nUncompressed package size: %s\n", buf);
strcat(pkginfo, buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
fscanf(pkg,"%s",buf);
printf("\nPackage location: %s\n", buf);
strcat(pkginfo, buf);
fflush(pkg);
fclose(pkg);
printf("\n-----------\n%s",&pkginfo);
}

It works fine reading a pack that starts like:
Code:
PACKAGE NAME:     gtkradiant-1.5.0-2005-01-16.i386
COMPRESSED PACKAGE SIZE:     2240 K
UNCOMPRESSED PACKAGE SIZE:     6320 K
PACKAGE LOCATION: gtkradiant-1.5.0-2005-01-16.i386.tgz
PACKAGE DESCRIPTION:
FILE LIST:
But this one here:
Code:
PACKAGE NAME:     acct-6.3.2-i386-1
COMPRESSED PACKAGE SIZE:     52 K
UNCOMPRESSED PACKAGE SIZE:     130 K
PACKAGE LOCATION: /var/log/mount/slackware/ap/acct-6.3.2-i386-1.tgz
PACKAGE DESCRIPTION:
acct: acct (process accounting utilities)
acct:
acct: This is a set of utilities which reports and summarizes data about
acct: user connect times and process execution statistics.  To activate
acct: process accounting, create the log file (touch /var/log/pacct), and
acct: then use the accton command to start it (accton /var/log/pacct).
acct: Be aware that the log file can grow to be quite large.
acct:
acct: The GNU process accounting utilities were written by Noel Cragg.
acct:
acct:
FILE LIST:
...generates a segfault.


Any ideas?
 
Old 02-23-2005, 06:59 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Code:
*buf[60]
Code:
echo "/var/log/packages/gtkradiant-1.5.0-2005-01-16.i386.tgz" | wc -c
55
echo "/var/log/packages/var/log/mount/slackware/ap/acct-6.3.2-i386-1.tgz" | wc -c
67
The problem is (I believe) that some of your packages already
have /var/log/packages in location, others don't. You prepend
it regardless.


Cheers,
Tink
 
Old 02-23-2005, 08:33 PM   #3
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks for the reply!

I don't believe I understand, I'm still new to C and I need a better explanation.
Can you help me to understand how to 'fix' this?
 
Old 02-23-2005, 08:43 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Try making your buffer e.g. 80 and see if it still
bombs out on you ...

What I was trying to say is that you're writing more
to buf than 60 characters ...

Code:
gchar *name, *buf[60], *pkginfo[200];
FILE *pkg;
GtkLabel *label;
label = GTK_LABEL (GTK_BIN (item)->child);
gtk_label_get (label, &name);
char *path[strlen(name) + strlen("/var/log/packages/")];
memcpy(path, "/var/log/packages/",strlen(name));
strcat(path, name);
pkg = fopen(path,"r");
For the gtkradiant package, for example, you have
PACKAGE LOCATION: gtkradiant-1.5.0-2005-01-16.i386.tgz
which is (prepended with /var/log/packages) 55 chars for path,
and fits into buf ...

For acc
PACKAGE LOCATION: /var/log/mount/slackware/ap/acct-6.3.2-i386-1.tgz
PLUS the /var/log/packages you add to path is 67 ... when you
write that to buf you write too much => segfault

Clear enough?


Cheers,
Tink
 
Old 02-23-2005, 09:20 PM   #5
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Original Poster
Rep: Reputation: 30
I increased the size of the buffer and it still segfaults.

Also, path has nothing to do with the contents of the buffer, it's just the var I use to build the appropriate path.
 
Old 02-23-2005, 10:45 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Heh - sorry, I didn't look at the code TOO closely.

But the thing is the same:
Code:
echo "acct: process accounting, create the log file (touch /var/log/pacct), and" | wc -c
74
The lines of description don't fit, just check if ANYTHING
in the file is longer than your buffer.

Also, can you find out (using gdb) at which line it bombs out
on you? That would certainly help finding the fluke :)


Cheers,
Tink
 
Old 02-23-2005, 11:20 PM   #7
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Original Poster
Rep: Reputation: 30
Figured it out, I screwed the path, not the buf.
it was:
Code:
memcpy(path, "/var/log/packages/",strlen(name));
Should have been:
Code:
memcpy(path, "/var/log/packages/",strlen(name) + strlen("/var/log/packages/"));
Danke for the help!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC seg faults benne Linux - Software 5 09-05-2005 01:09 AM
Repeated seg faults Tick Linux - General 5 07-29-2004 07:09 PM
seg faults happening allan_y Linux - General 2 07-25-2004 05:30 AM
bash seg faults Kilka *BSD 4 12-15-2003 01:40 AM
w3m seg faults slakmagik Linux - Software 0 05-04-2003 10:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration