LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 07-07-2008, 03:04 PM   #1
shurrey
LQ Newbie
 
Registered: Jul 2008
Location: Flint Hill, VA
Distribution: Red Hat EL Server release 5 (7)
Posts: 7

Rep: Reputation: 0
Segmentation Fault on fclose


Hi,

I've been writing C code for 7 years in a SCO Openserver environment. I'm currently migrating this code from SCO to Red Hat EL Server release 5. I've gotten a lot of SIGSEGV errors along the way, mostly because the C compiler in SCO lets you get away with lazy programming.

In any case, I've fixed most with better error checking and initialization. I've come across one though, that I've had difficulty cracking. The app basically opens a file, reads the first three characters to a variable for processing (this works), then reads the rest of the file, which may or may not be populated, processes this information and then closes the file. At file close, the application dies with a segmentation fault.

Here is the code in question:

Code:
void main(COUNT argc, TEXT *argv[])
{
COUNT i;
TRNKSTP tp;
TEXT *tbuf;
FILE *fp,*fopen();
TEXT fname[80];
TEXT errbuf[80];
COUNT offset=0;
COUNT itemp=0;
TEXT timeout[4];
int fcerr=0;

    if( argc > 1 )
    {
        if( strcmp( argv[1], "ver" ) == 0 )
        {
            printf("BUILD DATE: %s %s\n", BUILD_DATE, BUILD_TIME);
            fflush(stdout);
            printf("BUILD VERSION: %s\n", BUILD_VERS);
            fflush(stdout);
            return;
        }
    }

printf("initialize text variables\n");fflush(stdout); /* YOYOYO */
    memset(fname,'\0',80);
    memset(errbuf,'\0',80);
printf("init tbuf\n");fflush(stdout); /* YOYOYO */
    tbuf = (TEXT *) malloc(glbcfp->maxtrkcp);

printf("thrdinit\n");fflush(stdout); /* YOYOYO */
    thrdinit();

printf("for itemp loop\n");fflush(stdout); /* YOYOYO */
    for(itemp=1;itemp<=glbcfp->maxcllp;itemp++)
    {
printf("offset\n");fflush(stdout); /* YOYOYO */
       offset = (itemp - 1)*glbcfp->maxtrkcp;
printf("fname\n");fflush(stdout); /* YOYOYO */
       sprintf(fname,"%s/cp%1d.tpta",confdir,itemp);
printf("fopen\n");fflush(stdout); /* YOYOYO */
       if ((fp = fopen(fname,"r+")) == NULL)
       {
           sprintf(errbuf,"Could not open %s",fname);
           repterr(ESCWARN,errbuf);
           /*exit(1);*/
           thrdshut();          /* perform shutdown procedures */
       }
       else
       {
           /* read 3rd party timeout value */
printf("timeout\n");fflush(stdout); /* YOYOYO */
           timeout[3] = '\0';
           if ( read(fileno(fp), timeout, 3) != 3 )
           {
               sprintf(errbuf,"Error reading %s",fname);
               repterr(ESCWARN,errbuf);
               thrdshut();              /* perform shutdown procedures */
           }
printf("itemp\n");fflush(stdout); /* YOYOYO */
           if ( itemp == 1 )  /* use value in cp1.tpta for all CPs */
           {
printf("tptimeout\n");fflush(stdout); /* YOYOYO */
               tptimeout = atoi(timeout);
printf("MAXTPTIME\n");fflush(stdout); /* YOYOYO */
               if ( tptimeout > MAXTPTIME ) /* CP doesn't like big numbers */
                   tptimeout = MAXTPTIME;
           }
printf("assg3tm\n");fflush(stdout); /* YOYOYO */
           assg3tm(itemp);  /* send set timeout message to the CP */

printf("read\n");fflush(stdout); /* YOYOYO */
           if (read(fileno(fp),tbuf,glbcfp->maxtrkcp) != glbcfp->maxtrkcp)
           {
               sprintf(errbuf,"Error reading %s",fname);
               repterr(ESCWARN,errbuf);
              /* exit(2);*/
                thrdshut();             /* perform shutdown procedures */

           }
printf("tp\n");fflush(stdout); /* YOYOYO */
           tp = trnkctbl+offset;
printf("for maxtrkcp loop,max <%d>\n", glbcfp->maxtrkcp);fflush(stdout); /* YOYO
YO */
           for( i=0; i<glbcfp->maxtrkcp; i++, tp++)
           {
printf("TP_tbuf <%c>, i <%d>\n",tbuf[i], i);fflush(stdout); /* YOYOYO */
                if (tbuf[i] == 2)
                {
                        /* only assign as 3rd party if not already 
                         * assigned as a 3rd party trunk */

printf("T_THRDRTY\n");fflush(stdout); /* YOYOYO */
                        if ( tp->type != T_THRDPRTY )
                        {
                            thrdxtpa( 0, (i+1+offset) );  
                        }
                }
           }
printf("check fp\n");fflush(stdout); /* YOYOYO */
           if(fp)
           {
printf("fclose\n");fflush(stdout); /* YOYOYO */
               if( (fcerr = fclose(fp)) != 0 )
               {
                   printf("Error <%d> closing file <%s>\n", fcerr, fname);
                   fflush(stdout);
               }
           }
       }
printf("restart itemp for loop\n");fflush(stdout); /* YOYOYO */
    }
printf("thrdshut\n");fflush(stdout); /* YOYOYO */
    thrdshut();         /* perform shutdown procedures */
printf("exit\n");fflush(stdout); /* YOYOYO */
    exit(0);
}
Here is the output from the printfs:

