Thread: Segmentation Fault of death

  1. #16
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    OK, so I made the following changes according to my professor's suggestions:
    Code:
    struct toklist
    {
       char *lbl;
       char *opc;
       char *opr1;
       char *opr2;
       int *ni;
       int *xbpe;
    } components;
    
    char label[81], opcode[81], opr1[81], opr2[81];
    int ni, xbpe;
    components.lbl = label;
    components.opc = opcode;
    components.opr1 = opr1;
    components.opr2 = opr2;
    components.ni = ∋
    components.xbpe = &xbpe;
    But get a new segmentation fault:
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x00000034c3470d3e in strcpy () from /lib64/tls/libc.so.6
    (gdb) backtrace
    #0  0x00000034c3470d3e in strcpy () from /lib64/tls/libc.so.6
    #1  0x00000000004029e3 in breakup (
        sourceline=0xffffc82d5e70ffff <Address 0xffffc82d5e70ffff out of bounds>, 
        components=
          {lbl = 0xffffcad83960ffff <Address 0xffffcad83960ffff out of bounds>, opc = 0xffffcb88f070ffff <Address 0xffffcb88f070ffff out of bounds>, opr1 = 0xffffd223f470ffff <Address 0xffffd223f470ffff out of bounds>, opr2 = 0xffffd260fbe0ffff <Address 0xffffd260fbe0ffff out of bounds>, ni = 0xffffd375e4f0ffff, xbpe = 0xffffd440dde0ffff}) at breakup.c:126
    From:
    Code:
    strcpy(components.lbl, label); 
    strcpy(components.opc, opcode); 
    strcpy(components.opr1, operand1); 
    strcpy(components.opr2, operand2); 
    *components.ni = ni;

  2. #17
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    OK, I get what you're saying now with the pointer. And I think that's what my professor was saying as well, you definitely portrayed it much better though haha, and I think with the changes I made that has been fixed but now I have an issue (probably with the pointers) in a different part of my function that is copying the local variables of the function into the variables of the calling function as I posted before this.

  3. #18
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, when you do:

    components.lbl = label

    That is making lbl point to label[0]. So when you strcpy you are basically copying label onto itself, if that makes sense. When both the source and destination of strcpy overlap in memory the function fails.

  4. #19
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What you probably want is to ALLOCATE memory for a string that components.lbl points to, and copy label in there.

    As such:

    Code:
    components.lbl = malloc(100 * sizeof(char)); /* allocate 100 chars */
    strcpy(components.lbl,label);

  5. #20
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    so label and components.lbl are the same location in memory? And if that is so then would I not need to copy the values?

  6. #21
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well if they are at the same location in memory the function strcpy fails. Because you are asking it to take the <mail> from one <mailbox> and put it in the same <mailbox>. Strcpy doesn't like that so it fails.

    "char * strcpy ( char * destination, const char * source );
    Copy string

    Copies the C string pointed by source into the array pointed by destination, including the terminating null character.

    To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source."

    This is the official documentation. Read the last part of the last sentence.

  7. #22
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    I don't see how they could overlap in memory since components.lbl is created in the calling function and label is a local variable of the function that is called.

  8. #23
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    They overlap because of:

    components.lbl = label;

    lbl now POINTS TO label[0]. So components.lbl[i] is the same as label[i] for any 0<i<strlen(label)

  9. #24
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    that assignment is in the original calling function so really I guess I should've called it components.lbl = label1; in the calling function and am trying to do strcpy(components.lbl, label2); in the called function.

    Does that make a difference?

  10. #25
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    I don't know if I posted the full functions yet or not so here we go:

    pass1.c
    Code:
    /* Pre-Processor Directives */
    #include <string.h>
    
    #ifndef SYMBSIZE
       #define SYMBSIZE 10
    #endif
    
    typedef struct tablemem
    {
       char symbol[SYMBSIZE];
       int value;
       int casenmbr;
       int otherinfo;
    } tabletype;
    
    struct toklist
    {
       char *lbl;
       char *opc;
       char *opr1;
       char *opr2;
       int *ni;
       int *xbpe;
    } components;
    
    extern char sourcelist[][81];
    extern int sourcecnt;
    extern int errors[];
    extern tabletype symbtab[];
    extern int symbtabsize;
    extern tabletype codetable[];
    extern int codetabsize;
    
    int pass1()
    {
       int Scnt = 0;
       int Locctr = 0;
       int ENDval = 0;
       int Errorflag = 0;
       int returnVal = 0;
    
       char label[81], opcode[81], opr1[81], opr2[81];
       int ni, xbpe;
       components.lbl = label;
       components.opc = opcode;
       components.opr1 = opr1;
       components.opr2 = opr2;
       components.ni = &ni;
       components.xbpe = &xbpe;
    
       struct tablemem newentry;
    
       while(sourcelist[Scnt] == ".")
          Scnt++;
    
       breakup(sourcelist[Scnt], components);
    
       if(components.opc == "START")
       {
          Locctr = (int)strtol(components.opr1, 0, 16);
          ENDval = (int)strtol(components.opr1, 0, 16);
    
          if(strcmp(components.lbl, "\0") != 0)
          {
             strcpy(newentry.symbol, components.lbl);
             newentry.value = Locctr;
             newentry.casenmbr = 0;
             newentry.otherinfo = 0;
    
             insert(newentry, symbtab, &symbtabsize);
          }
          Scnt++;
          breakup(sourcelist[Scnt], components);
       }
    
       while(components.opc != "END")
       {
          if(sourcelist[Scnt] != ".")
          {
             if(components.lbl != '\0')
             {
                int index = findfirst(symbtab, symbtabsize, components.lbl);
    
                if(index == -1)
                {
                   strcpy(newentry.symbol, components.lbl);
                   newentry.value = Locctr;
                   newentry.casenmbr = 0;
                   newentry.otherinfo = 0;
    
                   insert(newentry, symbtab, &symbtabsize);
                }
                else
                {
                   errors[Scnt] = 1;
                   Errorflag = 1;
                }
             }
    
             int opcodeIndex = findfirst(codetable, codetabsize, components.opc);
    
             if(opcodeIndex != -1)
             {
                int opcodecase = 0;
    
                if(components.opc == "RESW" || components.opc == "RESB")
                   opcodecase = 1;
                else if(components.opc == "WORD" || components.opc == "BYTE")
                   opcodecase = 2;
    
                switch(opcodecase)
                {
                   case 1:
                   {
                      Locctr += storageincr(opcodeIndex, components.opr1);
                      break;
                   }
                   case 2:
                   {
                      Locctr += storageincr(opcodeIndex, components.opr1);
                      break;
                   }
                   default:
                   {
                      Locctr += opcodeincr(opcodeIndex, components.ni,
                         components.xbpe);
                      break;
                   }
                }
             }
             else
             {
                /* directives such as BASE handled here or error flags */
                Errorflag = 1;
             }
          }
          Scnt++;
          breakup(sourcelist[Scnt], components);
       }
       if(strcmp(components.lbl, "\0") != 0)
       {
          int index = findfirst(symbtab, symbtabsize, components.lbl);
    
          if(index == -1)
          {
             strcpy(newentry.symbol, components.lbl);
             newentry.value = Locctr;
             newentry.casenmbr = 0;
             newentry.otherinfo = 0;
    
             insert(newentry, symbtab, &symbtabsize);
          }
          else
          {
             errors[Scnt] = 1;
             Errorflag = 1;
          }
       }
    
       if(strcmp(components.opr1, "\0") != 0)
       {
          int index = findfirst(symbtab, symbtabsize, components.opr1);
    
          if(index != -1)
             ENDval = symbtab[index].value;
       }
    
       if(ENDval != 0)
          returnVal = ENDval;
       else
       {
          // Set returnVal to START value
       }
    
       if(Errorflag == 1)
          returnVal = -1;
    
       return returnVal;
    }
    breakup.c
    Code:
    /* Pre-Processor Directives */
    #include <string.h>
    
    struct toklist
    {
       char *lbl;
       char *opc;
       char *opr1;
       char *opr2;
       int *ni;
       int *xbpe;
    } ;
    
    void breakup(char sourceline[], struct toklist components)
    {
       char workingstring[81], temp[81], *ptr;
       char label[81], opcode[81], operand1[81], operand2[81];
       int ni = 3, bp = 0;
       int xbpe = *components.xbpe;
    
       /* Remove x and e bits */
       if(xbpe > 7)
          xbpe -= 8;
       if(xbpe > 3)
       {
          bp += 4;
          xbpe -= 4;
       }
       if(xbpe > 1)
       {
          bp += 2;
          xbpe -= 2;
       }
       xbpe = 0;
    
       strcpy(workingstring, sourceline);
       strtok(workingstring, "\n");
       label[0] = '\0';
       opcode[0] = '\0';
       operand1[0] = '\0';
       operand2[0] = '\0';
    
       /* Check for a label */
       if(workingstring[0] == ' ' || workingstring[0] == '\t')
       {/* No label, tokenize opcode */
          if((ptr = strtok(workingstring, " \t")) != NULL)
          {
             if(ptr[0] == '*')
             {
                ptr = ptr + 1;
                ni = 0;
             }
             if(ptr[0] == '+')
             {
                ptr = ptr + 1;
                xbpe += 1;
             }
             strcpy(opcode, ptr);
          }
       }
       else
       {/* Tokenize label and opcode */
          if((ptr = strtok(workingstring, " \t")) != NULL)
             strcpy(label, ptr);
          if((ptr = strtok(NULL, " \t")) != NULL)
          {
             if(ptr[0] == '*')
             {
                ptr = ptr + 1;
                ni = 0;
             }
             if(ptr[0] == '+')
             {
                ptr = ptr + 1;
                xbpe += 1;
             }
             strcpy(opcode, ptr);
          }
       }
    
       /* Remove White Space */
       if((ptr = strtok(NULL, "")) != NULL)
       {
          while(ptr[0] == ' ' || ptr[0] == '\t')
             ptr = ptr + 1;
          strcpy(workingstring, ptr);
       }
    
       /* Set n bit for indirect addressing */
       if(workingstring[0] == '@')
       {
          strcpy(workingstring, workingstring + 1);
          ni = 2;
       }
       /* Set i bit for immediate addressing */
       if(workingstring[0] == '#')
       {
          strcpy(workingstring, workingstring + 1);
          ni = 1;
       }
    
       /* Check for single quote */
       if(workingstring[1] == '\'' || workingstring[2] == '\'')
       {
          ptr = strtok(workingstring, "\'");
          strcpy(temp, ptr);
          strcat(temp, "\'");
          ptr = strtok(NULL, "\'");
          strcat(temp, ptr);
          strcat(temp, "\'");
          strcpy(operand1, temp);
       }
       else
          /* Tokenize operand1 */   
          if((ptr = strtok(workingstring, ", \t")) != NULL)
             strcpy(operand1, ptr);
    
       /* Tokenize operand2 */
       if((ptr = strtok(NULL, ", \t")) != NULL)
          strcpy(operand2, ptr);
       /* If there is an operand2 set x bit */
       if(strncmp(operand2, "\0", 1) != 0)
          xbpe += 8;
    
       /* Set values in the components */
       strcpy(components.lbl, label); 
       strcpy(components.opc, opcode); 
       strcpy(components.opr1, operand1); 
       strcpy(components.opr2, operand2);
       *components.ni = ni;
       *components.xbpe = xbpe + bp;
    }

  11. #26
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    I'm still looking for any help on this if anyone has any ideas.

  12. #27
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Hey! Can you just refresh what exactly the problem was? What are you trying to get done and isn't working? Try to be specific.

  13. #28
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Well I'm getting a segmentation fault, not really sure if I'm looking at the right place in my code. I'm supposed to be reading a test file, breaking it into an array for each line and then tokenizing those lines up for an assembler. I'm essentially creating pass 1 of a 2 pass assembler. This is the error I'm getting right now:

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x00000000004029d9 in breakup (
        sourceline=0x504ea0 ".CHECK   getsource removal of initial blank lines", components=
          {lbl = 0x7fbffff780 ".CHECK", opc = 0x34c3315cf0 "getsource", opr1 = 0x7fbffff790 "removal", opr2 = 0x7fbffff750 "of", ni = 0xab8aa1e, xbpe = 0x4006e6})
        at breakup.c:130
    130        *components.ni = ni;
    (gdb) bt
    #0  0x00000000004029d9 in breakup (
        sourceline=0x504ea0 ".CHECK   getsource removal of initial blank lines", components=
          {lbl = 0x7fbffff780 ".CHECK", opc = 0x34c3315cf0 "getsource", opr1 = 0x7fbffff790 "removal", opr2 = 0x7fbffff750 "of", ni = 0xab8aa1e, xbpe = 0x4006e6})
        at breakup.c:130
    #1  0x0000000000401e2d in pass1 () at pass1.c:56
    #2  0x0000000000400e81 in main ()

  14. #29
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Here is my current source as well for review/reference:

    pass1.c
    Code:
    /* Pre-Processor Directives */
    #include <string.h>
    
    #ifndef SYMBSIZE
       #define SYMBSIZE 10
    #endif
    
    typedef struct tablemem
    {
       char symbol[SYMBSIZE];
       int value;
       int casenmbr;
       int otherinfo;
    } tabletype;
    
    struct toklist
    {
       char *lbl;
       char *opc;
       char *opr1;
       char *opr2;
       int *ni;
       int *xbpe;
    } components;
    
    extern char sourcelist[][81];
    extern int sourcecnt;
    extern int errors[];
    extern tabletype symbtab[];
    extern int symbtabsize;
    extern tabletype codetable[];
    extern int codetabsize;
    
    int pass1()
    {
       int Scnt = 0;
       int Locctr = 0;
       int ENDval = 0;
       int Errorflag = 0;
       int returnVal = 0;
    
       char label[81], opcode[81], opr1[81], opr2[81];
       int ni, xbpe;
       components.lbl = label;
       components.opc = opcode;
       components.opr1 = opr1;
       components.opr2 = opr2;
       components.ni = &ni;
       components.xbpe = &xbpe;
    
       struct tablemem newentry;
    
       while(sourcelist[Scnt] == ".")
          Scnt++;
    
       breakup(sourcelist[Scnt], &components);
    
       if(components.opc == "START")
       {
          Locctr = (int)strtol(components.opr1, 0, 16);
          ENDval = (int)strtol(components.opr1, 0, 16);
    
          if(strcmp(components.lbl, "\0") != 0)
          {
             strcpy(newentry.symbol, components.lbl);
             newentry.value = Locctr;
             newentry.casenmbr = 0;
             newentry.otherinfo = 0;
    
             insert(newentry, symbtab, &symbtabsize);
          }
          Scnt++;
          breakup(sourcelist[Scnt], components);
       }
    
       while(components.opc != "END")
       {
          if(sourcelist[Scnt] != ".")
          {
             if(components.lbl != '\0')
             {
                int index = findfirst(symbtab, symbtabsize, components.lbl);
    
                if(index == -1)
                {
                   strcpy(newentry.symbol, components.lbl);
                   newentry.value = Locctr;
                   newentry.casenmbr = 0;
                   newentry.otherinfo = 0;
    
                   insert(newentry, symbtab, &symbtabsize);
                }
                else
                {
                   errors[Scnt] = 1;
                   Errorflag = 1;
                }
             }
    
             int opcodeIndex = findfirst(codetable, codetabsize, components.opc);
    
             if(opcodeIndex != -1)
             {
                int opcodecase = 0;
    
                if(components.opc == "RESW" || components.opc == "RESB")
                   opcodecase = 1;
                else if(components.opc == "WORD" || components.opc == "BYTE")
                   opcodecase = 2;
    
                switch(opcodecase)
                {
                   case 1:
                   {
                      Locctr += storageincr(opcodeIndex, components.opr1);
                      break;
                   }
                   case 2:
                   {
                      Locctr += storageincr(opcodeIndex, components.opr1);
                      break;
                   }
                   default:
                   {
                      Locctr += opcodeincr(opcodeIndex, components.ni,
                         components.xbpe);
                      break;
                   }
                }
             }
             else
             {
                /* directives such as BASE handled here or error flags */
                Errorflag = 1;
             }
          }
          Scnt++;
          breakup(sourcelist[Scnt], components);
       }
       if(strcmp(components.lbl, "\0") != 0)
       {
          int index = findfirst(symbtab, symbtabsize, components.lbl);
    
          if(index == -1)
          {
             strcpy(newentry.symbol, components.lbl);
             newentry.value = Locctr;
             newentry.casenmbr = 0;
             newentry.otherinfo = 0;
    
             insert(newentry, symbtab, &symbtabsize);
          }
          else
          {
             errors[Scnt] = 1;
             Errorflag = 1;
          }
       }
    
       if(strcmp(components.opr1, "\0") != 0)
       {
          int index = findfirst(symbtab, symbtabsize, components.opr1);
    
          if(index != -1)
             ENDval = symbtab[index].value;
       }
    
       if(ENDval != 0)
          returnVal = ENDval;
       else
       {
          // Set returnVal to START value
       }
    
       if(Errorflag == 1)
          returnVal = -1;
    
       return returnVal;
    }
    breakup.c
    Code:
    /* Pre-Processor Directives */
    #include <string.h>
    
    struct toklist
    {
       char *lbl;
       char *opc;
       char *opr1;
       char *opr2;
       int *ni;
       int *xbpe;
    } ;
    
    void breakup(char sourceline[], struct toklist components)
    {
       char workingstring[81], temp[81], *ptr;
       char label[81], opcode[81], operand1[81], operand2[81];
       int ni = 3, bp = 0;
       int xbpe = *components.xbpe;
    
       /* Remove x and e bits */
       if(xbpe > 7)
          xbpe -= 8;
       if(xbpe > 3)
       {
          bp += 4;
          xbpe -= 4;
       }
       if(xbpe > 1)
       {
          bp += 2;
          xbpe -= 2;
       }
       xbpe = 0;
    
       strcpy(workingstring, sourceline);
       strtok(workingstring, "\n");
       label[0] = '\0';
       opcode[0] = '\0';
       operand1[0] = '\0';
       operand2[0] = '\0';
    
       /* Check for a label */
       if(workingstring[0] == ' ' || workingstring[0] == '\t')
       {/* No label, tokenize opcode */
          if((ptr = strtok(workingstring, " \t")) != NULL)
          {
             if(ptr[0] == '*')
             {
                ptr = ptr + 1;
                ni = 0;
             }
             if(ptr[0] == '+')
             {
                ptr = ptr + 1;
                xbpe += 1;
             }
             strcpy(opcode, ptr);
          }
       }
       else
       {/* Tokenize label and opcode */
          if((ptr = strtok(workingstring, " \t")) != NULL)
             strcpy(label, ptr);
          if((ptr = strtok(NULL, " \t")) != NULL)
          {
             if(ptr[0] == '*')
             {
                ptr = ptr + 1;
                ni = 0;
             }
             if(ptr[0] == '+')
             {
                ptr = ptr + 1;
                xbpe += 1;
             }
             strcpy(opcode, ptr);
          }
       }
    
       /* Remove White Space */
       if((ptr = strtok(NULL, "")) != NULL)
       {
          while(ptr[0] == ' ' || ptr[0] == '\t')
             ptr = ptr + 1;
          strcpy(workingstring, ptr);
       }
    
       /* Set n bit for indirect addressing */
       if(workingstring[0] == '@')
       {
          strcpy(workingstring, workingstring + 1);
          ni = 2;
       }
       /* Set i bit for immediate addressing */
       if(workingstring[0] == '#')
       {
          strcpy(workingstring, workingstring + 1);
          ni = 1;
       }
    
       /* Check for single quote */
       if(workingstring[1] == '\'' || workingstring[2] == '\'')
       {
          ptr = strtok(workingstring, "\'");
          strcpy(temp, ptr);
          strcat(temp, "\'");
          ptr = strtok(NULL, "\'");
          strcat(temp, ptr);
          strcat(temp, "\'");
          strcpy(operand1, temp);
       }
       else
          /* Tokenize operand1 */   
          if((ptr = strtok(workingstring, ", \t")) != NULL)
             strcpy(operand1, ptr);
    
       /* Tokenize operand2 */
       if((ptr = strtok(NULL, ", \t")) != NULL)
          strcpy(operand2, ptr);
       /* If there is an operand2 set x bit */
       if(strncmp(operand2, "\0", 1) != 0)
          xbpe += 8;
    
       /* Set values in the components */
       strcpy(components.lbl, label); 
       strcpy(components.opc, opcode); 
       strcpy(components.opr1, operand1); 
       strcpy(components.opr2, operand2);
       *components.ni = ni;
       *components.xbpe = xbpe + bp;
    }

  15. #30
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    You may notice in pass1 the first instance of calling breakup I tried sending the address of components, I'm not sure if that's correct or not though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM