Skip to content

Commit dcb34b5

Browse files
brawerjoto
authored andcommitted
Make bzip2 unit tests pass on musl-based systems
Other than glibc, the musl standard C library does not check the validity of file descriptors passed to `fdopen()`, so the constructor of the (de)compressor does not throw an excption on musl-based systems. Rather, an exception is thrown later, when the opened file is being used. According to the musl team, the current musl behavior conforms to the POSIX specification, and application code should be made agnostic to whether or not the standard C library happens to validate file descriptors passed to `fdopen()`. For the discussion of `fdopen()` filedescriptor validation, see this thread on the musl mailing list: * https://www.openwall.com/lists/musl/2022/08/04/1 * https://www.openwall.com/lists/musl/2022/08/04/2 * https://www.openwall.com/lists/musl/2022/08/04/3 After this change, all libosmium tests (including those for bzip2) are passing on Alpine Linux, which uses musl as its standard C library. Fixes #353.
1 parent 73d3594 commit dcb34b5

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

test/t/io/test_bzip2.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77

88
#include <string>
99

10+
static void read_from_decompressor(int fd) {
11+
osmium::io::Bzip2Decompressor decomp{fd};
12+
decomp.read();
13+
decomp.close();
14+
}
15+
1016
TEST_CASE("Invalid file descriptor of bzip2-compressed file") {
11-
REQUIRE_THROWS_AS(osmium::io::Bzip2Decompressor{-1}, std::system_error);
17+
REQUIRE_THROWS(read_from_decompressor(-1));
1218
}
1319

1420
TEST_CASE("Non-open file descriptor of bzip2-compressed file") {
1521
// 12345 is just a random file descriptor that should not be open
16-
REQUIRE_THROWS_AS(osmium::io::Bzip2Decompressor{12345}, std::system_error);
22+
REQUIRE_THROWS(read_from_decompressor(12345));
1723
}
1824

1925
TEST_CASE("Empty bzip2-compressed file") {
@@ -93,13 +99,19 @@ TEST_CASE("Corrupted bzip2-compressed file") {
9399
REQUIRE(count == count_fds());
94100
}
95101

102+
static void write_to_compressor(int fd) {
103+
osmium::io::Bzip2Compressor comp{fd, osmium::io::fsync::yes};
104+
comp.write("foo");
105+
comp.close();
106+
}
107+
96108
TEST_CASE("Compressor: Invalid file descriptor for bzip2-compressed file") {
97-
REQUIRE_THROWS_AS(osmium::io::Bzip2Compressor(-1, osmium::io::fsync::yes), std::system_error);
109+
REQUIRE_THROWS(write_to_compressor(-1));
98110
}
99111

100112
TEST_CASE("Compressor: Non-open file descriptor for bzip2-compressed file") {
101113
// 12345 is just a random file descriptor that should not be open
102-
REQUIRE_THROWS_AS(osmium::io::Bzip2Compressor(12345, osmium::io::fsync::yes), std::system_error);
114+
REQUIRE_THROWS(write_to_compressor(12345));
103115
}
104116

105117
TEST_CASE("Write bzip2-compressed file with explicit close") {

0 commit comments

Comments
 (0)