correct arithmetic adjustments

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-05-05 20:40:58 -04:00 committed by June
parent 16a98b0683
commit 321e197d8c
12 changed files with 24 additions and 53 deletions

View file

@ -340,13 +340,7 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
e.insert((Instant::now(), 1));
},
hash_map::Entry::Occupied(mut e) => {
*e.get_mut() = (
Instant::now(),
e.get()
.1
.checked_add(1)
.expect("bad_query_ratelimiter attempt/try count should not ever get this high"),
);
*e.get_mut() = (Instant::now(), e.get().1.saturating_add(1));
},
}
};

View file

@ -1395,13 +1395,7 @@ async fn validate_and_add_event_id(
e.insert((Instant::now(), 1));
},
Entry::Occupied(mut e) => {
*e.get_mut() = (
Instant::now(),
e.get()
.1
.checked_add(1)
.expect("bad_event_ratelimiter attempt/try count should not ever get this high"),
);
*e.get_mut() = (Instant::now(), e.get().1.saturating_add(1));
},
}
};

View file

@ -806,7 +806,9 @@ pub(crate) async fn upgrade_room_route(body: Ruma<upgrade_room::v3::Request>) ->
power_levels_event_content
.users_default
.checked_add(int!(1))
.expect("user power level should not be this high"),
.ok_or_else(|| {
Error::BadRequest(ErrorKind::BadJson, "users_default power levels event content is not valid")
})?,
);
power_levels_event_content.events_default = new_level;
power_levels_event_content.invite = new_level;

View file

@ -557,7 +557,7 @@ async fn handle_left_room(
left_state_events.push(pdu.to_sync_state_event());
i = i.saturating_add(1);
i = i.wrapping_add(1);
if i % 100 == 0 {
tokio::task::yield_now().await;
}
@ -705,11 +705,7 @@ async fn load_joined_room(
// Recalculate heroes (first 5 members)
let mut heroes = Vec::new();
if joined_member_count
.checked_add(invited_member_count)
.expect("joined/invite member count should not be this high")
<= 5
{
if joined_member_count.saturating_add(invited_member_count) <= 5 {
// Go through all PDUs and for each member event, check if the user is still
// joined or invited until we have 5 or we reach the end
@ -802,7 +798,7 @@ async fn load_joined_room(
};
state_events.push(pdu);
i = i.saturating_add(1);
i = i.wrapping_add(1);
if i % 100 == 0 {
tokio::task::yield_now().await;
}
@ -823,7 +819,7 @@ async fn load_joined_room(
}
state_events.push(pdu);
i = i.saturating_add(1);
i = i.wrapping_add(1);
if i % 100 == 0 {
tokio::task::yield_now().await;
}

View file

@ -25,7 +25,7 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
.parse_compressed_state_event(compressed)?;
result.insert(parsed.0, parsed.1);
i = i.saturating_add(1);
i = i.wrapping_add(1);
if i % 100 == 0 {
tokio::task::yield_now().await;
}
@ -63,7 +63,7 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
);
}
i = i.saturating_add(1);
i = i.wrapping_add(1);
if i % 100 == 0 {
tokio::task::yield_now().await;
}

View file

@ -284,11 +284,7 @@ fn count_to_id(room_id: &RoomId, count: PduCount, offset: u64, subtract: bool) -
pdu_id.extend_from_slice(&0_u64.to_be_bytes());
let num = u64::MAX.saturating_sub(x);
if subtract {
if num > 0 {
num.saturating_sub(offset)
} else {
num
}
num.saturating_sub(offset)
} else {
num.saturating_add(offset)
}

View file

@ -173,13 +173,11 @@ pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result
let mut current_sstatehash: Option<u64> = None;
let mut current_room = None;
let mut current_state = HashSet::new();
let mut counter: u32 = 0;
let mut handle_state = |current_sstatehash: u64,
current_room: &RoomId,
current_state: HashSet<_>,
last_roomstates: &mut HashMap<_, _>| {
counter = counter.saturating_add(1);
let handle_state = |current_sstatehash: u64,
current_room: &RoomId,
current_state: HashSet<_>,
last_roomstates: &mut HashMap<_, _>| {
let last_roomsstatehash = last_roomstates.get(current_room);
let states_parents = last_roomsstatehash.map_or_else(

View file

@ -402,19 +402,8 @@ impl KeyValueDatabase {
let sqlite_exists = path.join("conduit.db").exists();
let rocksdb_exists = path.join("IDENTITY").exists();
let mut count: u8 = 0;
if sqlite_exists {
count = count.saturating_add(1);
}
if rocksdb_exists {
count = count.saturating_add(1);
}
if count > 1 {
error!("Multiple databases at database_path detected");
return Ok(());
if sqlite_exists && rocksdb_exists {
return Err(Error::bad_config("Multiple databases at database_path detected."));
}
if sqlite_exists && config.database_backend != "sqlite" {

View file

@ -144,7 +144,7 @@ pub(crate) async fn delete_list(body: Vec<&str>) -> Result<RoomMessageEventConte
.drain(1..body.len().checked_sub(1).unwrap())
.collect::<Vec<_>>();
let mut mxc_deletion_count: u32 = 0;
let mut mxc_deletion_count: usize = 0;
for mxc in mxc_list {
debug!("Deleting MXC {mxc} in bulk");

View file

@ -22,7 +22,7 @@ pub(crate) async fn list(_body: Vec<&str>, page: Option<usize>) -> Result<RoomMe
let rooms = rooms
.into_iter()
.skip(page.saturating_sub(1) * PAGE_SIZE)
.skip(page.saturating_sub(1).saturating_mul(PAGE_SIZE))
.take(PAGE_SIZE)
.collect::<Vec<_>>();

View file

@ -39,7 +39,7 @@ pub(crate) async fn process(command: RoomDirectoryCommand, _body: Vec<&str>) ->
let rooms = rooms
.into_iter()
.skip(page.checked_sub(1).unwrap().checked_mul(PAGE_SIZE).unwrap())
.skip(page.saturating_sub(1).saturating_mul(PAGE_SIZE))
.take(PAGE_SIZE)
.collect::<Vec<_>>();

View file

@ -188,7 +188,9 @@ impl Service {
Ok(duration) => {
debug!("Parsed duration: {:?}", duration);
debug!("System time now: {:?}", SystemTime::now());
SystemTime::now().checked_sub(duration).unwrap()
SystemTime::now().checked_sub(duration).ok_or_else(|| {
Error::bad_database("Duration specified is not valid against the current system time")
})?
},
Err(e) => {
error!("Failed to parse user-specified time duration: {}", e);