Skip to content

Commit

Permalink
Fix null dereference when passing empty slice to Base64.encode (#12377)
Browse files Browse the repository at this point in the history
  • Loading branch information
dscottboggs authored and beta-ziliani committed Sep 7, 2022
1 parent 5c08b4a commit b1e3a86
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spec/std/base64_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ describe "Base64" do
assert_prints base64_strict_encode(slice), "AQEBAQE="
end

it "encodes empty slice" do
slice = Bytes.empty
assert_prints base64_encode(slice), ""
assert_prints base64_strict_encode(slice), ""
end

it "encodes static array" do
array = uninitialized StaticArray(UInt8, 5)
(0...5).each { |i| array[i] = 1_u8 }
Expand Down
1 change: 1 addition & 0 deletions src/base64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ module Base64
bytes = chars.to_unsafe
size = data.size
cstr = data.to_unsafe
return if cstr.null? || size == 0
endcstr = cstr + size - size % 3 - 3

# process bunch of full triples
Expand Down

0 comments on commit b1e3a86

Please sign in to comment.