Skip to content

Commit

Permalink
remove some slice indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Apr 7, 2024
1 parent 19486e0 commit c3e58cd
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/consthash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ impl ConstFnvHash {

let mut i = 0;
while i < bytes.len() {
// can't use better method in const fns
#[allow(clippy::indexing_slicing)]
let byte = bytes[i];
hash ^= byte as u64;
hash = hash.wrapping_mul(0x100000001b3);
Expand Down
8 changes: 4 additions & 4 deletions src/demo/lzss.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::convert::TryInto;

pub fn decompress(input: &[u8], output: &mut Vec<u8>) {
decompress_(input, output);
}

/// inner fn that returns an option so we can use ? for short circuiting return
fn decompress_(input: &[u8], output: &mut Vec<u8>) -> Option<()> {
let target_len = u32::from_le_bytes(input[0..4].try_into().unwrap()) as usize;
let mut len_bytes = [0; 4];
len_bytes.copy_from_slice(input.get(0..4)?);
let target_len = u32::from_le_bytes(len_bytes) as usize;

let mut read_pos = 4;
let mut read_byte = move || {
Expand Down Expand Up @@ -35,7 +35,7 @@ fn decompress_(input: &[u8], output: &mut Vec<u8>) -> Option<()> {
let start = output.len() - pos - 1;
// can't do extend_from_within since it start + count can be larger than output.len
for i in 0..count {
output.push(output[start + i]);
output.push(*output.get(start + i)?);
}
} else {
output.push(read_byte()?);
Expand Down
5 changes: 4 additions & 1 deletion src/demo/message/packetentities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,15 @@ impl PacketEntity {
}

pub fn get_baseline_props<'a>(&self, parser_state: &'a ParserState) -> Cow<'a, [SendProp]> {
let Some(send_table) = parser_state.send_tables.get(usize::from(self.server_class)) else {
return Cow::default();
};
parser_state
.get_baseline(
self.baseline_index,
self.entity_index,
self.server_class,
&parser_state.send_tables[usize::from(self.server_class)],
send_table,
self.delta.is_some(),
)
.unwrap_or_default()
Expand Down
8 changes: 7 additions & 1 deletion src/demo/message/stringtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,13 @@ fn write_table_entry(
if let Some((history_index, history_count)) = history_item {
history_index.write_sized(stream, 5)?;
history_count.write_sized(stream, 5)?;
let diff_bytes = &text.as_bytes()[history_count..];
let diff_bytes =
text.as_bytes()
.get(history_count..)
.ok_or(BitError::IndexOutOfBounds {
pos: history_count,
size: text.len(),
})?;
stream.write_bytes(diff_bytes)?;
0u8.write(stream)?; // writing the string as bytes doesn't add the null terminator
} else {
Expand Down
12 changes: 4 additions & 8 deletions src/demo/message/tempentities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,10 @@ impl ParseBitSkip<'_> for TempEntitiesMessage {

impl Encode for TempEntitiesMessage {
fn encode(&self, stream: &mut BitWriteStream<LittleEndian>, state: &ParserState) -> Result<()> {
let count = if self.events.len() == 1 {
if self.events[0].reliable {
0
} else {
1
}
} else {
self.events.len() as u8
let count = match (self.events.len(), self.events.first()) {
(1, Some(event)) if event.reliable => 0,
(1, _) => 1,
(len, _) => len as u8,
};
count.write(stream)?;

Expand Down
1 change: 1 addition & 0 deletions src/demo/packet/datatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ impl ParseSendTable {
// sort often changed props before the others
let mut start = 0;
for i in 0..flat.len() {
#[allow(clippy::indexing_slicing)]
if flat[i].parse_definition.changes_often() {
if i != start {
flat.swap(i, start);
Expand Down
33 changes: 14 additions & 19 deletions src/demo/parser/analyser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use serde::de::Error;
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
use std::collections::{BTreeMap, HashMap};
use std::convert::TryFrom;
use std::ops::{Index, IndexMut};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ChatMessage {
Expand Down Expand Up @@ -169,6 +168,18 @@ impl ClassList {
classes.sort_by(|a, b| a.1.cmp(&b.1).reverse());
classes.into_iter()
}

pub fn get(&self, class: Class) -> u8 {
// class number is always in bounds
#[allow(clippy::indexing_slicing)]
self.0[class as u8 as usize]
}

pub fn get_mut(&mut self, class: Class) -> &mut u8 {
// class number is always in bounds
#[allow(clippy::indexing_slicing)]
&mut self.0[class as u8 as usize]
}
}

#[test]
Expand All @@ -180,22 +191,6 @@ fn test_classlist_sorted() {
)
}

impl Index<Class> for ClassList {
type Output = u8;

#[cfg_attr(feature = "no-panic", no_panic::no_panic)]
fn index(&self, class: Class) -> &Self::Output {
&self.0[class as u8 as usize]
}
}

impl IndexMut<Class> for ClassList {
#[cfg_attr(feature = "no-panic", no_panic::no_panic)]
fn index_mut(&mut self, class: Class) -> &mut Self::Output {
&mut self.0[class as u8 as usize]
}
}

impl Serialize for ClassList {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -218,7 +213,7 @@ impl From<HashMap<Class, u8>> for ClassList {
let mut classes = ClassList::default();

for (class, count) in map.into_iter() {
classes[class] = count;
*classes.get_mut(class) = count;
}

classes
Expand Down Expand Up @@ -505,7 +500,7 @@ impl Analyser {
GameEvent::PlayerSpawn(event) => {
let spawn = Spawn::from_event(event, tick);
if let Some(user_state) = self.state.users.get_mut(&spawn.user) {
user_state.classes[spawn.class] += 1;
*user_state.classes.get_mut(spawn.class) += 1;
user_state.team = spawn.team;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/demo/parser/gamestateanalyser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ impl GameState {
}
};

#[allow(clippy::indexing_slicing)]
&mut self.players[index]
}
pub fn get_or_create_building(
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#![cfg_attr(not(test), deny(clippy::unwrap_used))]
#![cfg_attr(not(test), deny(clippy::expect_used))]
#![cfg_attr(not(test), deny(clippy::indexing_slicing))]
#![cfg_attr(not(test), deny(clippy::panic))]

pub use bitbuffer::Result as ReadResult;

pub use crate::demo::{
Expand Down

0 comments on commit c3e58cd

Please sign in to comment.