Code:
(gdb) run
Starting program: /u/cmg/cmg5.0.0/exe/thrdprty 
initialize text variables
init tbuf
thrdinit
cmaninit
procid
fnamebuff
fopen
read
fclose
gbufsize
gbfattch
initgbuf
initmsg
cmaninit return
for itemp loop
offset
fname
fopen
timeout
itemp
tptimeout
MAXTPTIME
assg3tm
getbuf
3tmtext
VT
3tmstatus
3tmfcode
3tmsrcname
3tmthrdxmcp
fcode
status
srcname
cpalive
destname
cmg_putmsg
cmg_putmsg return
TYgetbuf
TYtext
TY
TYstatus
TYfcode
TYsrcname
TYthrdxmcp
fcode
status
srcname
cpalive
destname
cmg_putmsg
cmg_putmsg return
TYreturn
read
tp
for maxtrkcp loop,max <336>
TP_tbuf <>, i <0>
TP_tbuf <>, i <1>
TP_tbuf <>, i <2>
TP_tbuf <>, i <3>
TP_tbuf <>, i <4>
TP_tbuf <>, i <5>
TP_tbuf <>, i <6>
TP_tbuf <>, i <7>
TP_tbuf <>, i <8>
TP_tbuf <>, i <9>
TP_tbuf <>, i <10>
TP_tbuf <>, i <11>
TP_tbuf <>, i <12>
TP_tbuf <>, i <13>
TP_tbuf <>, i <14>
TP_tbuf <>, i <15>
TP_tbuf <>, i <16>
TP_tbuf <>, i <17>
TP_tbuf <>, i <18>
TP_tbuf <>, i <19>
TP_tbuf <>, i <20>
TP_tbuf <>, i <21>
TP_tbuf <>, i <22>
TP_tbuf <>, i <23>
TP_tbuf <>, i <24>
TP_tbuf <>, i <25>
TP_tbuf <>, i <26>
TP_tbuf <>, i <27>
TP_tbuf <>, i <28>
TP_tbuf <>, i <29>
TP_tbuf <>, i <30>
TP_tbuf <>, i <31>
TP_tbuf <>, i <32>
TP_tbuf <>, i <33>
TP_tbuf <>, i <34>
TP_tbuf <>, i <35>
TP_tbuf <>, i <36>
TP_tbuf <>, i <37>
TP_tbuf <>, i <38>
TP_tbuf <>, i <39>
TP_tbuf <>, i <40>
TP_tbuf <>, i <41>
TP_tbuf <>, i <42>
TP_tbuf <>, i <43>
TP_tbuf <>, i <44>
TP_tbuf <>, i <45>
TP_tbuf <>, i <46>
TP_tbuf <>, i <47>
TP_tbuf <>, i <48>
TP_tbuf <>, i <49>
TP_tbuf <>, i <50>
TP_tbuf <>, i <51>
TP_tbuf <>, i <52>
TP_tbuf <>, i <53>
TP_tbuf <>, i <54>
TP_tbuf <>, i <55>
TP_tbuf <>, i <56>
TP_tbuf <>, i <57>
TP_tbuf <>, i <58>
TP_tbuf <>, i <59>
TP_tbuf <>, i <60>
TP_tbuf <>, i <61>
TP_tbuf <>, i <62>
TP_tbuf <>, i <63>
TP_tbuf <>, i <64>
TP_tbuf <>, i <65>
TP_tbuf <>, i <66>
TP_tbuf <>, i <67>
TP_tbuf <>, i <68>
TP_tbuf <>, i <69>
TP_tbuf <>, i <70>
TP_tbuf <>, i <71>
TP_tbuf <>, i <72>
TP_tbuf <>, i <73>
TP_tbuf <>, i <74>
TP_tbuf <>, i <75>
TP_tbuf <>, i <76>
TP_tbuf <>, i <77>
TP_tbuf <>, i <78>
TP_tbuf <>, i <79>
TP_tbuf <>, i <80>
TP_tbuf <>, i <81>
TP_tbuf <>, i <82>
TP_tbuf <>, i <83>
TP_tbuf <>, i <84>
TP_tbuf <>, i <85>
TP_tbuf <>, i <86>
TP_tbuf <>, i <87>
TP_tbuf <>, i <88>
TP_tbuf <>, i <89>
TP_tbuf <>, i <90>
TP_tbuf <>, i <91>
TP_tbuf <>, i <92>
TP_tbuf <>, i <93>
TP_tbuf <>, i <94>
TP_tbuf <>, i <95>
TP_tbuf <>, i <96>
TP_tbuf <>, i <97>
TP_tbuf <>, i <98>
TP_tbuf <>, i <99>
TP_tbuf <>, i <100>
TP_tbuf <>, i <101>
TP_tbuf <>, i <102>
TP_tbuf <>, i <103>
TP_tbuf <>, i <104>
TP_tbuf <>, i <105>
TP_tbuf <>, i <106>
TP_tbuf <>, i <107>
TP_tbuf <>, i <108>
TP_tbuf <>, i <109>
TP_tbuf <>, i <110>
TP_tbuf <>, i <111>
TP_tbuf <>, i <112>
TP_tbuf <>, i <113>
TP_tbuf <>, i <114>
TP_tbuf <>, i <115>
TP_tbuf <>, i <116>
TP_tbuf <>, i <117>
TP_tbuf <>, i <118>
TP_tbuf <>, i <119>
TP_tbuf <>, i <120>
TP_tbuf <>, i <121>
TP_tbuf <>, i <122>
TP_tbuf <>, i <123>
TP_tbuf <>, i <124>
TP_tbuf <>, i <125>
TP_tbuf <>, i <126>
TP_tbuf <>, i <127>
TP_tbuf <>, i <128>
TP_tbuf <>, i <129>
TP_tbuf <>, i <130>
TP_tbuf <>, i <131>
TP_tbuf <>, i <132>
TP_tbuf <>, i <133>
TP_tbuf <>, i <134>
TP_tbuf <>, i <135>
TP_tbuf <>, i <136>
TP_tbuf <>, i <137>
TP_tbuf <>, i <138>
TP_tbuf <>, i <139>
TP_tbuf <>, i <140>
TP_tbuf <>, i <141>
TP_tbuf <>, i <142>
TP_tbuf <>, i <143>
TP_tbuf <>, i <144>
TP_tbuf <>, i <145>
TP_tbuf <>, i <146>
TP_tbuf <>, i <147>
TP_tbuf <>, i <148>
TP_tbuf <>, i <149>
TP_tbuf <>, i <150>
TP_tbuf <>, i <151>
TP_tbuf <>, i <152>
TP_tbuf <>, i <153>
TP_tbuf <>, i <154>
TP_tbuf <>, i <155>
TP_tbuf <>, i <156>
TP_tbuf <>, i <157>
TP_tbuf <>, i <158>
TP_tbuf <>, i <159>
TP_tbuf <>, i <160>
TP_tbuf <>, i <161>
TP_tbuf <>, i <162>
TP_tbuf <>, i <163>
TP_tbuf <>, i <164>
TP_tbuf <>, i <165>
TP_tbuf <>, i <166>
TP_tbuf <>, i <167>
TP_tbuf <>, i <168>
TP_tbuf <>, i <169>
TP_tbuf <>, i <170>
TP_tbuf <>, i <171>
TP_tbuf <>, i <172>
TP_tbuf <>, i <173>
TP_tbuf <>, i <174>
TP_tbuf <>, i <175>
TP_tbuf <>, i <176>
TP_tbuf <>, i <177>
TP_tbuf <>, i <178>
TP_tbuf <>, i <179>
TP_tbuf <>, i <180>
TP_tbuf <>, i <181>
TP_tbuf <>, i <182>
TP_tbuf <>, i <183>
TP_tbuf <>, i <184>
TP_tbuf <>, i <185>
TP_tbuf <>, i <186>
TP_tbuf <>, i <187>
TP_tbuf <>, i <188>
TP_tbuf <>, i <189>
TP_tbuf <>, i <190>
TP_tbuf <>, i <191>
TP_tbuf <>, i <192>
TP_tbuf <>, i <193>
TP_tbuf <>, i <194>
TP_tbuf <>, i <195>
TP_tbuf <>, i <196>
TP_tbuf <>, i <197>
TP_tbuf <>, i <198>
TP_tbuf <>, i <199>
TP_tbuf <>, i <200>
TP_tbuf <>, i <201>
TP_tbuf <>, i <202>
TP_tbuf <>, i <203>
TP_tbuf <>, i <204>
TP_tbuf <>, i <205>
TP_tbuf <>, i <206>
TP_tbuf <>, i <207>
TP_tbuf <>, i <208>
TP_tbuf <>, i <209>
TP_tbuf <>, i <210>
TP_tbuf <>, i <211>
TP_tbuf <>, i <212>
TP_tbuf <>, i <213>
TP_tbuf <>, i <214>
TP_tbuf <>, i <215>
TP_tbuf <>, i <216>
TP_tbuf <>, i <217>
TP_tbuf <>, i <218>
TP_tbuf <>, i <219>
TP_tbuf <>, i <220>
TP_tbuf <>, i <221>
TP_tbuf <>, i <222>
TP_tbuf <>, i <223>
TP_tbuf <>, i <224>
TP_tbuf <>, i <225>
TP_tbuf <>, i <226>
TP_tbuf <>, i <227>
TP_tbuf <>, i <228>
TP_tbuf <>, i <229>
TP_tbuf <>, i <230>
TP_tbuf <>, i <231>
TP_tbuf <>, i <232>
TP_tbuf <>, i <233>
TP_tbuf <>, i <234>
TP_tbuf <>, i <235>
TP_tbuf <>, i <236>
TP_tbuf <>, i <237>
TP_tbuf <>, i <238>
TP_tbuf <>, i <239>
TP_tbuf <>, i <240>
TP_tbuf <>, i <241>
TP_tbuf <>, i <242>
TP_tbuf <>, i <243>
TP_tbuf <>, i <244>
TP_tbuf <>, i <245>
TP_tbuf <>, i <246>
TP_tbuf <>, i <247>
TP_tbuf <>, i <248>
TP_tbuf <>, i <249>
TP_tbuf <>, i <250>
TP_tbuf <>, i <251>
TP_tbuf <>, i <252>
TP_tbuf <>, i <253>
TP_tbuf <>, i <254>
TP_tbuf <>, i <255>
TP_tbuf <>, i <256>
TP_tbuf <>, i <257>
TP_tbuf <>, i <258>
TP_tbuf <>, i <259>
TP_tbuf <>, i <260>
TP_tbuf <>, i <261>
TP_tbuf <>, i <262>
TP_tbuf <>, i <263>
TP_tbuf <>, i <264>
TP_tbuf <>, i <265>
TP_tbuf <>, i <266>
TP_tbuf <>, i <267>
TP_tbuf <>, i <268>
TP_tbuf <>, i <269>
TP_tbuf <>, i <270>
TP_tbuf <>, i <271>
TP_tbuf <>, i <272>
TP_tbuf <>, i <273>
TP_tbuf <>, i <274>
TP_tbuf <>, i <275>
TP_tbuf <>, i <276>
TP_tbuf <>, i <277>
TP_tbuf <>, i <278>
TP_tbuf <>, i <279>
TP_tbuf <>, i <280>
TP_tbuf <>, i <281>
TP_tbuf <>, i <282>
TP_tbuf <>, i <283>
TP_tbuf <>, i <284>
TP_tbuf <>, i <285>
TP_tbuf <>, i <286>
TP_tbuf <>, i <287>
TP_tbuf <>, i <288>
TP_tbuf <>, i <289>
TP_tbuf <>, i <290>
TP_tbuf <>, i <291>
TP_tbuf <>, i <292>
TP_tbuf <>, i <293>
TP_tbuf <>, i <294>
TP_tbuf <>, i <295>
TP_tbuf <>, i <296>
TP_tbuf <>, i <297>
TP_tbuf <>, i <298>
TP_tbuf <>, i <299>
TP_tbuf <>, i <300>
TP_tbuf <>, i <301>
TP_tbuf <>, i <302>
TP_tbuf <>, i <303>
TP_tbuf <>, i <304>
TP_tbuf <>, i <305>
TP_tbuf <>, i <306>
TP_tbuf <>, i <307>
TP_tbuf <>, i <308>
TP_tbuf <>, i <309>
TP_tbuf <>, i <310>
TP_tbuf <>, i <311>
TP_tbuf <>, i <312>
TP_tbuf <>, i <313>
TP_tbuf <>, i <314>
TP_tbuf <>, i <315>
TP_tbuf <>, i <316>
TP_tbuf <>, i <317>
TP_tbuf <>, i <318>
TP_tbuf <>, i <319>
TP_tbuf <>, i <320>
TP_tbuf <>, i <321>
TP_tbuf <>, i <322>
TP_tbuf <>, i <323>
TP_tbuf <>, i <324>
TP_tbuf <>, i <325>
TP_tbuf <>, i <326>
TP_tbuf <>, i <327>
TP_tbuf <>, i <328>
TP_tbuf <>, i <329>
TP_tbuf <>, i <330>
TP_tbuf <>, i <331>
TP_tbuf <>, i <332>
TP_tbuf <>, i <333>
TP_tbuf <>, i <334>
TP_tbuf <>, i <335>
check fp
fclose

