getenv() causes segfault in recv() loop

This is a discussion on getenv() causes segfault in recv() loop within the Networking/Device Communication forums, part of the General Programming Boards category; I've written a recv loop function that looks like this: Code: int Recept (int sock, char *buffer) { int count=0; ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    getenv() causes segfault in recv() loop

    I've written a recv loop function that looks like this:
    Code:
    int Recept (int sock, char *buffer) {
            int count=0;
            char *ptr=buffer;
            if ((buffer=malloc(1))==NULL) return -2;
            while (recv(sock,ptr,1,0)>0) {
                    printf("bytes=%6d\t%c\n",count,ptr[0]);      // for debugging
                    if ((ptr[0]=='\n') && (count > 0) && (buffer[count]=='\r')) {
                            buffer[count]='\0';
                            puts ("Recept() done.");
                            return count;} 
                    ptr++; count++;
                    buffer=realloc(buffer,count+1);
                    if (buffer==NULL) return -3;
            }
            return -1; 
    }
    It reaches the end (but sometimes not) of a GET request (eg, "...</html>") and then segfaults before "Recept () done" appears -- apparently in getenv() from libc.so.6. I'm almost positive it worked fine yesturday, although I have changed the code which accounts for the connection on sock.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    your ptr pointer points into some address that has nothing to do with storage allocated with malloc/realloc... so you writing to some random address...

    And I do not see a reason to pass buffer as a parameter - you change its value in the function anyway and the calling function does not know about it... - so it has no feed back anyway
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    your ptr pointer points into some address that has nothing to do with storage allocated with malloc/realloc... so you writing to some random address...
    I was surprised by this but you're right. Anyway, I changed it to use a buffer with a fixed size:
    Code:
    int Recept (int sock, char *buffer, int size) {
            int count=0;
            char *ptr=buffer;
            while (recv(sock,ptr,1,0)>0) {
                    if (ptr[0]=='\n') {
                            if (buffer[count-1]=='\r') buffer[count-1]='\0'; 
                            else buffer[count]='\0';
                            return count;}
                    ptr++; count++;
                    if (count>=size) return -2; 
            }
            perror("Recept() -- recv");
            return -1; 
    }
    and the strange thing that happens now is this: I use it looped in one function for the reply to a HEAD request. No problems. Then I send GET on the same item (a binary) so I want to skip thru the head:
    Code:
    for (i=o;i<10;i++)  {
          tmp=Recept(sock,buffer,4096);         // exactly the same line as in the other function
          printf("&#37;d %s\n",tmp,buffer);                   // just to check
    }
    At every iteration including the first one, I get:

    Recept() -- recv: Illegal seek
    -1


    I've googled around for why recv would return this error and no one seems to have sorted it out, but I still have my fingers crossed
    It would seem that the recv here doesn't work at all, because the next thing is I want the binary and I have Content-Length, so I just do recv(sock,buffer,Content-Length,0), which does work except it includes the ten lines of the head I wanted to skip -- ie, Recept did nothing but fail.
    Last edited by MK27; 11-26-2008 at 03:29 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by MK27 View Post
    Recept() -- recv: Illegal seek
    -1


    I've googled around for why recv would return this error and no one seems to have sorted it out, but I still have my fingers crossed
    recv() returns no such error. You are improperly calling perror() when the call to recv() did not set an error code. recv() can return 0 if the socket is disconnected. Instead of treating this as a normal event you are treating it as an error and you are printing whatever garbage error code happens to be sitting in errno at the moment.

    The error which sits in errno is only a valid error if the last call to a library function return -1.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. QUERY_STRING in CGI with a C/C++ Program
    By stovellpaul in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2002, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21