0

I am getting these check50 errors can someone tell me in easy english what they are and why they are caused and the possible solution for it and if my approach to load and indexes functions are correct or not

load function is as follows

bool load(FILE* file, BYTE** content, size_t* length)
{
    // TODO
    if(file == NULL)
    {
        return false;
    }

    // needed when dereferenceing the data to the pointer content so
    // that the next buffer is stored at *content + counter
    int counter = 0;
    int buffer = BYTES;

    //creating the memory block directly at content
    *content = malloc(buffer);

    BYTE buff;
    while(fread(&buff, sizeof(BYTE), 1, file) == 1)
    {
        //if 512 blocks have been read then add another 512 blocks to memory
        if(counter == buffer)
        {
            buffer = buffer + counter;
            *content = realloc(*content, buffer);
        }

        *(*content + counter) = buff;
        counter++;
    }
    *length = counter;
    return true;
}

and indexeses is as follows

char* indexes(const char* path)
{
    // TODO
    if (path != NULL)
    {
        FILE* infile= NULL;

        // to create enough space to add /index.html or index.php in the path
        char* directory = malloc(strlen(path) + 13);

        //copying path to directory
        strcpy(directory, path);

        //to see if index.php is there
        infile = fopen("index.php", "r");
        // that means index.php is there
        if(infile != NULL)
        {
            // adding /index.php in directory's end
            strcat(directory, "/index.php");

            //storing the address of directory to path
            //*path = &directory;
            return directory;
        }
        else
        {
            fclose(infile);
            infile = fopen("index.html", "r");

            if(infile != NULL)
                {
                // adding /index.html in directory's end
                strcat(directory, "/index.html");

                //storing the address of directory to path
                //*path = &directory;
                return directory;
            }
        }

        free(directory);
        fclose(infile);
    }


    return NULL;
}

I managed to get all green faces in the first check50 test.

enter image description here

1
  • Please edit in the check50 results or sandbox link :) Aug 30, 2016 at 13:42

1 Answer 1

0

The two hello.php? errors indicate seg-faults. Maybe (probably) in parse, when the request line contains a query.

The other two errors indicate a problem in indexes, and specifically the second result
\ expected output, not an exit code of 0 indicates a seg-fault.

This is a problem infile = fopen("index.php", "r");. Program will attempt to open "index.php" in present working directory, which is your document root, which is, presumably, public. Look at the check50 test again. It is looking for "/test/index.php" under root. It cannot open that file.

The seg-fault comes here fclose(infile);. Because it did not open a file, infile is NULL, you can't fclose NULL.

Remember, the spec only asks indexes to determine if either file actually exists in path. access is a function that does just that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .