Skip to content

Commit

Permalink
Set String encodings in SecureRandom trampoline
Browse files Browse the repository at this point in the history
All `String`s returned by `SecureRandom` methods are either ASCII or
Binary, so set the encoding to reflect this.

This means we now pass some additional tests in ruby/spec since the
random bytes we are returning now properly yield their length as their
bytesize. Prior to #1222, `String#length` always assumed UTF-8, which
means if the binary content returned by `SecureRandom` happened to have
a multi-byte UTF-8 character, the `length` would be shorter than
expected.
  • Loading branch information
lopopolo committed Nov 26, 2021
1 parent 4f583ca commit fac36b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
20 changes: 13 additions & 7 deletions artichoke-backend/src/extn/stdlib/securerandom/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub fn alphanumeric(interp: &mut Artichoke, len: Option<Value>) -> Result<Value,
} else {
securerandom::alphanumeric(None)?
};
interp.try_convert_mut(alpha)
let alpha = spinoso_string::String::ascii(alpha);
spinoso_string::String::alloc_value(alpha, interp)
}

#[inline]
Expand All @@ -23,7 +24,8 @@ pub fn base64(interp: &mut Artichoke, len: Option<Value>) -> Result<Value, Error
} else {
securerandom::base64(None)?
};
interp.try_convert_mut(base64)
let base64 = spinoso_string::String::ascii(base64.into_bytes());
spinoso_string::String::alloc_value(base64, interp)
}

#[inline]
Expand All @@ -44,7 +46,8 @@ pub fn urlsafe_base64(interp: &mut Artichoke, len: Option<Value>, padding: Optio
} else {
securerandom::urlsafe_base64(None, padding)?
};
interp.try_convert_mut(base64)
let base64 = spinoso_string::String::ascii(base64.into_bytes());
spinoso_string::String::alloc_value(base64, interp)
}

#[inline]
Expand All @@ -55,7 +58,8 @@ pub fn hex(interp: &mut Artichoke, len: Option<Value>) -> Result<Value, Error> {
} else {
securerandom::hex(None)?
};
interp.try_convert_mut(hex)
let hex = spinoso_string::String::ascii(hex.into_bytes());
spinoso_string::String::alloc_value(hex, interp)
}

#[inline]
Expand All @@ -70,18 +74,20 @@ pub fn random_bytes(interp: &mut Artichoke, len: Option<Value>) -> Result<Value,
} else {
securerandom::random_bytes(None)?
};
interp.try_convert_mut(bytes)
let bytes = spinoso_string::String::binary(bytes);
spinoso_string::String::alloc_value(bytes, interp)
}

#[inline]
pub fn random_number(interp: &mut Artichoke, max: Option<Value>) -> Result<Value, Error> {
let max = interp.try_convert_mut(max)?;
let num = securerandom::random_number(max)?;
Ok(interp.convert_mut(num))
interp.try_convert_mut(num)
}

#[inline]
pub fn uuid(interp: &mut Artichoke) -> Result<Value, Error> {
let uuid = securerandom::uuid()?;
interp.try_convert_mut(uuid)
let uuid = spinoso_string::String::ascii(uuid.into_bytes());
spinoso_string::String::alloc_value(uuid, interp)
}
2 changes: 0 additions & 2 deletions spec-runner/all-library-specs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ include = "all"
[specs.library.securerandom]
include = "all"
skip = [
# specs require ASCII-8BIT / BINARY encoding for `String`s
"random_bytes",
# missing support for Bignum and Range arguments
"random_number",
]
Expand Down
2 changes: 0 additions & 2 deletions spec-runner/enforced-specs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ include = "all"
[specs.library.securerandom]
include = "all"
skip = [
# specs require ASCII-8BIT / BINARY encoding for `String`s
"random_bytes",
# missing support for Bignum and Range arguments
"random_number",
]
Expand Down

0 comments on commit fac36b9

Please sign in to comment.