Bug #12360

fwrite can loop forever on zero byte write

Added by Robert Mustacchi about 1 year ago. Updated about 1 year ago.

lib - userland libraries
Start date:
Due date:
% Done:


Estimated time:
Gerrit CR:


The different stdio write paths all have their own implementation of writing to the underlying object and the interim buffer. While working on the memory streams, if a write is beyond the end of a fixed buffer with fmemstream(3C), you are supposed to return a zero byte write. While most of the other places this occurs handle a zero-byte write correctly, fwrite() does not. This in turn leads to the code infinite looping. This probably has not been an issue as it is rare for write(2) to return zero. The solution is just to treat this like the other cases. While we should consolidate the underlying write implementations I didn't want to do that at this time since there was enough other stuff going on with the support of memory streams.


Updated by Robert Mustacchi about 1 year ago

This has been tested by exercising fmemopen(3C) based streams which triggered this behavior. This now brings the behavior in line with the other internal write paths in stdio. See 7092 for the full test output and testing behavior.


Updated by Electric Monk about 1 year ago

  • Status changed from New to Closed
  • % Done changed from 90 to 100

git commit cd62a92d4a964bfe61d35ba2301b69e65e22a509

commit  cd62a92d4a964bfe61d35ba2301b69e65e22a509
Author: Robert Mustacchi <>
Date:   2020-03-26T07:42:53.000Z

    7092 Want support for stdio memory streams
    12360 fwrite can loop forever on zero byte write
    12392 ftello64 doesn't handle ungetc() correctly when unbuffered
    Reviewed by: John Levon <>
    Reviewed by: Yuri Pankov <>
    Approved by: Dan McDonald <>

Also available in: Atom PDF