1
0
mirror of https://github.com/Zygo/bees.git synced 2025-05-17 13:25:45 +02:00

fd: make the close method on IOHandle private

Fd's cache does not handle changes in the state of its IOHandle parameter.
If we allow:

	Fd f;
	f->close();

then Fd ends up caching a pointer to a closed Fd, and will become very
badly confused if a new Fd appears with the same int identifier.

Fix by removing the close method.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
Zygo Blaxell 2021-04-04 14:17:32 -04:00
parent 06062cfd96
commit 7cffad5fc3
2 changed files with 13 additions and 8 deletions

View File

@ -35,11 +35,11 @@ namespace crucible {
IOHandle& operator=(IOHandle &&) = delete; IOHandle& operator=(IOHandle &&) = delete;
IOHandle& operator=(const IOHandle &) = delete; IOHandle& operator=(const IOHandle &) = delete;
int m_fd; int m_fd;
void close();
public: public:
virtual ~IOHandle(); virtual ~IOHandle();
IOHandle(int fd = -1); IOHandle(int fd = -1);
int get_fd() const; int get_fd() const;
void close();
}; };
class Fd { class Fd {

View File

@ -37,7 +37,7 @@ test_basic_read()
char read_buf[test_string_len]; char read_buf[test_string_len];
read_or_die(f, read_buf); read_or_die(f, read_buf);
assert(!strncmp(read_buf, test_string, test_string_len)); assert(!strncmp(read_buf, test_string, test_string_len));
f->close(); f = Fd();
} }
static static
@ -262,24 +262,29 @@ static void test_map()
assert_is_closed(c, false); assert_is_closed(c, false);
} }
static void test_close_method() static void test_close()
{ {
Fd fd = open("fd.cc", O_RDONLY); Fd fd = open("fd.cc", O_RDONLY);
int i = fd; int i = fd;
assert_is_closed(i, false); assert_is_closed(i, false);
fd->close(); fd = Fd();
assert_is_closed(i, true); assert_is_closed(i, true);
} }
static void test_shared_close_method() static void test_shared_close()
{ {
Fd fd = open("fd.cc", O_RDONLY); Fd fd = open("fd.cc", O_RDONLY);
int i = fd; int i = fd;
Fd fd2 = fd; Fd fd2 = fd;
assert_is_closed(i, false); assert_is_closed(i, false);
assert_is_closed(fd2, false); assert_is_closed(fd2, false);
fd->close(); fd2 = Fd();
assert_is_closed(i, false);
assert_is_closed(fd, false);
assert_is_closed(fd2, true);
fd = Fd();
assert_is_closed(i, true); assert_is_closed(i, true);
assert_is_closed(fd, true);
assert_is_closed(fd2, true); assert_is_closed(fd2, true);
} }
@ -391,8 +396,8 @@ int main(int, const char **)
RUN_A_TEST(test_assign_int_close()); RUN_A_TEST(test_assign_int_close());
RUN_A_TEST(test_assign_int_close_2()); RUN_A_TEST(test_assign_int_close_2());
RUN_A_TEST(test_map()); RUN_A_TEST(test_map());
RUN_A_TEST(test_close_method()); RUN_A_TEST(test_close());
RUN_A_TEST(test_shared_close_method()); RUN_A_TEST(test_shared_close());
RUN_A_TEST(test_derived_resource_type()); RUN_A_TEST(test_derived_resource_type());
RUN_A_TEST(test_derived_map()); RUN_A_TEST(test_derived_map());
RUN_A_TEST(test_derived_cast()); RUN_A_TEST(test_derived_cast());