0

Working on server.c pset6.I get HTTP/1.1 403 Forbidden while I run server.c. I used gdb and found a flaw in indexes().I used printf statements and figured out that after I use strcpy function the path changes from /home/Ubuntu/workspace/public to lic/ which eventually results in indexes returning NULL and giving 403 error. If someone could help explain that why is this the case. Posting my indexes code

         char* indexes(const char* path)
        {   
         char* returnString1=0;
        char* Inphp="index.php";
         char* Inhtml="index.html";
           returnString1=calloc(1,sizeof(strlen(path)+strlen(Inphp)+1));
         printf("PATh is %s\n",path);
       //copy path to returnString1;
       //add '\0' at the end of returnString1;
         printf("PATh is %s\n",path); 
      // concatenate Inphp to returnString1;
          printf("PATh is %s\n",path);

           printf("ReturnString 1 is %s \n",returnString1);

         char* returnString2=0;
    returnString2=calloc(1,sizeof(strlen(path)+strlen(Inhtml)+1));
        //add '\0' at the end of returnString2;
         printf("PATh is %s\n",path);
         //copy path to returnString2;
           printf("PATh is %s\n",path);
         printf("\nReturnString 2 is %s \n",returnString2);
         // concatenate Inhtml to returnString2;
      printf("ReturnString 2 is %s \n",returnString2);

   if(access(returnString1,F_OK)==1)
 {  //free returnString2; 
   return returnString1;
 }

 else if(access(returnString2,F_OK)==1)
{
   // free returnString1
    return returnString2;
 }
  return NULL;
}

Posting the gdb output picture![GDB SCREENSHOT PATH ]1

1 Answer 1

0

so first of all I gotta say I liked your code a lot... anyway uve 2 small errors. first, when you calloc, you use strlen to specify the number of chars but then instead of multiplying by sizeof(char), you said size of the strlens. second, the last condition checking for access, the return value for access success Is zero not 1, and -1 for error.

hope this helps :)

1
  • Never thought I could err in using calloc, thanks a tonne for figuring out the mistakes. Suggestions helped me out. Jun 11, 2016 at 14:22

You must log in to answer this question.

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