Skip to content

Layout: a failed Save(string) destroys the file that was already there #508

Closed

Description

@Tim81

Document.Save(string) and Document.SaveAsync(string) open the target file before the layout
runs. A layout failure therefore leaves a zero-byte file in place of whatever the path held.

Measured

a good save wrote      : exists=True size=1535
the failing save threw : InvalidOperationException
the file afterwards    : exists=True size=0
SaveAsync before/after : 1535 -> 0

The failing document is one with a running band of Height = double.NaN, which is refused during
layout. Any of the layout refusals does it: a non-finite font size, a ColSpan below one, a
non-finite cell padding, margins that leave no content area.

The stream overloads behave correctly by comparison: Save(Stream) throws with nothing written,
and the caller still owns the stream.

Why this matters more than a failed save usually does

The pattern it breaks is the ordinary one. A caller regenerates a report over its previous output,
the input has one bad value, and the previous report is gone. Nothing in the exception says a file
was destroyed, and the exception is about layout, so the obvious reading is that the save did not
happen.

It is also the case a caller cannot defend against without knowing this, since the failure is not
detectable before the save: the layout is what finds the bad value, and the layout runs inside the
save.

Scope

  • Lay out first, then open the file. The renderer already produces the whole document before any
    byte is written, so the ordering is free.
  • Or write to a temporary file beside the target and move it into place on success, which also
    makes the save atomic against a crash. That is the stronger fix and worth considering, since a
    half-written PDF from a killed process has the same shape as this.
  • A test per string overload asserting that a pre-existing file survives a failed save byte for
    byte, using one of the layout refusals to trigger it.
  • Check the signing path, which has its own save, for the same ordering.

Not in scope

The stream overloads. They already leave the caller's stream alone in the sense that matters: they
write nothing. A caller who passes a FileStream they opened with FileMode.Create has made that
choice themselves.

Found by the second review lens on #503, while checking a documentation claim that no file is
written when a band height is non-finite. No file is, but the file that was there is gone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions