?
Solved

Segmentation Fault in GCC; Sporadic Values for Int

Posted on 2003-03-24
23
Medium Priority
?
298 Views
Last Modified: 2010-04-15
When I run the following program I come across 2 errors.  First, after compiling successfully, it fails to complete a run because it encounters either a Segmentation Fault or a Bus Error.  Second, there is an integer value called label_count.  I initialize the variable to zero and increment it by one.  However, once it enters the loop, I start getting very large values for label_count and no idea why.  Any help on this is greatly appreciated.

<a href="http://www.designpurchasingnetwork.com/sic/sicxe.c">Source File</a>
<a href="http://www.designpurchasingnetwork.com/sic/SRCFILE.txt">Input Source File</a>
<a href="http://www.designpurchasingnetwork.com/sic/opcodes.txt">Input Codes File</a>

0
Comment
Question by:DragXSlay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
  • 3
  • +1
23 Comments
 

Expert Comment

by:Getch
ID: 8195482
1st thing compile your program with -ggdb -o arguments.
Then type gdb yourfile to debug it with gdb debugger.
If your program uses arguments then set args arg1...
type then r yourfile and gdb will find the stack with the
segmentanntion error then type where or backtrace to see the
segmentantion error where is.Download gdb debug tutorial
to fix it

cya getch
0
 

Expert Comment

by:Getch
ID: 8195496
Is this your code ?
0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8195510
Yes, it is.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:DragXSlay
ID: 8195554
ggdb isn't installed on the server I'm testing on.
0
 

Expert Comment

by:Getch
ID: 8195813
when i tested your code... doesn't give me an seg error or anything :)
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8196199
Problem #1.
PRE_OPCODE_LENGTH is #defined to be 1, meaning that pre_mnemonicOpcode[] can only store 1 character.  In pass_one(), you call strleftcpy() with pre_mnemonicOpcode as the destination pointer.  Since what you're copying is bigger than one byte, you blow past the end of the pre_mmnmonicOpcode[] array and overwrite label_count (and likely other variables also).  So you either need to increase the size of pre_mnemonicOpcode[], or limit the amount of data you copy into it.  (I haven't looked into your code enough to figure out what pre_mnemonicOpcode[] is supposed to be doing).  A fix for this might also fix the Segmentation Fault - let us know.

Suggestion:  you might want to add a 'max_len' parameter to all your strxxxcpy() functions and pass in the size of the destination buffer.  If within strxxxcpy() you find the destination buffer is not big enough, you can printf() an error message.
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8196427
Problem #1.
PRE_OPCODE_LENGTH is #defined to be 1, meaning that pre_mnemonicOpcode[] can only store 1 character.  In pass_one(), you call strleftcpy() with pre_mnemonicOpcode as the destination pointer.  Since what you're copying is bigger than one byte, you blow past the end of the pre_mmnmonicOpcode[] array and overwrite label_count (and likely other variables also).  So you either need to increase the size of pre_mnemonicOpcode[], or limit the amount of data you copy into it.  (I haven't looked into your code enough to figure out what pre_mnemonicOpcode[] is supposed to be doing).  A fix for this might also fix the Segmentation Fault - let us know.

Suggestion:  you might want to add a 'max_len' parameter to all your strxxxcpy() functions and pass in the size of the destination buffer.  If within strxxxcpy() you find the destination buffer is not big enough, you can printf() an error message.
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8196560
Problem #1.
PRE_OPCODE_LENGTH is #defined to be 1, meaning that pre_mnemonicOpcode[] can only store 1 character.  In pass_one(), you call strleftcpy() with pre_mnemonicOpcode as the destination pointer.  Since what you're copying is bigger than one byte, you blow past the end of the pre_mmnmonicOpcode[] array and overwrite label_count (and likely other variables also).  So you either need to increase the size of pre_mnemonicOpcode[], or limit the amount of data you copy into it.  (I haven't looked into your code enough to figure out what pre_mnemonicOpcode[] is supposed to be doing).  A fix for this might also fix the Segmentation Fault - let us know.

