-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc++] Ensure that we restore invariants in basic_filebuf::overflow #147389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1906af8
[libc++] Ensure that we restore invariants in basic_filebuf::overflow
ldionne ae2baa9
Add test
ldionne 78a9535
Only run the new test on Apple
ldionne ade9511
Merge branch 'main' into review/fix-basic_filebuf
ldionne 966801d
Try to work around Windows error
ldionne 5daa9f3
Is there a weird macro named __failure on Windows?
ldionne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../test/std/input.output/file.streams/fstreams/filebuf.virtuals/overflow.writefail.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: no-filesystem | ||
|
||
// setrlimit(RLIMIT_FSIZE) seems to only work as intended on Apple platforms | ||
// REQUIRES: target={{.+}}-apple-{{.+}} | ||
philnik777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// <fstream> | ||
|
||
// Make sure that we properly handle the case where we try to write content to a file | ||
// but we fail to do so because std::fwrite fails. | ||
|
||
#include <cassert> | ||
#include <csignal> | ||
#include <cstddef> | ||
#include <fstream> | ||
#include <string> | ||
|
||
#include "platform_support.h" | ||
#include "test_macros.h" | ||
|
||
#if __has_include(<sys/resource.h>) | ||
# include <sys/resource.h> | ||
void limit_file_size_to(std::size_t bytes) { | ||
rlimit lim = {bytes, bytes}; | ||
assert(setrlimit(RLIMIT_FSIZE, &lim) == 0); | ||
|
||
std::signal(SIGXFSZ, [](int) {}); // ignore SIGXFSZ to ensure std::fwrite fails | ||
} | ||
#else | ||
# error No known way to limit the amount of filesystem space available | ||
#endif | ||
|
||
template <class CharT> | ||
void test() { | ||
std::string temp = get_temp_file_name(); | ||
std::basic_filebuf<CharT> fbuf; | ||
assert(fbuf.open(temp, std::ios::out | std::ios::trunc)); | ||
|
||
std::size_t const limit = 100000; | ||
limit_file_size_to(limit); | ||
|
||
std::basic_string<CharT> large_block(limit / 10, CharT(42)); | ||
|
||
std::streamsize ret; | ||
std::size_t bytes_written = 0; | ||
while ((ret = fbuf.sputn(large_block.data(), large_block.size())) != 0) { | ||
bytes_written += ret; | ||
|
||
// In theory, it's possible for an implementation to allow writing arbitrarily more bytes than | ||
// set by setrlimit, but in practice if we bust 100x our limit, something else is wrong with the | ||
// test and we'd end up looping forever. | ||
assert(bytes_written < 100 * limit); | ||
} | ||
|
||
fbuf.close(); | ||
} | ||
|
||
int main(int, char**) { | ||
test<char>(); | ||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
test<wchar_t>(); | ||
#endif | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC you're currently only testing this path. We should also test the
fwrite
calls in theelse
path (i.e. have a locale that's not__always_noconv_
and have that return the different possible states).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The facet for
wchar_t
returnsalways_noconv() == false
, so we're also testing below. We could try to hit every branch in theif
but I think there is diminishing returns.