![]() |
| | #1 |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,166
| getenv() causes segfault in recv() loop 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;
}
|
| MK27 is offline | |
| | #2 |
| CSharpener 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 | |
| | #3 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,166
| Quote:
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;
}
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
}
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 04:29 PM. | |
| MK27 is offline | |
| | #4 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,759
| Quote:
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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |