fix(webapi/register): fix shift overflow

This would make challenge always fail if difficulty is a multiple of 8.
This commit is contained in:
oxalica 2024-09-19 06:43:17 -04:00
parent 5afe6af11a
commit ec7f428519

View file

@ -195,7 +195,8 @@ pub async fn user_register(
let hash = &hash[..]; let hash = &hash[..];
// `difficulty` is u8 so it must be < 256 // `difficulty` is u8 so it must be < 256
let (bytes, bits) = (expect_bits as usize / 8, expect_bits as usize % 8); let (bytes, bits) = (expect_bits as usize / 8, expect_bits as usize % 8);
let ok = hash[..bytes].iter().all(|&b| b == 0) && hash[bytes] >> (8 - bits) == 0; // NB. Shift by 8 would overflow and wrap around for u8. Convert it to u32 first.
let ok = hash[..bytes].iter().all(|&b| b == 0) && (hash[bytes] as u32) >> (8 - bits) == 0;
if !ok { if !ok {
return Err(error_response!( return Err(error_response!(
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,