SecureBuffer: raise when out of memory

This commit is contained in:
Didactic Drunk 2021-06-02 20:25:28 -07:00
parent fb77719d51
commit 35fcda70aa
2 changed files with 11 additions and 4 deletions

View File

@ -43,8 +43,10 @@ describe Sodium::SecureBuffer do
buf2 = buf.dup
buf2.@state.should eq Sodium::SecureBuffer::State::Readwrite
buf[0] = 0_u8
buf2[0] = 0_u8
buf[0] = 1_u8
buf.to_slice.hexstring.should_not eq buf2.to_slice.hexstring
buf2[0] = 1_u8
buf.to_slice.hexstring.should eq buf2.to_slice.hexstring
end
it "transitions correctly" do

View File

@ -13,6 +13,10 @@ module Sodium
class InvalidStateTransition < Error
end
# Check RLIMIT_MEMLOCK if you receive this
class OutOfMemory < Error
end
end
enum State
@ -31,6 +35,7 @@ module Sodium
def initialize(@bytesize : Int32)
@ptr = LibSodium.sodium_malloc @bytesize
raise Error::OutOfMemory.new if @ptr.null?
end
# Returns a **readonly** random SecureBuffer.
@ -115,8 +120,8 @@ module Sodium
end
end
# Temporarily make buffer readonly within the block returning to the prior state on exit.
# WARNING: Not thread safe unless this object is readonly or readwrite
# Temporarily make buffer readwrite within the block returning to the prior state on exit.
# WARNING: Not thread safe unless this object is **readwrite**
def readwrite
with_state State::Readwrite do
yield