Thread: Ignore Return Value of write()

  1. #1
    Banned
    Join Date
    Jul 2022
    Posts
    112

    Ignore Return Value of write()

    The conventional way to use write(),
    is to verify that the return value matches the number of bytes.

    But what could possibly go wrong,
    if the return value is ignored and errno is used..

    errno = 0;
    while() write()
    if errno

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    errno is set only if write() returns -1.

    man 2 write

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Handling write() correctly is far more complex than that. It has partial writes and retryable errors.

    I consider assigning errno bad form. But that might be my own hang-up.

  4. #4
    Banned
    Join Date
    Jul 2022
    Posts
    112
    I consider assigning errno bad form.
    Is there a possibility that write() is successful and errno may still be set,
    errno is a global..
    Last edited by kodax; 11-11-2022 at 10:33 PM.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by kodax View Post
    Is there a possibility that write() is successful and errno may still be set,
    errno is a global..
    It is nuanced, and the 'code' you have provided in the original post is lacking. I can't compile it, I can't test it, I have to make assumptions and read your mind - as you so quickly point out, we here are not mind readers.

    Success and failure are ill-defined terms. If half of the data is written, and errno not set, is that success or failure?

    In general the write() function should act as the documentation says it will. It will only set errno if absolutely no data can be written.

    If you have an actual bit of code, then post it and we can comment.

    If you use errno without write() indicating an error then it is poor code.

    If you do not handle E_AGAIN and E_INTR properly then it is poor code.

    If you do not handle write() writing some, but not all, of the requested data then it is poor code.

  6. #6
    Banned
    Join Date
    Jul 2022
    Posts
    112
    If you use errno without write() indicating an error then it is poor code.
    Absolutely, it is poor code, it goes against the documentation,
    write() returns a value for a reason.

    The idea was to use write() without checking it's return value,
    and relying on errno instead, and true it is irregular.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Great, so we're all agreed then.
    Using write() without checking the return result is a dumb idea.
    Case closed then.
    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.

  8. #8
    Banned
    Join Date
    Jul 2022
    Posts
    112
    There's also a possibility that write() fails,
    and errno is 0.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Circular argument troll.

    Take a timeout.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can I write a function to return char * [5]
    By tester1234 in forum C Programming
    Replies: 17
    Last Post: 12-16-2015, 02:01 AM
  2. cin.ignore() in C
    By swgh in forum C Programming
    Replies: 10
    Last Post: 08-09-2007, 10:45 PM
  3. Console window waiting on cin.ignore() or cin.ignore(2)
    By The SharK in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2006, 04:17 PM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. need help with cin.get, or cin.ignore
    By yoyo in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 01:14 AM

Tags for this Thread