0

I am confused by the output of strcat. It seems to be adding characters to the beginning of a pointer.

I am stepping through the index function in gdb and printing the values at pointers as I go. Printing the pointer pathfile is giving me unusual results. I get the following:

468         pathfile = NULL;
(gdb) info locals
slash = false
len = 36
pathfile = 0x607a40 "/home/ubuntu/workspace/pset6/public/index.php"
(gdb) print path
$4 = 0x607a10 "/home/ubuntu/workspace/pset6/public/"
(gdb) n
469         pathfile = realloc(pathfile, (len+11)*sizeof(char));
(gdb) print pathfile
$5 = 0x0
(gdb) n
470         strcat(pathfile, path);
(gdb) 
471         if (slash) strcat(pathfile, "/");
(gdb) print pathfile
$6 = 0x607a80 "\f$\255\373/home/ubuntu/workspace/pset6/public/"
(gdb) n
472         strcat(pathfile, "index.html");
(gdb) 
473         if (fopen(pathfile, "r") != NULL) return pathfile;
(gdb) print pathfile
$7 = 0x607a80 "\f$\255\373/home/ubuntu/workspace/pset6/public/index.html"
(gdb) 

When I print locals I see that pathfile has "index.php" at the end of it. This is what I expect. My program then sets the pointer to NULL, and I use strcat on line 470 as the first step to look for "index.html". I print pathfile again and I get $6 which has stuff at the beginning of the pointer!? What is that? How did it get there?

My function is shown here:

char* indexes(const char* path)
{
    bool slash = false;
    int len = strlen(path);
    if (path[len-1] != '/')
    {
        len++;
        slash = true;
    }
//  Check for index.php
    char* pathfile = malloc((len+10)*sizeof(char));
    strcat(pathfile, path);
    if (slash) strcat(pathfile, "/");
    strcat(pathfile, "index.php");
    if(fopen(pathfile, "r") != NULL) return pathfile;
    //free(pathfile);

//  Check for index.html
    //pathfile = realloc(pathfile, (len+11)*sizeof(char)); // changed from malloc to realloc
    pathfile = NULL;
    pathfile = realloc(pathfile, (len+11)*sizeof(char));
    strcat(pathfile, path);
    if (slash) strcat(pathfile, "/");
    strcat(pathfile, "index.html");
    if (fopen(pathfile, "r") != NULL) return pathfile;
    //free(pathfile);

    return NULL;
}

I haven't been able to get check50 to work in a couple of days but when I run the server and select test, I do not get redirected to the video, I just see the directory. When I try to reload test, I get an error.

1
  • Note: I got check50 to work and I only have one error: :( Requesting /test/ outputs /test/index.html. – Brendan Rafferty Jun 4 '16 at 9:06
1

first I wanna mention that it's really bad practice to have magical numbers (e.g., 10 and 11) in your code as they make your code difficult to understand.

second, you don't really need to check whether path ends with a '/'. you may assume that per the distribution code.

you should use access to check whether a file exists rather than unnecessarily fopening the file. man access for more information!


char* pathfile = malloc((len + 10) * sizeof(char));
strcat(pathfile, path);

so you're malloc'ing a block of memory that's big enough to hold path appended to it index.php. per strcat's man page:

char *strcat(char *dest, const char *src);

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.

the problem here is that pathfile is not actually a string yet. there is no guarantee that it will have a '\0' and even if it does, no guarantee on where it would be. so you shouldn't concatenate path to pathfile. rather, you should copy the latter into the former. man strcpy for more information!


in case index.php doesn't exist, you never free the memory allocated for index.php's path (no, setting pathfile to NULL doesn't free memory). you need to call free to do that. man 3 free for more information!

after calling free on pathfile, you may allocate a new block of memory for it by calling malloc. it's more convenient to use malloc here because, first, you're allocating a new block of memory for a different path (not resizing a block of memory), and, second, you wouldn't have to set pathfile to NULL before calling malloc.

finally, same notes from above apply to the way you're trying check for index.html.

You must log in to answer this question.

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