Program received signal SIGSEGV, Segmentation fault.
0x006a81e2 in fclose@@GLIBC_2.1 () from /lib/libc.so.6
And finally, here is the 'bt full' results:

Code:
(gdb) bt full
#0  0x006a81e2 in fclose@@GLIBC_2.1 () from /lib/libc.so.6
No symbol table info available.
#1  0x08049410 in main (argc=Cannot access memory at address 0x0
) at thrdprty.c:190
        i = <value optimized out>
        tp = (TRNKSTP) 0xb7c53298
        tbuf = (TEXT *) 0x84c5008 ""
        fp = (FILE *) 0x84c5018
        fname = "/u/cmg/cmg5.0.0/config/cp1.tpta", '\0' <repeats 48 times>
        errbuf = '\0' <repeats 79 times>
        itemp = 1
        timeout = " 60"
        fcerr = <value optimized out>
Any help would be greatly appreciated, as I'm a RedHat noob. Please let me know if further information is required. Thanks in advance.
 
Old 07-07-2008, 05:00 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

A couple of suggestions:

1. Anything that trashes memory; any buffer overrun in the program between "fopen()" and "fclose()", could conceivably cause "fclose()" to fail. So (as you're probably already aware), "fclose()" (or, for that matter, *any* of your I/O calls) could be innocent victims of the actual root cause.

2. You might try something like "valgrind" or "Electric Fence":
http://valgrind.org/info/
http://www.cprogramming.com/debugging/valgrind.html
etc etc

3. You should definitely substitute "fprintf (stderr)" for "printf()/fflush (stdout)". Less clutter, better results. You'll like it - promise!

4. I don't see where you're sanity-checking "glbcfp->maxtrkcp", "glbcfp->maxcllp" and friends.

5. Similarly, I don't see where you're checking if the loop goes out of bounds. Is there any chance "tp" might fall outside of the buffer you've allocated for "tbuff"? Or any of your reads might be larger than tbuff itself?

6. You check for "fp = fopen() == NULL" (good...), but then later on you qualify "fclose()" with "if (fp) ...". Haven't you already verified that fp *ISN'T* null?

For that matter, if you're worried about fp (and it's a good thing to worry about!), why not always set "fp = NULL" immediately after every "fclose()"?

