Skip to content

Commit d79c16d

Browse files
committed
fix: Return an error on writing partial bytes instead of crashing
1 parent 496acf5 commit d79c16d

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ links = [
77
]
88

99
[dependencies]
10-
gleam_stdlib = ">= 0.41.0 and < 2.0.0"
10+
gleam_stdlib = ">= 0.42.0 and < 2.0.0"
1111

1212
[dev-dependencies]
1313
gleeunit = ">= 1.2.0 and < 2.0.0"

manifest.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# You typically do not need to edit this file
33

44
packages = [
5-
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
6-
{ name = "gleam_stdlib", version = "0.41.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1B2F80CB1B66B027E3198A2FF71EF3F2F31DF89ED97AD606F25FD387A4C3C1EF" },
5+
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
6+
{ name = "gleam_stdlib", version = "0.47.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "3B22D46743C46498C8355365243327AC731ECD3959216344FA9CF9AD348620AC" },
77
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
88
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
99
]
1010

1111
[requirements]
12-
gleam_stdlib = { version = ">= 0.41.0 and < 2.0.0" }
12+
gleam_stdlib = { version = ">= 0.42.0 and < 2.0.0" }
1313
gleeunit = { version = ">= 1.2.0 and < 2.0.0" }
1414
simplifile = { version = ">= 2.0.1 and < 3.0.0" }

src/file_streams/file_stream.gleam

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ pub fn write_bytes(
274274
Error(file_stream_error.Enotsup),
275275
)
276276

277+
// Check that the bit array contains a whole number of bytes
278+
use <- bool.guard(
279+
bit_array.bit_size(bytes) % 8 != 0,
280+
Error(file_stream_error.Einval),
281+
)
282+
277283
case file_write(stream.io_device, bytes) {
278284
raw_result.Ok -> Ok(Nil)
279285
raw_result.Error(e) -> Error(e)

test/file_streams_test.gleam

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,17 @@ pub fn set_encoding_test() {
547547
simplifile.delete(tmp_file_name)
548548
|> should.equal(Ok(Nil))
549549
}
550+
551+
@target(erlang)
552+
pub fn write_partial_bytes_test() {
553+
let assert Ok(stream) = file_stream.open_write(tmp_file_name)
554+
555+
file_stream.write_bytes(stream, <<"A", 0:7>>)
556+
|> should.equal(Error(file_stream_error.Einval))
557+
558+
file_stream.close(stream)
559+
|> should.equal(Ok(Nil))
560+
561+
simplifile.delete(tmp_file_name)
562+
|> should.equal(Ok(Nil))
563+
}

0 commit comments

Comments
 (0)