Skip to main content
edited body
Source Link
vnp
  • 57.3k
  • 4
  • 51
  • 140

I hope I am not overusing assert calls

You do. assert is a debugging instrument. If NDEBUG macro is defined, the calls to assert are removed at the compile time. This is a usual practice for the production code.

  • #define whaterror strerror(errno)

    I advise against it. The macro adds no value, and only obfuscates the code.

  • memset(malloc

    Keep in mind that malloc may fail.

  • memset(bytes == -1 ? chunk : chunk + bytes, 0, BUFFSIZE);

    Setting BUFFSIZE bytes to 0 writes beyond the allocated space. You need BUFFSIZE - bytes.

    On the other hand, clearing the entire tail of the chunk is not necessary. chunk[bytes] = 0; is enough.

  • !errno ?, besides being another obfuscation, is quite wrong. errno is a way to inspect errors, not to detect them. In fact, quoting man errno,

    Successful calls never set errno; once set, it remains until another error occurs.

    recv returning -1 is one and only indication of error.

  • Since the socket is not marked nonblocking, it may never return EAGAIN. On the other hand, some errors are irrecoverabletransient, and should not break the loop.

I hope I am not overusing assert calls

You do. assert is a debugging instrument. If NDEBUG macro is defined, the calls to assert are removed at the compile time. This is a usual practice for the production code.

  • #define whaterror strerror(errno)

    I advise against it. The macro adds no value, and only obfuscates the code.

  • memset(malloc

    Keep in mind that malloc may fail.

  • memset(bytes == -1 ? chunk : chunk + bytes, 0, BUFFSIZE);

    Setting BUFFSIZE bytes to 0 writes beyond the allocated space. You need BUFFSIZE - bytes.

    On the other hand, clearing the entire tail of the chunk is not necessary. chunk[bytes] = 0; is enough.

  • !errno ?, besides being another obfuscation, is quite wrong. errno is a way to inspect errors, not to detect them. In fact, quoting man errno,

    Successful calls never set errno; once set, it remains until another error occurs.

    recv returning -1 is one and only indication of error.

  • Since the socket is not marked nonblocking, it may never return EAGAIN. On the other hand, some errors are irrecoverable, and should break the loop.

I hope I am not overusing assert calls

You do. assert is a debugging instrument. If NDEBUG macro is defined, the calls to assert are removed at the compile time. This is a usual practice for the production code.

  • #define whaterror strerror(errno)

    I advise against it. The macro adds no value, and only obfuscates the code.

  • memset(malloc

    Keep in mind that malloc may fail.

  • memset(bytes == -1 ? chunk : chunk + bytes, 0, BUFFSIZE);

    Setting BUFFSIZE bytes to 0 writes beyond the allocated space. You need BUFFSIZE - bytes.

    On the other hand, clearing the entire tail of the chunk is not necessary. chunk[bytes] = 0; is enough.

  • !errno ?, besides being another obfuscation, is quite wrong. errno is a way to inspect errors, not to detect them. In fact, quoting man errno,

    Successful calls never set errno; once set, it remains until another error occurs.

    recv returning -1 is one and only indication of error.

  • Since the socket is not marked nonblocking, it may never return EAGAIN. On the other hand, some errors are transient, and should not break the loop.

deleted 121 characters in body
Source Link
vnp
  • 57.3k
  • 4
  • 51
  • 140

I hope I am not overusing assert calls

You do. assert is a debugging instrument. If NDEBUG macro is defined, the calls to assert are removed at the compile time. This is a usual practice for the production code.

  • #define whaterror strerror(errno)

    I advise against it. The macro adds no value, and only obfuscates the code.

  • memset(malloc

    Keep in mind that malloc may fail.

  • memset(bytes == -1 ? chunk : chunk + bytes, 0, BUFFSIZE);

    Setting BUFFSIZE bytes to 0 writes beyond the allocated space. You need BUFFSIZE - bytes.

    On the other hand, clearing the entire tail of the chunk is not necessary. chunk[bytes] = 0; is enough.

  • !errno ?, besides being another obfuscation, is quite wrong. errno is a way to reportinspect errors, not to detect them. In fact, quoting man errno,

    Successful calls never set errno; once set, it remains until another error occurs.

    recv returning -1 is one and only indication of error.

  • Since the socket is not marked nonblocking, it may never return EAGAIN. On the other hand, there are other transientsome errors, which do not necessarily require exit. For example are irrecoverable, a system may recover from ENOBUFS; EINTR alsoand should not unconditionally stopbreak the serverloop.

I hope I am not overusing assert calls

You do. assert is a debugging instrument. If NDEBUG macro is defined, the calls to assert are removed at the compile time. This is a usual practice for the production code.

  • #define whaterror strerror(errno)

    I advise against it. The macro adds no value, and only obfuscates the code.

  • memset(malloc

    Keep in mind that malloc may fail.

  • memset(bytes == -1 ? chunk : chunk + bytes, 0, BUFFSIZE);

    Setting BUFFSIZE bytes to 0 writes beyond the allocated space. You need BUFFSIZE - bytes.

    On the other hand, clearing the entire tail of the chunk is not necessary. chunk[bytes] = 0; is enough.

  • !errno ?, besides being another obfuscation, is quite wrong. errno is a way to report errors, not to detect them. In fact, quoting man errno,

    Successful calls never set errno; once set, it remains until another error occurs.

    recv returning -1 is one and only indication of error.

  • Since the socket is not marked nonblocking, it may never return EAGAIN. On the other hand, there are other transient errors, which do not necessarily require exit. For example, a system may recover from ENOBUFS; EINTR also should not unconditionally stop the server.

I hope I am not overusing assert calls

You do. assert is a debugging instrument. If NDEBUG macro is defined, the calls to assert are removed at the compile time. This is a usual practice for the production code.

  • #define whaterror strerror(errno)

    I advise against it. The macro adds no value, and only obfuscates the code.

  • memset(malloc

    Keep in mind that malloc may fail.

  • memset(bytes == -1 ? chunk : chunk + bytes, 0, BUFFSIZE);

    Setting BUFFSIZE bytes to 0 writes beyond the allocated space. You need BUFFSIZE - bytes.

    On the other hand, clearing the entire tail of the chunk is not necessary. chunk[bytes] = 0; is enough.

  • !errno ?, besides being another obfuscation, is quite wrong. errno is a way to inspect errors, not to detect them. In fact, quoting man errno,

    Successful calls never set errno; once set, it remains until another error occurs.

    recv returning -1 is one and only indication of error.

  • Since the socket is not marked nonblocking, it may never return EAGAIN. On the other hand, some errors are irrecoverable, and should break the loop.

Source Link
vnp
  • 57.3k
  • 4
  • 51
  • 140

I hope I am not overusing assert calls

You do. assert is a debugging instrument. If NDEBUG macro is defined, the calls to assert are removed at the compile time. This is a usual practice for the production code.

  • #define whaterror strerror(errno)

    I advise against it. The macro adds no value, and only obfuscates the code.

  • memset(malloc

    Keep in mind that malloc may fail.

  • memset(bytes == -1 ? chunk : chunk + bytes, 0, BUFFSIZE);

    Setting BUFFSIZE bytes to 0 writes beyond the allocated space. You need BUFFSIZE - bytes.

    On the other hand, clearing the entire tail of the chunk is not necessary. chunk[bytes] = 0; is enough.

  • !errno ?, besides being another obfuscation, is quite wrong. errno is a way to report errors, not to detect them. In fact, quoting man errno,

    Successful calls never set errno; once set, it remains until another error occurs.

    recv returning -1 is one and only indication of error.

  • Since the socket is not marked nonblocking, it may never return EAGAIN. On the other hand, there are other transient errors, which do not necessarily require exit. For example, a system may recover from ENOBUFS; EINTR also should not unconditionally stop the server.