7. Also, explicitly set every pointer "*p = NULL" after every free.

It's cheap insurance, and it's a good practice.

8. A quick'n'dirty workaround might be to just declare "tbuf[]" as a big, huge array (instead of worrying about malloc ... and worrying if you've malloc'ed enough). You still need to make sure you don't read out of bounds, but it couldn't hurt...


IMHO .. PSM

Last edited by paulsm4; 07-07-2008 at 05:05 PM.
 
Old 07-07-2008, 05:10 PM   #3
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
I was about to suggest valgrind; but, I was beaten to the punch.

My only other point is that you open the file as a stream (fopen) and read from it using read() as opposed to fread(): fread() is the preferred method to read a stream)

I've always though it best to stick with one or the other.

I found this:

Family open(), close(), read() and Co:
- are not C standard.
- are Unix, BSD, POSIX and Single Unix Specification standard compliant.
- are wrappers to syscalls.
- are not formatted I/O : we have a unformatted block of one or more bytes.
- don't use a C standard I/O buffer.
- use Linux VFS buffers and cache.
- generally used for accessing data at a low level (device or raw
filesystem format).

Family fopen(), fclos(), fread() and Co:
- are C standard.
- are functions of the standard I/O libc (glibc).
- use an internal buffer (in their coding).
- enable formatted I/O on some calls.
- use a C standard I/O buffer.
- use Linux VFS buffers and cache.
- are generally used for accessing data streams in a device independant
fashion.

