This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Fix `std::out_of_range` thrown from `basic_stringbuf::str() &&`
ClosedPublic

Authored by pfusik on Aug 1 2023, 3:17 AM.

Diff Detail

Event Timeline

pfusik created this revision.Aug 1 2023, 3:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 1 2023, 3:17 AM
pfusik requested review of this revision.Aug 1 2023, 3:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 1 2023, 3:17 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript

Fixes a bug reported by @leonardchan at https://reviews.llvm.org/D155276
In the bug scenario, __view.data() == nullptr. The offset is validated by the string constructor/assign even if count == 0.

I just fixed the CI can you rebase this patch on main?

Mordante accepted this revision.Aug 1 2023, 8:58 AM

Thanks for the quick fix! LGTM modulo a comment.

After you land the patch, can you put it up for backporting to LLVM-17?
(The instructions are here https://www.llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches)

libcxx/include/sstream
405

Depending on the order of evaluation __view.data() - __str_.data() may be used after __str_ has been moved. Lets determine the size before assign. This is pre-existing.

Whether this is save depends on the order in which the function arguments are evaluated. Clang and GCC use a different order.

This revision is now accepted and ready to land.Aug 1 2023, 8:58 AM
pfusik marked an inline comment as done.Aug 1 2023, 9:02 AM
pfusik added inline comments.
libcxx/include/sstream
405

I was also having this concern for a moment. :) But std::move is just a cast that does not modify its argument. The actual move is inside assign.

pfusik updated this revision to Diff 546095.Aug 1 2023, 9:33 AM
pfusik marked an inline comment as done.

Rebased.

Mordante added inline comments.Aug 1 2023, 10:43 AM
libcxx/include/sstream
405

Good point, you're right. It only does a cast and nothing else.

philnik added inline comments.
libcxx/include/sstream
405

While that is true, I think moving the calculation out of the function call doesn't hurt. This would be a real bug if assign took the string by value, which makes this really subtile and definitely worthy of a comment if we were to keep it this way.

pfusik updated this revision to Diff 546158.Aug 1 2023, 11:22 AM
pfusik marked an inline comment as done.

Refactor a function argument to a variable (NFC).

philnik accepted this revision.Aug 1 2023, 11:23 AM
pfusik updated this revision to Diff 546322.Aug 1 2023, 10:34 PM

Rebased.

This revision was landed with ongoing or failed builds.Aug 2 2023, 7:27 AM
This revision was automatically updated to reflect the committed changes.
pfusik added a comment.Aug 2 2023, 8:11 AM

After you land the patch, can you put it up for backporting to LLVM-17?
(The instructions are here https://www.llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches)

https://github.com/llvm/llvm-project/issues/64346