C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 11-26-2008, 10:08 AM   #1
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,166
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 11-26-2008, 10:11 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,319
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
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is online now   Reply With Quote
Old 11-26-2008, 04:25 PM   #3
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,166
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("%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.
__________________

"A man can't just sit around." -- Larry Walters

Last edited by MK27; 11-26-2008 at 04:29 PM.
MK27 is offline   Reply With Quote
Old 12-05-2008, 02:04 PM   #4
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,759
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:50 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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