Skip to content

Commit

Permalink
fix: Return an error on writing partial bytes instead of crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-viney committed Dec 11, 2024
1 parent 496acf5 commit d79c16d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ links = [
]

[dependencies]
gleam_stdlib = ">= 0.41.0 and < 2.0.0"
gleam_stdlib = ">= 0.42.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.2.0 and < 2.0.0"
Expand Down
6 changes: 3 additions & 3 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# You typically do not need to edit this file

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

[requirements]
gleam_stdlib = { version = ">= 0.41.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.42.0 and < 2.0.0" }
gleeunit = { version = ">= 1.2.0 and < 2.0.0" }
simplifile = { version = ">= 2.0.1 and < 3.0.0" }
6 changes: 6 additions & 0 deletions src/file_streams/file_stream.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ pub fn write_bytes(
Error(file_stream_error.Enotsup),
)

// Check that the bit array contains a whole number of bytes
use <- bool.guard(
bit_array.bit_size(bytes) % 8 != 0,
Error(file_stream_error.Einval),
)

case file_write(stream.io_device, bytes) {
raw_result.Ok -> Ok(Nil)
raw_result.Error(e) -> Error(e)
Expand Down
14 changes: 14 additions & 0 deletions test/file_streams_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,17 @@ pub fn set_encoding_test() {
simplifile.delete(tmp_file_name)
|> should.equal(Ok(Nil))
}

@target(erlang)
pub fn write_partial_bytes_test() {
let assert Ok(stream) = file_stream.open_write(tmp_file_name)

file_stream.write_bytes(stream, <<"A", 0:7>>)
|> should.equal(Error(file_stream_error.Einval))

file_stream.close(stream)
|> should.equal(Ok(Nil))

simplifile.delete(tmp_file_name)
|> should.equal(Ok(Nil))
}

0 comments on commit d79c16d

Please sign in to comment.