I have run GDB on this section of code using http://localhost:8080/test/ To mimic the check50 test to see why my code is failing that test. When I did this, I got the following when info locals was run:

try1 = 0x7fffffff9c00 "/home/ubuntu/workspace/pset6/public/test/index.php" try2 = 0x7fffffff9bc0 "/home/ubuntu/workspace/pset6/public/test/index.html"

This seems all fine to me as strcat was causing issues so I made 2 test cases. When I then get to the conditions in my code, the code runs down the first branch of the condition (it chooses that index.php exists in /test/ folder which it quite obviously does not.

I have attached the link to my indexes function below:


Any idea why this would happen? It seems like this should be no problem at all and yet it is causing me a lot of issues. I have gotten thru getting the right output and I am returning a pointer as expected, this seems like a very weird issue to have with a correct address. Any help is appreciated.

1 Answer 1


If you are going to treat array and array2 as string literals, why not declare them as such (eg char* array = "index.php"). No allocation muss, no null-terminator fuss.

gdb hint: inspect the value of index in main after you return from indexes.

The real problem comes here: char* ptr_best = try1; (and try2 too). You're setting a the pointer ptr_best the the local pointer try1, which will be out of scope when you return to main. If you want to return try1, why not just return try1? Sometimes, less is more.

  • code: gist.github.com/anonymous/095ad60b187373601d9241fecc9dd819 So I changed a few things to be more in line with what you are talking about but am still encountering the same issue. When run with "localhost:8080/test" the program is still routing me into the first condition where index.php exists. According to gdb, this is what is going on right before the first if statement is checked: (see below). Why are path1/path2 matching try1/try2? That does not make sense to me. As far as I can see, the char* going in looks right...what am I missing?
    – Zach B
    Aug 31, 2016 at 19:52
  • 456 if(access(try1, F_OK )) (gdb) info locals path1 = 0x6070e0 "/home/ubuntu/workspace/pset6/public/test/index.php" path2 = 0x607120 "/home/ubuntu/workspace/pset6/public/test/index.html" array = 0x4040f5 "index.php" array2 = 0x4040ff "index.html" try1 = 0x6070e0 "/home/ubuntu/workspace/pset6/public/test/index.php" try2 = 0x607120 "/home/ubuntu/workspace/pset6/public/test/index.html"
    – Zach B
    Aug 31, 2016 at 19:52
  • Missed this on the first go round: ` if(access(try1, F_OK ))`. Read the man page on access. Pay special attention to the return value (spoiler: it's not a bool!). Re. path1,path2: That's what a buffer overrun looks like. Since try1 and try2 don't have any memory of their own allocated...... Aug 31, 2016 at 22:15
  • Ah ok, that makes a bit more sense. Here I was looking at the usage and I missed that. Thanks for the help!
    – Zach B
    Sep 1, 2016 at 4:46

You must log in to answer this question.

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