Last edited by harry edwards; 07-07-2008 at 05:11 PM.
 
Old 07-07-2008, 08:29 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
I also found this line curious

FILE *fp,*fopen();

fopen() is defined in stdio.h, you shouldn't declare it yourself.
 
Old 07-07-2008, 09:40 PM   #5
shurrey
LQ Newbie
 
Registered: Jul 2008
Location: Flint Hill, VA
Distribution: Red Hat EL Server release 5 (7)
Posts: 7

Original Poster
Rep: Reputation: 0
Everything here is very helpful. I am currently looking to install valgrind and do some good testing. I am also investigating the fread command. I will also try removing the fopen declaration. I will update as soon as I have some answers. Again, thanks for the assistance. This has been driving me nuts. Reading through the debug statements, it seems as though everything is going through ok, but looks can be deceiving.
 
Old 07-09-2008, 09:11 AM   #6
shurrey
LQ Newbie
 
Registered: Jul 2008
Location: Flint Hill, VA
Distribution: Red Hat EL Server release 5 (7)
Posts: 7

Original Poster
Rep: Reputation: 0
Update: sort of. I'm awaiting a standalone server, because Valgrind locks up my virtual server and it's loaded down so there are no more resources available to re-allocate.
 
Old 07-09-2008, 09:36 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You've got some crazy defines and/or typedef's going on there.
 
