Project

General

Profile

Bug #12360

fwrite can loop forever on zero byte write

Added by Robert Mustacchi 9 months ago. Updated 8 months ago.

Status:
Closed
Priority:
Normal
Category:
lib - userland libraries
Start date:
Due date:
% Done:

100%

Estimated time:
Difficulty:
Medium
Tags:
Gerrit CR:

Description

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.

#1

Updated by Robert Mustacchi 8 months 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.

#2

Updated by Electric Monk 8 months ago

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

git commit cd62a92d4a964bfe61d35ba2301b69e65e22a509

commit  cd62a92d4a964bfe61d35ba2301b69e65e22a509
Author: Robert Mustacchi <rm@fingolfin.org>
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 <john.levon@joyent.com>
    Reviewed by: Yuri Pankov <ypankov@fastmail.com>
    Approved by: Dan McDonald <danmcd@joyent.com>

Also available in: Atom PDF