0

I am having problems while running check50 2015.fall.pset6.server2 server.c The thing is that all echecks are correct except these 2:

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

:( Requesting directory containing index.php outputs index.php \ expected output, not standard error of "======= Backtrace: =========\n======= M..."

I have looked and looked but I don't see where the problem may be. I think it has to be with indexes function. I have done all kind of weird things to it as you can see, but it doesn't work, please see code:

char* indexes(const char* path) {
char* php;
char* html;
char* dir;
if (path == NULL) 
{
    return NULL;
}
if (path[0] == '/')
{
    path++;
}
// check to see if dir ends with a /
if (strcmp(&path[(strlen(path) - 1)], "/") == 0)
{
    php = "index.php";
    html = "index.html";
}
else
{
    php = "/index.php";
    html = "/index.html";
}
dir = malloc(sizeof(strlen(path) + strlen(php) + 1));
strcpy(dir, path);
strcat(dir, php);
dir[strlen(path) + strlen(php)] = '\0';
if (access(dir, F_OK) != -1)
{
     return dir;
}
else
{
    free(dir);
    dir = malloc(sizeof(strlen(path) + strlen(html) + 1));
    strcpy(dir, path);
    strcat(dir, html);
    dir[strlen(path) + strlen(html)] = '\0';
    if (access(dir, F_OK) != -1)
    {
       return dir;
    }
    free(dir);
}
return NULL;}

Thanks for any help!!

1

The old sizeof trap. strlen(path) + strlen(php) + 1 returns an integer. sizeof an integer is 4. In two places. Do not use sizeof. Here is a good explanation.

Think about this, if you allocate dir for the larger string in the first place, there would be no need to free/malloc it again. That doesn't appear to be causing a problem (besides the sizeof problem of course). It's just more work for the program and the programmer.

Maybe you added this line dir[strlen(path) + strlen(php)] = '\0'; and the similar one for html for troubleshooting, but they are unnecessary. According to man strcat:

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.

Again, this doesn't seem to be part of the problem, just more work for the program and the programmer.

Of course you may discover other problems after this is fixed. Keep at it!

5
  • Thanks. I have deleted the '\0' lines, and only malloc the larger html string (dir = malloc(sizeof(strlen(path) + strlen(html) + 1)), also deleted the free part, but as you said, that was not the problem, so we back to the original question of those two errors. Please help! – Javi Oct 14 '16 at 10:05
  • Do not use sizeof. Full stop. sizeof(int) is 4. You are allocating dir for 4 bytes. Not big enough.strlen(path) + strlen(html) + 1 is how many bytes you want dir to be. Here is a good explanation. – DinoCoderSaurus Oct 14 '16 at 11:07
  • thanks again. Things seem to get better even though the erros remain. This is the output of check50: :( Requesting /test/ outputs /test/index.html :( Requesting directory containing index.php outputs index.php \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..." – Javi Oct 14 '16 at 12:11
  • What is this all about? if (path[0] == '/') { path++; } – DinoCoderSaurus Oct 14 '16 at 12:17
  • I had it to eliminate /dir/ instances since I thought it was causing access() to return -1. I was wrong big time. Now fixed! Thanks!! – Javi Oct 14 '16 at 12:39

You must log in to answer this question.

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