Old 07-09-2008, 11:53 AM   #8
shurrey
LQ Newbie
 
Registered: Jul 2008
Location: Flint Hill, VA
Distribution: Red Hat EL Server release 5 (7)
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
You've got some crazy defines and/or typedef's going on there.
Thanks! Most were inherited. This code was originally written for Xenix in the early 90s.
 
Old 07-09-2008, 03:43 PM   #9
shurrey
LQ Newbie
 
Registered: Jul 2008
Location: Flint Hill, VA
Distribution: Red Hat EL Server release 5 (7)
Posts: 7

Original Poster
Rep: Reputation: 0
Update: got a stadalone for a few days, installed valgrind and ran a valgrind --tool="memcheck" -v and right after the read() got the following error:

Code:
read
==13762== Syscall param read(buf) points to unaddressable byte(s)
==13762==    at 0x40EEED3: __read_nocancel (in /lib/libc-2.5.so)
==13762==    by 0x4045DEB: (below main) (in /lib/libc-2.5.so)
==13762==  Address 0x4171029 is 0 bytes after a block of size 1 alloc'd
==13762==    at 0x40203C0: malloc (vg_replace_malloc.c:149)
==13762==    by 0x804902F: main (thrdprty.c:116)
So it sounds like the malloc didn't work properly or at least read doesn't like it very much. Ironically, the segmentation error didn't occur.
 
