LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2010, 01:46 PM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
eliminate garbage from buffer c++


Code:
char retBuffer[100];

recv(sock , retBuffer ,sizeof(retBuffer) , 0);
if i send a string smaller then my buffer size retBuffer always have garbage of the end of it....what can i do to eliminate tis problem ?
 
Old 03-04-2010, 01:50 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
recv returns the number of bytes received
Code:
int num=recv(sock , retBuffer ,sizeof(retBuffer) , 0);
if(num==-1) printf("error");
else retBuffer[num]=0;

Last edited by troop; 03-04-2010 at 01:54 PM.
 
Old 03-05-2010, 12:42 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Minor bug:
Code:
// Potential buffer overrun
  int num = recv(sock, retBuffer, sizeof(retBuffer), 0);
  if(num == -1) 
    printf("error");
  else 
    retBuffer[num] = 0;
Code:
// Safer version
  int num = recv(sock, retBuffer, sizeof(retBuffer) - 1, 0);
  if(num == -1) 
    printf("error");
  else 
    retBuffer[num] = 0;

Last edited by paulsm4; 03-05-2010 at 12:43 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to eliminate DHCP etc. johnsfine Linux - Networking 2 08-12-2008 10:02 AM
C: convert char buffer received from socket to host endianess buffer kpachopoulos Programming 3 06-07-2007 05:06 PM
Eliminate touchpad aeruzcar Linux - Hardware 2 06-15-2006 02:12 PM
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-25-2006 11:57 PM
How to eliminate the spyware ? emailssent General 32 11-01-2004 09:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration