0

I have a ‘curl: (52) Empty reply from server’ on my terminal with ‘free(): invalid next size (normal)’ on the debugger.

The execution is fine until if(file == NULL) check in transfer() function. Here is a print screen of file details.

enter image description here

void transfer(const char* path, const char* type)
{
    // ensure path is readable
    if (access(path, R_OK) == -1)
    {
        error(403);
        return;
    }

    // open file
    FILE* file = fopen(path, "r");
    if (file == NULL)
    {
        printf("Reaching here 7\n");
        error(500);
        return;
    }

The printf statement is not executed. But the error(500) statement is highlighted, after which process stops with exit code 0 and the following:

Child terminated with signal = 0x6 (SIGABRT)

My program is passing check50 except for this scenario which I was testing out:

:( Requesting /test/ outputs /test/index.html \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."

Please let me know what could be causing the issue. Thanks!

Here is my indexes function():

char* indexes(const char* path)
{
    DIR *directory = opendir(path);

    if(directory == NULL)
        return NULL;
    struct dirent *name = NULL;
    int indexHtmlFound = 0;
    int indexPhpFound = 0;

    do {
        name = readdir(directory);
        if (name != NULL)
        {
            if(!(strcmp(name->d_name, "index.html")))
            {
                indexHtmlFound = 1;
                break;
            }
            if(!(strcmp(name->d_name, "index.php")))
            {
                indexPhpFound = 1; 
                break;
            }
        }
    } while(name != NULL);

    closedir(directory);

    char *newPath = malloc(sizeof(path)+11);
    if (newPath == NULL)
        return NULL;
    strcpy(newPath, path);
    newPath[strlen(path)] = '\0';

    if(indexHtmlFound)
    {
        strcat(newPath, "index.html");
        newPath[strlen(newPath)] = '\0';
    }
    else if(indexPhpFound)
    {
        strcat(newPath, "index.php");
        newPath[strlen(newPath)] = '\0';
    }

    if(indexHtmlFound || indexPhpFound)
        return newPath;
    else
    {
        free(newPath);
        return NULL;
    }
}
6
  • looks like a bug in indexes function because /test/ is a dir, so indexes is called. Program will execute` transfer` when type is "text/x-php", so there's another connection. As all other check50 tests pass, abs_path is correct. You could troubleshoot and post your resolution as answer, or post indexes function to get additional help. – DinoCoderSaurus May 19 '16 at 11:42
  • Thank you @DinoCoderSaurus. I have added the indexes function. – Neha Shah May 20 '16 at 3:21
  • I think transfer() is called when type is not php. if (strcasecmp("text/x-php", type) == 0) { interpret(path, query); } // transfer file at path else { transfer(path, type); } – Neha Shah May 20 '16 at 3:27
  • Update: With valgrind, output works fine. – Neha Shah May 20 '16 at 3:42
  • Sorry about the transfer mistake. When you tested with valgrind, did you send a request to the server? If you use valgrind server, and ^C, it will not report any errors, because it essentially has not done anything. You need to run valgrind server and then send a request (browser or curl) and then ^C to get real results from valgrind. And different requests will produce different results because of the program flow. – DinoCoderSaurus May 20 '16 at 13:36
1

What's the difference?

char *newPath = malloc(sizeof(path)+11);

char *newPath = malloc(strlen(path)+11);

path is a pointer, sizeof(path) is 4 or 8 depending on architecture. If path is /home/ubuntu/pset6/public.... well, you see what's happening.

Is it necessary to look at all the files in path to determine whether index.php|html exist? Consider how you might use access() to accomplish this task with less code, memory and disk access.

1
  • I can't believe I overlooked it! Thanks for saving the day :) – Neha Shah May 20 '16 at 23:58

You must log in to answer this question.

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