Suggestion:  you might want to add a 'max_len' parameter to all your strxxxcpy() functions and pass in the size of the destination buffer.  If within strxxxcpy() you find the destination buffer is not big enough, you can printf() an error message.
0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8197159
Thanks, that fixed the label_count problem.  I extended the length to 2 since it only reads in one character and a space.  It seems the segmentation fault is occuring on the following line: symtab_add(label,temp_loc,label_count);

Any idea why?  I displayed all the variables and they're all correct.
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8197281
Did you check the lengths of all your other char arrays?  I noticed PRE_OPERAND_LENGTH is also #defined to be 1.  Have you included room for the string termination character, i.e. 0, in all your string buffers?  If pre_mnemonicOpcode[] has a character, plus a space, plus the 0, then the length should be 3 not 2. Maybe you can post your updated code after checking the array sizes.

Sorry for the repeat posts - I only posted once.  I'm not sure why the same post shows up 3 times with 3 different times????
0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8197375
I changed both of the PRE variables to 2.  The space after the single character is replaced with the '\0' null terminator in the loop after strnleftcopy is called so that shouldn't be a problem.  The label_count is returning the proper values now.  I uploaded the modified code.  I believe the problem is on that line symtab_add(label,temp_loc,label_count); because the printf function before the function call displays but the one immediately after it does not...instead I receive a segmentation fault.  Let me know if you see anything else.  Thanks for brining that to my attention.
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8197619
"The space after the single character is replaced with the '\0' null terminator in the loop after strnleftcopy is called so that shouldn't be a problem."

The problem is INSIDE strnleftcopy().  If the while() loop is entered even once, then THREE bytes will be written into the buffer initially pointed to by dest, i.e.:
*source
' '
0

Since your buffer can only hold 2 bytes, then the third byte is written past the end of the buffer, and will clobber the innocent variable that happens to be there.

I no longer get a segmentation violation with your current source, but I do see some garbage in some of the printfs.  What's the last line of stuff you see printf()ed before the Segmentation violation?
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8197694
I bumped the size of HEX_OPCODE_LENGTH, PRE_OPCODE_LENGTH, and PRE_OPERAND_LENGTH to 3, and then changed %s to %d on lines 565, 576, 580, and got the following result:

MAIN: FIRST 0
MAIN: CLOOP1 6
MAIN: ENDFIL 13
MAIN: LENGTH 16
MAIN: RETADR 76
MAIN: VALUE 79
MAIN: CLOOP2 81
MAIN: CL 85
MAIN: CLS 8D
MAIN: XL 90
MAIN: XLS 92
ATOI: .NUM. .500. .13.
ATOI: 500 97
MAIN: BUFFER 97
* BUFEND 897
OP: 2048 897
ATOI: .CONS. .1024. .17.
ATOI: 1024 897
LABEL: 2199 897
LABEL: 2048 897
CLS     008D
BUFFER     0097
NUM     01F4
MAXL2     0800
BUFE2     0897
CLOOP1     0006
CLOOP2     0081
VALUE     0079
COPY     0000
BUFEND     0897
XLS     0092
MAXLEN     0800
FIRST     0000
XL     0090
CONS     0400
RETADR     0076
LENGTH     0016
RDREC     0897
ENDFIL     0013
CL     0085
Pass 1 of the SIC/XE Source has been completed!
0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8198785
I made the exact same changes as you and got the following output:

