0

My program compiles correctly, but when I use check50, I get multiple errors. I believe there is a segmentation fault in indexes because all of the check50 tests return 0.

Does the segmentation fault result from my use of malloc() in my code?

Thanks in advance!

Here is my code:

// edited out per academic honesty    

Here is check50:

:) server.c exists
:) server compiles
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
:( Requesting cat.html returns 200, text/html, and correct file
:( Requesting cat2.HTML returns 200, text/html, and correct file
:( Requesting cat3.HtMl returns 200, text/html, and correct file
:( Requesting cat.gif returns 200, image/gif, and correct file
:( Requesting favicon.ico returns 200, image/x-icon, and correct file
:( Requesting test.css returns 200, text/css, and correct file
:( Requesting test.js returns 200, text/javascript, and correct file
:( Requesting hello.php returns 200, text/html, and correct output
   \ expected output, not an exit code of 0
:( Requesting hello.php? returns 200, text/html, and correct output
   \ expected output, not an exit code of 0
:( Requesting hello.php?name=Alice returns 200, text/html, and correct output
   \ expected output, not an exit code of 0
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
:( Requesting directory containing index.php outputs index.php
   \ expected output, not an exit code of 0
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
5
  • Catastrophic check50/server2 failure usually implicates load. joined_string and joined_string2 are not allocated, which will cause a memory leak, but a request to cat.jpg et al will not call indexes, so it's not the whole problem. The seg fault on all php calls may indicate an additional problem in load, ie using some "file-positioning" call (like fseek), which can't be used with pipes. info here. Commented Jan 18, 2017 at 15:06
  • Thank you! I'll fix the strings and then tweak my code. Should I mark this as an answer? Commented Jan 18, 2017 at 16:17
  • 1
    "joined_string and joined_string2 are not allocated" They are only pointers to string and string2, they do not need malloc, we can use valgrind and there are no leaks
    – MARS
    Commented Jan 18, 2017 at 22:36
  • Ah, thanks @MARS, I see. No leaks true BUT since it is a local pointer to the (local) result of strcat(string1, index_php);, this sequence free(string1); return joined_string; will return "nothing". Ditto joined_string2. Commented Jan 19, 2017 at 1:39
  • @DinoCoderSaurus If I'm following correctly, free(string1); return joined_string; does nothing, right? If I don't need to allocate memory, should I re-arrange my sequences? Commented Jan 19, 2017 at 13:40

1 Answer 1

1

There are problems in this index function. However, they are not causing all the check50 failures. Those problems probably stem from load function. Since check50 (probably) makes a direct request to cat.jpg (as with curl), server would not call indexes, thus it could not account for that failure.

The problem in the indexes function has to do with scope. string1 is allocated locally here
char* string1 = malloc(strlen(path) + strlen(index_php) + 1);.
This line char* joined_string = strcat(string1, index_php); creates joined_string as a pointer into string1.
This line free(string1); essentially "destroys the contents" of string1. Now joined_string has no "value", so this return joined_string; returns nothing. (Thanks @MARS for clarifying).

Remember, strcat modifies it's first argument. So this strcat(string1,index_php) concatenates index_php to string1. Exactly what you want! Then all you have to do is return string1;. Sometimes less is more :)

Remember to free(string1); if you find string2.

You will need to make the similar adjustments to the string2 routine.

Once you correct these problems, you will likely still have check50/server2 failures because of problems in the load function.

The seg fault failures \ expected output, not an exit code of 0 on all (and only) the php files make me suspect you are using a "file-positioning" function, like fseek, in your load routine. That will always lead to failure. The first part of this answer explains why.

You might find the info at the end of this answer helpful when trying to use gdb to troubleshoot load.

3
  • Thanks so much for this! Commented Jan 19, 2017 at 20:31
  • I do not use a fseek() in my code, though. I only use EOF and fgetc(), making my load code a lot like my load function for an earlier problem set. Are either of these considered "file-positioning function"? Commented Jan 19, 2017 at 20:42
  • ftell, fseek, rewind are file positioning functions. fgetc is not. Commented Jan 19, 2017 at 21:31

You must log in to answer this question.

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