Debian Codegraph @

libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // <fstream>
10
11 // basic_filebuf<charT,traits>* close();
12
13 // This test closes an fd that belongs to a std::filebuf, and Bionic's fdsan
14 // detects this and aborts the process, starting in Android R (API 30).
15 // See D137129.
16 // XFAIL: LIBCXX-ANDROID-FIXME && !android-device-api={{2[1-9]}}
17
18 #include <fstream>
19 #include <cassert>
20 #if defined(__unix__)
21 #include <fcntl.h>
22 #include <unistd.h>
23 #endif
24 #include "test_macros.h"
25 #include "platform_support.h"
26
27 int main(int, char**)
28 {
29 std::string temp = get_temp_file_name();
30 {
31 std::filebuf f;
32 assert(!f.is_open());
33 assert(f.open(temp.c_str(), std::ios_base::out) != 0);
34 assert(f.is_open());
35 assert(f.close() != nullptr);
36 assert(!f.is_open());
37 assert(f.close() == nullptr);
38 assert(!f.is_open());
39 }
40 #if defined(__unix__)
41 {
42 std::filebuf f;
43 assert(!f.is_open());
44 // Use open directly to get the file descriptor.
45 int fd = open(temp.c_str(), O_RDWR);
46 assert(fd >= 0);
47 // Use the internal method to create filebuf from the file descriptor.
48 assert(f.__open(fd, std::ios_base::out) != 0);
49 assert(f.is_open());
50 // Close the file descriptor directly to force filebuf::close to fail.
51 assert(close(fd) == 0);
52 // Ensure that filebuf::close handles the failure.
53 assert(f.close() == nullptr);
54 assert(!f.is_open());
55 assert(f.close() == nullptr);
56 }
57 #endif
58 std::remove(temp.c_str());
59
60 return 0;
61 }