Old 07-09-2008, 07:50 PM   #10
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

First, please consider changing your "read()" and your "printf ()/fflush()" calls to something like this:
Code:
...
  fprintf(stderr, "read\n"); /* YOYOYO */
  iret = fread (tbuff, 1, glbcfp->maxtrkcp);
  if (iret < glbcfp->maxtrkcp)
  {
    ...
Second, please verify that tbuff is successfully allocated, and is always at least as large as glbcfp->maxtrkcp. Adding more "fprintf()" statements is arguably the fastest/easiest way to do this.

Third, keep an eye on code like:
Code:
  tp = trnkctbl+offset;
I didn't notice you explicitly writing to "tp" (I didn't look very closely), but it definitely looks suspicious. Again: adding more "fprintf()'s" might help.
 
Old 07-10-2008, 02:48 PM   #11
shurrey
LQ Newbie
 
Registered: Jul 2008
Location: Flint Hill, VA
Distribution: Red Hat EL Server release 5 (7)
Posts: 7

Original Poster
Rep: Reputation: 0
I've already started transitioning to fprintf. These are just debug statements that will be removed post haste. I have already changed read() to use fread().

Tp is only pointed to in order to see if the corresponding trunk is already assigned. The assignment to *tp is done in a different section of the code once the assignement is completed successfully and is completely independent from this executable.

The check for length of read is done in the if statement in which the read takes place, although the value is not saved to a variable:

Code:
if (fread(tbuf,1, glbcfp->maxtrkcp,fileno(fp)) != glbcfp->maxtrkcp)
{
    sprintf(errbuf,"Error reading %s",fname);
    repterr(ESCWARN,errbuf);
    thrdshut();              /* perform shutdown procedures */
}

Last edited by shurrey; 07-10-2008 at 02:50 PM.
 
Old 07-30-2008, 07:55 AM   #12
shurrey
LQ Newbie
 
Registered: Jul 2008
Location: Flint Hill, VA
Distribution: Red Hat EL Server release 5 (7)
Posts: 7

Original Poster
Rep: Reputation: 0
I apologize. I forgot to update this thread. Apparently, for whatever reason, read/fread did not like reading into a pointer. Doing so caused FILE *fp to be overwritten and fileno(fp) was returning -1, rather than 6. I changed tbuf from "char *tbuf;" to "char tbuf[glfcfp->maxtrkcp];" and the problem went away.

Valgrind was a great help, and has identified a great number of memory leaks from the last 20 years that I have been able to fix without finding them in a production site, so thanks. Also, thanks for all the pointers (no pun intended). Much appreciated.

Last edited by shurrey; 07-30-2008 at 07:57 AM.
 
Old 08-03-2008, 07:13 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Just a general comment: add setlinebuf(stdout); after your declarations and get rid of all of the fflush(stdout);.

Also, your function declaration FILE *fopen(); isn't any good, so your use of FILE *fopen(const char*, const char*); probably defaults to int fopen(int, int);. Not really a big deal in this particular case, but try the appropriate declaration instead.

Should have read your last post before looking at your code for 30 minutes! Glad you fixed it.
ta0kira
 
  


Reply

Tags
fault, segmentation fault, sigsegv


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
Segmentation fault fridgider Linux - Software 1 04-09-2005 11:36 AM
segmentation fault pippet Programming 4 01-24-2005 01:02 AM
What is segmentation fault? koyi Programming 3 08-30-2003 12:03 AM
seg fault on fclose (); lackluster Programming 4 06-28-2003 07:30 PM


All times are GMT -5. The time now is 06:21 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration