Thread: info on memset and recv

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > I have to say that vart and Salem are both way, way, way off base when they claim the point is to cover a bug or because the use of recv is "incorrect".

    Example

    Code:
    memset( buff, 0, sizeof buff )
    recv( sock, buff, sizeof buff, 0 );
    If recv() actually FILLS the buffer, then two things
    a) the memset was a complete and utter waste of time,
    b) the resulting buffer STILL DOES NOT have a \0, nor does it have room for one either.

    In other words, a total loss.

    Consider
    Code:
    // drop the -1 if you're not interested in making a string
    n = recv( sock, buff, sizeof buff - 1, 0 );  
    if ( n > 0 ) {
      buff[n] = '\0';  // if you really want a \0 string
      // do stuff with 'n' bytes
    }
    Done this way, the need for memset hokus-pokus vanishes, the buffer is correctly \0 terminated in a single instruction (or two, but certainly much more efficiently than memset), if that is what you want to do.
    In any event, the real size of the received data is available to make sure you can do the right thing.


    > My preference would be to add or at least check for the null terminator after the recv, which seems more optimal.
    Who said that you were going to get a \0 from recv()
    Who said that you were going to receive it in a timely manner, even if it was sent?

    It's quite simple. Use the return result of the function properly, and you're not going to go wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    Use the return result of the function properly, and you're not going to go wrong.
    Sure, and that's the byte where I would place the zero.

    However, I still can't say that it's wrong to use memset there. You just made up a ridiculous example:
    Code:
    memset( buff, 0, sizeof buff )
    recv( sock, buff, sizeof buff, 0 );
    The error here IS NOT MEMSET, it's that the recv parameter should be "sizeof buff" minus 1.

    Then you reinterate two points that I made explicitly, as if I had not accounted for them, which I did -- explicitly:
    Quote Originally Posted by Salem
    Who said that you were going to get a \0 from recv()
    Who said that you were going to receive it in a timely manner, even if it was sent?
    Quote Originally Posted by mk27
    sometimes with networking you cannot be held responsible for the string you are receiving, so you may want to make sure it is null terminated if you need it to be. Also, you are not guaranteed to "recv" the entire string, which means if you "properly" presume it will be there, it may not.
    With your level of experience, etc, I don't think you have any excuses for failing to read properly, so either do so or stop overtly trying to confuse the OP with your tish.
    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

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    The error here IS NOT MEMSET, it's that the recv parameter should be "sizeof buff" minus 1.
    It's not an error to do this:
    Code:
    #define THISROCKS { \
        size_t thisrocks = 1; \
        while( thisrocks++ ); \
        }
    
    ...
    THISROCKS
    // drop the -1 if you're not interested in making a string
    n = recv( sock, buff, sizeof buff - 1, 0 );  
    THISROCKS
    if ( n > 0 ) {
    THISROCKS
      buff[n] = '\0';  // if you really want a \0 string
      // do stuff with 'n' bytes
    }
    But it doesn't make it good thing to do either.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed