Make BinaryStreamWriter::padToAlignment write blocks vs bytes.
authorStella Laurenzo <stellaraccident@gmail.com>
Mon, 2 May 2022 02:20:50 +0000 (19:20 -0700)
committerStella Laurenzo <stellaraccident@gmail.com>
Sun, 8 May 2022 00:37:18 +0000 (17:37 -0700)
commit6dedbcd5e96ff36ea7d38534068a2d3e4746a929
tree31f9bc24b5ae667380d5c42494bac2d2b1baaf93
parent6bbf51f3ed59ae37f0fec729f25af002111c9e74
Make BinaryStreamWriter::padToAlignment write blocks vs bytes.

While I think this is a performance improvement over the original, this actually fixes a correctness issue: For an appendable underlying stream, padToAlignment would fail if the additional padding would have caused the stream to grow since it was doing its own check on bounds. By deferring to the regular writeArray method this takes the same path as everything else, which does the correct bounds check in WritableBinaryStreamRef::checkOffsetForWrite (i.e. skips the extension check if BSF_Append is set). I had started to fix the existing bounds check in BinaryStreamWriter but deferred to this because it layered better and is more efficient/consistent.

It didn't look like this method was tested at all, so I added a unit test.

Differential Revision: https://reviews.llvm.org/D124746
llvm/lib/Support/BinaryStreamWriter.cpp
llvm/unittests/Support/BinaryStreamTest.cpp