I need help finding cause of the segmentation fault in my server.c.

I've been using gdb line by line for this pset and I finally think I can use it. However, it reports the seg fault at the line 130 which is the reset() function.

// accept connections one at a time
while (true)
    // reset server's state

    // wait/(block (am) until client is connected
    if (connected())
    {  etc...
  • which of course is not code that I wrote, but comes with the file given to us.

I realize it will be caused by my code, but how can I find the problem when it only reports an issue with this line?

I've run valgrind and it actually says I have no leaks and no errors. Although, does Valgrind actually work for server.c if not running it with telnet etc?

Please tell me how you'd like me to upload my code... I've stepped through almost line by line with gdb and my variables seem to all be correct. Sometimes gdb has my query variable optimized out - so maybe the issue is there - but the pointers to the query value seem to be all correct. I don't know the way around finding a variable value if gdb decides to optimise out...

Thanks very much in advance. I have spent days and days on this pset and I just can't solve this myself.

  • Where in reset does it segfault? What happens if you step into it? server.c is 99% string manipulation, so chances are you're exceeding some of your arrays somewhere, or chopping them the wrong way. But it would help to know exactly where the segfault is and what values your variables have at that moment
    – Irene
    Aug 16 '15 at 2:12
  • Thanks for reply. I stepped through the program with gdb - maybe I don't know tricks with gdb - I'm setting breaks at so many lines - I presume it is going back to do something at line 130 - because although the seg fault has never changed from reporting line 130, the place in the rest of the code when it decides to flash back to line 130 to reset has progressed further through the program as I fixed my code. Does that make sense? Now, I think the problem is happening around the - ensure path exists part of my code. But the path variable seems correct.
    – wow
    Aug 16 '15 at 7:26
  • Please can someone help me? It doesn't look like pset6 gets much assistance on this forum, I see quite a few unanswered questions.
    – wow
    Aug 18 '15 at 2:22
  • Hi, wow. This may help you debug a segmentation fault: unknownroad.com/rtfm/gdbtut/gdbsegfault.html Run gdb according to those steps, from the line where they mention the segfault (the way gdb is run is slightly different, but the rest should work). That should help you pinpoint exactly what happens when it segfaults.
    – Irene
    Aug 23 '15 at 6:09
  • Thanks for attempting to help Irene. But it didn't help as I already understood how to get the line at which the seg fault occurs. As I detail above, the line it seg faults at is NOT code that I wrote - it came with the pset. And its just the server reset function.
    – wow
    Aug 28 '15 at 22:29

have you tried using the 's' command in GDB to step into the reset() function? That might give me more information about what is happening in reset to cause the segmentation fault.

  • Hi Adam, I appreciate any help. Yes, I've stepped through using 's'. I've got a screenshot if it helps (but I don't know how to upload). Nothing there helped me - but it may you... Program received signal SIGSEGV, Segmentation fault. main (argc=5, argv=0xbffff114) at server.c:130
    – wow
    Aug 28 '15 at 22:39
  • If you stepped through the reset() function with "s", you will see exactly in which of the lines it segfaults. Is it as soon as it enters the function? Does it not enter the function at all? What is on line 130 exactly? Is there any chance you have something before that that sort of "broke" the code? In the original file, the call to reset() is on line 115 and there's nothing for us to do before that line. What extra code have you added?
    – Irene
    Aug 29 '15 at 20:21
  • Line 130 is the original line 115 - I just have comments that I added to the file for learning purposes - I didn't change the code there at all.
    – wow
    Aug 29 '15 at 23:05
  • The actual code that's in question was always posted above with my original question... it's the reset server function that came with the pset. Please can someone help me? Desperate to learn from this so I can move on to the next pset and one day finish this course...
    – wow
    Sep 5 '15 at 0:22
  • Please can someone help me? I am still stuck here.
    – wow
    Sep 12 '15 at 22:31

I get segmentation fault too, but I can't even run gdb. When I type "gdb ./server public" I get an error "The program is not being run".... how do you get to run gdb with server.c?

  • make server -> gdb server -> break main -> run public
    – ChrisG
    Aug 27 '15 at 15:21
  • Thanks man! I was about to give up on gdb and go back to using printf's instead.... I'm hoping that I can now find out what causes the segmentation fault...
    – Anja
    Aug 27 '15 at 16:29
  • Now, could you remove this post, as it's not an actual answer? Thanks! Keep coding!
    – ChrisG
    Aug 27 '15 at 16:30

You must log in to answer this question.

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