0
votes

I don't know why my code gives me a segmentation fault. Before it worked fine, so i must have done something to it, but I don't know what.

This function gets some Markdown-Elements from a specific line in a file and returns a dynamic array ( int * elements) with the elements

int * parser_getElements(char * string)
{

int * elements;

char * token, * temptoken;
int charcount = 0, elemcount = 1, i = 0;

assert(string != NULL);

elements = malloc(sizeof(int));
*elements = 0;

token = strtok(string," ");

while(token != NULL)
{
    temptoken = token;
    while (*temptoken)
    {
        charcount++;
        ++temptoken;
    }

    if(token[0] == '*' && token[charcount - 1] == '*')
    {

        if(token[1] == '*' && token[charcount - 2] == '*')
        {
            elements[elemcount-1] = EMPH;
        }
        else
        {
            if(token[1] != '*' && token[charcount - 2] != '*')
            {
                elements[elemcount-1] = ITAL;
            }
        }
        elemcount++;
        elements = realloc(elements,sizeof(int) * elemcount);
    }
    else if(token[0] == '_' && token[charcount - 1] == '_')
    {

        if(token[1] == '_' && token[charcount - 2] == '_')
        {
            elements[elemcount-1] = EMPH;
        }
        else
        {
            if(token[1] != '_' && token[charcount - 2] != '_')
            {
                elements[elemcount-1] = ITAL;
            }
        }
        elemcount++;
        elements = realloc(elements,sizeof(int) * elemcount);
    }

    else
    {
        elements[elemcount-1] = TEXT;
        elemcount++;
        elements = realloc(elements,sizeof(int) * elemcount);
    }

    token = strtok(NULL, " ");
    charcount = 0;
}

return elements;
}

The problem occurs in the function below.

void parser_printMDElements(List markdown, FILE * out)
{
MD_Element element;
int count = 0, no_text = 0;

if(markdown != NULL)
{

    element = markdown->value->elements[0];

    while(element <= EMPH)
    {
        if(element > TEXT)
        {
            no_text = 1;
        }
        count++;
        element = markdown->value->elements[count];
    }

    count = 0;
    element = markdown->value->elements[0];

    while(element <= EMPH)
    {
        if(no_text)
        {
            printf("%s\n",MD_Element_Descr[element]);
        }
        count++;
        element = markdown->value->elements[count];
    }

    parser_printMDElements(markdown->next,out);
}
}

This line(first appearance)

element = markdown->value->elements[0];

gives me a segmentation fault and I don't know why. The program stops after this line.

This is the main program

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "errors.h"
#include "parser.h"

int main(int argc, char * argv[])
{

Errorcode error = ERR_NULL;
char * gap;
int gapcount = 1;
List md = parser_newList();

/** Save name of document in firstnode */
md->value = malloc(sizeof(struct NodeValue));
md->value->string = argv[1];
md->type = DOCUMENT;

md->next = malloc(sizeof(struct Node));

error = parser_parseFile(argv[1],&(md->next));
if(error == ERR_NULL)
{
    gap = malloc(sizeof(char));
    *gap = '\0';
    parser_printMDElements(md, gap, gapcount, stdout);
    printf("\n");
}

return 0;
}

This code is extracted from parser.h

enum MD_Element
{
    DOCUMENT,
    LINE,
    SECTION,
    LISTELEMENT,
    H1,
    H2,
    H3,
    H4,
    H5,
    H6,
    TEXT,
    IMG,
    LINK,
    QUOTE,
    CODE,
    ITAL,
    EMPH
};    

typedef enum MD_Element MD_Element;

typedef struct NodeValue * NodeValue;

struct NodeValue
{
    char * string;
    int * elements;
};

typedef struct Node * Node;

struct Node
{
    MD_Element type;
    NodeValue value;
    Node next;
};

typedef Node List;

Can sb. tell me what I've done wrong? Thanks!

1
Well, where is the call to parser_printMDElements? Where is the type definition of List? Where is the initialization of markdown? - barak manos
i edited my post accordingly - user3337413

1 Answers

0
votes

There are still many pieces of your code missing here.

I'm guessing that your problem is possibly in md->value->string = argv[1];.

(athough your question does not show how you are using md->value->string).

If you attempt to change the contents of the memory pointed by md->value->string, then you will have a memory access violation, since this variable is pointing to a read-only memory segment.

If that is indeed the case, then you can solve it by replacing md->value->string = argv[1]; with:

md->value->string = malloc(strlen(argv[1])+1);
strcpy(md->value->string,argv[1]);