MAIN: FIRST 0
MAIN: CLOOP1 6
MAIN: ENDFIL 13
MAIN: LENGTH 16
MAIN: RETADR 76
MAIN: VALUE 79
MAIN: CLOOP2 81
MAIN: CL 85
MAIN: CLS 8D
MAIN: XL 90
MAIN: XLS 92
ATOI: 500 151
MAIN: BUFFER 97
* BUFEND 897
OP: 2048 897
ATOI: 1024 2199
LABEL: 2199 897
LABEL: 2048 897
CLS     008D
BUFFER  0097
NUM     01F4
MAXL2   0800
BUFE2   0897
CLOOP1  0006
CLOOP2  0081
VALUE   0079
COPY    0000
BUFEND  0897
XLS     0092
MAXLEN  0800
FIRST   0000
XL      0090
CONS    0400
RETADR  0076
LENGTH  0016
RDREC   0897
ENDFIL  0013
CL      0085
MULR    39380000
Bus Error

The segmentation fault has been replaced with a Bus Error.  Also, that last variable "MULR" shouldn't be in the hash table.  I'm so close to getting this up and running, any idea why it added MULR to the list?  Thanks for all your help!
0
 
LVL 2

Accepted Solution

by:
honey_hamster earned 500 total points
ID: 8199198
On around line 858 you have <= BKT_SIZE in your for() loop - I think it should be < BKT_SIZE.  I can't duplicate your results (I'm running GCC in a DOS Window), so I can't be sure this is your problem.  Let us know if the change doesn't fix things.
0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8199623
Yup, that's it.  Perfect.  I'm going to up the point value as a thanks.  I have one more question though.  I'm getting an extra error in my intermediate file and the problem seems to be separating the variables in the operand field.  If happens with the code on lines 513 to 527.  I updated the source so you can take a look if you don't mind.  Thanks again.

0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8199800
Yup, that's it.  Perfect.  I'm going to up the point value as a thanks.  I have one more question though.  I'm getting an extra error in my intermediate file and the problem seems to be separating the variables in the operand field.  If happens with the code on lines 513 to 527.  I updated the source so you can take a look if you don't mind.  Thanks again.

0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8199807
Could you be more specific as to which error in the intermediate file is the extra error?
0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8199817
Yup, that's it.  Perfect.  I'm going to up the point value as a thanks.  I have one more question though.  I'm getting an extra error in my intermediate file and the problem seems to be separating the variables in the operand field.  If happens with the code on lines 513 to 527.  I updated the source so you can take a look if you don't mind.  Thanks again.

0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8199838
0897 MAXLEN   EQU     BUFEND-BUFFER
********* ERROR:  Illegal Expression in Operand Field

This should not be an error because BUFFER is defined in the LABEL column before BUFEND.  The problem is where I separate the variables.  I think the solution is just finding a better way to breaking the string into substrings.
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8200453
I'm still not 100% sure exactly how your program is supposed to work, but in your symbol table structure, you have an 'order' field that's associated with each label.  On line 540, you have 'if( first_order < second_order )',  and this is failing and causing the extra error.  It seems like you're trying to check for a negative value as a result of a subtraction of 2 labels, and flagging it as an error, yet your srcfile.txt seems to think that negative value is okay.  I'm not sure what you want the program to do here, but hopefully I've pointed you at the issue.

PS what's an interior decorator (at least that's the owner of the website to which you posted your source) doing writing assembler language parsers?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8200790
Segmentation fault often occus when you try to reference a memory location which is now free or deallocated. For example, if you are returning a pointer from a function, and let's say that the pointer pointed to a local variable of the function, then after the function execution finishes, the memory allocated for all its local variables is freed and the address returned is now free, not allocated. So effectively, the pointer in the callling function which held the address returned by this called function, is now pointing to garbage.

I haven't gone through your entire code, so it is not possible to point out where it is occuring, but can you go through your code and see if the above information helps you to correct your problem.

Mayank.
0
 
LVL 2

Author Comment

by:DragXSlay
ID: 8206113
Nevermind, I figured out my problem.  Thanks for all your help on this.  I posted the source code on a client's website.  That's my daytime job at least...PHP/ColdFusion/Perl/HTML/Javascript etc.  This C/C++ stuff is fairly new to me.  Thanks again for your help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question