GeezerVison is showing github.com — refactor: avoid slice indexing in general (#872) · rpgp/rpgp@33445c4
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/armor/reader.rs +9-3Lines changed: 9 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -300,7 +300,7 @@ fn read_checksum(input: &[u8]) -> std::io::Result<u64> { | 300 | 300 | let mut buf = [0; 4]; | 301 | 301 | let mut i = checksum.len(); | 302 | 302 | for a in checksum.iter().rev() { | 303 | | - buf[i] = *a; | | 303 | + *buf.get_mut(i).ok_or(io::ErrorKind::InvalidData)? = *a; | 304 | 304 | i -= 1; | 305 | 305 | } | 306 | 306 |
| @@ -546,7 +546,10 @@ impl<R: BufRead> Dearmor<R> { | 546 | 546 | if let Some(mut crc) = self.crc { | 547 | 547 | if size > 0 { | 548 | 548 | // update the hash | 549 | | - crc.write(&into[0..size]); | | 549 | + let data = into | | 550 | + .get(0..size) | | 551 | + .ok_or_else(|| io::Error::other("read more than the buffer holds"))?; | | 552 | + crc.write(data); | 550 | 553 | } | 551 | 554 | } | 552 | 555 |
| @@ -614,7 +617,10 @@ impl<R: BufRead> Read for Dearmor<R> { | 614 | 617 | self.current_part = Part::Body(Base64Decoder::new(Base64Reader::new(b))); | 615 | 618 | } | 616 | 619 | Part::Body(mut b) => { | 617 | | - let last_read = self.read_body(&mut into[read..], &mut b)?; | | 620 | + let dst = into | | 621 | + .get_mut(read..) | | 622 | + .ok_or_else(|| io::Error::other("read more than the buffer holds"))?; | | 623 | + let last_read = self.read_body(dst, &mut b)?; | 618 | 624 | if last_read == 0 && read < into.len() { | 619 | 625 | // we are done with the body | 620 | 626 | let (b, buf) = b.into_inner_with_buffer(); | |
Collapse file Copy file name to clipboardExpand all lines: src/armor/writer.rs +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -134,7 +134,11 @@ impl<W: std::io::Write> std::io::Write for Base64Encoder<W> { | 134 | 134 | while !buf.is_empty() { | 135 | 135 | match self.0.write(buf) { | 136 | 136 | Ok(0) => {} | 137 | | - Ok(n) => buf = &buf[n..], | | 137 | + Ok(n) => { | | 138 | + buf = buf.get(n..).ok_or_else(|| { | | 139 | + std::io::Error::other("writer reported more bytes than it was given") | | 140 | + })? | | 141 | + } | 138 | 142 | Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {} | 139 | 143 | Err(e) => return Err(e), | 140 | 144 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/base64/decoder.rs +15-6Lines changed: 15 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,23 +67,32 @@ impl<R: Read> Read for Base64Decoder<R> { | 67 | 67 |
| 68 | 68 | let nr = self.inner.buf_len() / 4 * 4; | 69 | 69 | let nw = self.inner.buf_len() / 4 * 3; | | 70 | + let input = self | | 71 | + .inner | | 72 | + .buffer() | | 73 | + .get(..nr) | | 74 | + .ok_or_else(|| io::Error::other("input length exceeds the buffer"))?; | 70 | 75 |
| 71 | 76 | let (consumed, written) = if nw > into.len() { | 72 | | - let (consumed, nw) = | 73 | | - try_decode_engine_slice(&self.inner.buffer()[..nr], &mut self.out_buffer[..]); | | 77 | + let (consumed, nw) = try_decode_engine_slice(input, &mut self.out_buffer[..]); | 74 | 78 |
| 75 | 79 | let n = std::cmp::min(nw, into.len()); | 76 | | - let t = &self.out_buffer[0..nw]; | | 80 | + let t = self | | 81 | + .out_buffer | | 82 | + .get(0..nw) | | 83 | + .ok_or_else(|| io::Error::other("decoded data exceeds the buffer"))?; | 77 | 84 | let (t1, t2) = t.split_at(n); | 78 | 85 |
| 79 | 86 | // copy what we have into `into` | 80 | | - into[0..n].copy_from_slice(t1); | | 87 | + into.get_mut(0..n) | | 88 | + .ok_or_else(|| io::Error::other("decoded data exceeds the buffer"))? | | 89 | + .copy_from_slice(t1); | 81 | 90 | // store the rest | 82 | 91 | self.out.copy_from_slice(t2); | 83 | 92 |
| 84 | 93 | (consumed, n) | 85 | 94 | } else { | 86 | | - try_decode_engine_slice(&self.inner.buffer()[..nr], into) | | 95 | + try_decode_engine_slice(input, into) | 87 | 96 | }; | 88 | 97 |
| 89 | 98 | self.inner.consume(consumed); | @@ -101,7 +110,7 @@ fn try_decode_engine_slice<T: ?Sized + AsRef<[u8]>>( | 101 | 110 | let input_bytes = input.as_ref(); | 102 | 111 | let mut n = input_bytes.len(); | 103 | 112 | while n > 0 { | 104 | | - match ENGINE.decode_slice(&input_bytes[..n], output) { | | 113 | + match ENGINE.decode_slice(input_bytes.get(..n).unwrap_or(input_bytes), output) { | 105 | 114 | Ok(size) => { | 106 | 115 | return (n, size); | 107 | 116 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/base64/reader.rs +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,19 +31,22 @@ impl<R: BufRead> Read for Base64Reader<R> { | 31 | 31 | let mut n = 0; | 32 | 32 | loop { | 33 | 33 | // skip new lines | 34 | | - while buf[buf_i] == b'\r' || buf[buf_i] == b'\n' { | | 34 | + while buf.get(buf_i) == Some(&b'\r') || buf.get(buf_i) == Some(&b'\n') { | 35 | 35 | buf_i += 1; | 36 | 36 | if buf_i == buf.len() { | 37 | 37 | break; | 38 | 38 | } | 39 | 39 | } | 40 | 40 |
| 41 | | - if buf_i < buf.len() { | 42 | | - if !is_base64_token(buf[buf_i]) { | | 41 | + if let Some(&byte) = buf.get(buf_i) { | | 42 | + if !is_base64_token(byte) { | 43 | 43 | break; | 44 | 44 | } | 45 | 45 |
| 46 | | - into[n] = buf[buf_i]; | | 46 | + let Some(slot) = into.get_mut(n) else { | | 47 | + break; | | 48 | + }; | | 49 | + *slot = byte; | 47 | 50 | n += 1; | 48 | 51 | buf_i += 1; | 49 | 52 | if n == into.len() { | |
Collapse file Copy file name to clipboardExpand all lines: src/composed/message/builder.rs +14-16Lines changed: 14 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1229,31 +1229,28 @@ fn encrypt_write<R: std::io::Read, W: std::io::Write>( | 1229 | 1229 | let mut is_first = true; | 1230 | 1230 |
| 1231 | 1231 | loop { | 1232 | | - let (length, mut buf) = if is_first { | | 1232 | + let (length, read) = if is_first { | 1233 | 1233 | let read = fill_buffer(&mut encrypted, &mut buffer, Some(first_chunk_size))?; | 1234 | 1234 | if read < first_chunk_size { | 1235 | 1235 | // finished reading, all data fits into a single chunk | 1236 | 1236 | let size = (read + config_len).try_into()?; | 1237 | 1237 | debug!("single chunk of size {size}"); | 1238 | | - (PacketLength::Fixed(size), &buffer[..read]) | | 1238 | + (PacketLength::Fixed(size), read) | 1239 | 1239 | } else { | 1240 | | - ( | 1241 | | - PacketLength::Partial(partial_chunk_size.try_into()?), | 1242 | | - &buffer[..read], | 1243 | | - ) | | 1240 | + (PacketLength::Partial(partial_chunk_size.try_into()?), read) | 1244 | 1241 | } | 1245 | 1242 | } else { | 1246 | 1243 | let read = fill_buffer(&mut encrypted, &mut buffer, Some(partial_chunk_size))?; | 1247 | 1244 | if read < partial_chunk_size { | 1248 | 1245 | // last chunk | 1249 | | - (PacketLength::Fixed(read.try_into()?), &buffer[..read]) | | 1246 | + (PacketLength::Fixed(read.try_into()?), read) | 1250 | 1247 | } else { | 1251 | | - ( | 1252 | | - PacketLength::Partial(partial_chunk_size.try_into()?), | 1253 | | - &buffer[..read], | 1254 | | - ) | | 1248 | + (PacketLength::Partial(partial_chunk_size.try_into()?), read) | 1255 | 1249 | } | 1256 | 1250 | }; | | 1251 | + let Some(mut buf) = buffer.get(..read) else { | | 1252 | + bail!("read more than the buffer holds"); | | 1253 | + }; | 1257 | 1254 |
| 1258 | 1255 | if is_first { | 1259 | 1256 | let packet_header = | @@ -1342,7 +1339,10 @@ impl<R: std::io::Read> std::io::Read for SignatureHashers<R> { | 1342 | 1339 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | 1343 | 1340 | let read = self.source.read(buf)?; | 1344 | 1341 |
| 1345 | | - self.update_hashers(&buf[..read]); | | 1342 | + let data = buf | | 1343 | + .get(..read) | | 1344 | + .ok_or_else(|| std::io::Error::other("read more than the buffer holds"))?; | | 1345 | + self.update_hashers(data); | 1346 | 1346 |
| 1347 | 1347 | Ok(read) | 1348 | 1348 | } | @@ -1443,8 +1443,7 @@ impl<R: std::io::Read> std::io::Read for SignGenerator<'_, R> { | 1443 | 1443 | } | 1444 | 1444 | } | 1445 | 1445 |
| 1446 | | - let to_write = buf.len().min(buffer.remaining()); | 1447 | | - buffer.copy_to_slice(&mut buf[..to_write]); | | 1446 | + let to_write = (&mut buffer).reader().read(buf)?; | 1448 | 1447 |
| 1449 | 1448 | self.state = State::Ops { | 1450 | 1449 | ops, | @@ -1510,8 +1509,7 @@ impl<R: std::io::Read> std::io::Read for SignGenerator<'_, R> { | 1510 | 1509 | } | 1511 | 1510 | } | 1512 | 1511 |
| 1513 | | - let to_write = buf.len().min(buffer.remaining()); | 1514 | | - buffer.copy_to_slice(&mut buf[..to_write]); | | 1512 | + let to_write = (&mut buffer).reader().read(buf)?; | 1515 | 1513 |
| 1516 | 1514 | self.state = State::Signatures { | 1517 | 1515 | buffer, | |
Collapse file Copy file name to clipboardExpand all lines: src/composed/message/reader/compressed.rs +1-5Lines changed: 1 addition & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -126,11 +126,7 @@ impl<R: DebugBufRead> Read for CompressedDataReader<R> { | 126 | 126 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | 127 | 127 | self.fill_inner()?; | 128 | 128 | match self { | 129 | | - Self::Body { ref mut buffer, .. } => { | 130 | | - let to_write = buffer.remaining().min(buf.len()); | 131 | | - buffer.copy_to_slice(&mut buf[..to_write]); | 132 | | - Ok(to_write) | 133 | | - } | | 129 | + Self::Body { ref mut buffer, .. } => buffer.reader().read(buf), | 134 | 130 | Self::Done { .. } => Ok(0), | 135 | 131 | Self::Error => Err(io::Error::other("CompressedDataReader errored")), | 136 | 132 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/composed/message/reader/literal.rs +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -170,8 +170,7 @@ impl<R: BufRead> Read for LiteralDataReader<R> { | 170 | 170 | self.fill_inner()?; | 171 | 171 | match self { | 172 | 172 | Self::Body { buffer, .. } | Self::Done { buffer, .. } => { | 173 | | - let to_write = buffer.remaining().min(buf.len()); | 174 | | - buffer.copy_to_slice(&mut buf[..to_write]); | | 173 | + let to_write = buffer.reader().read(buf)?; | 175 | 174 | Ok(to_write) | 176 | 175 | } | 177 | 176 | Self::Error => Err(io::Error::other("LiteralDataReader errored")), | |
Collapse file Copy file name to clipboardExpand all lines: src/composed/message/reader/packet_body.rs +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,8 +58,7 @@ impl<R: BufRead> Read for PacketBodyReader<R> { | 58 | 58 | self.fill_inner()?; | 59 | 59 | match self.state { | 60 | 60 | State::Body { ref mut buffer, .. } => { | 61 | | - let to_write = buffer.remaining().min(buf.len()); | 62 | | - buffer.copy_to_slice(&mut buf[..to_write]); | | 61 | + let to_write = buffer.reader().read(buf)?; | 63 | 62 | Ok(to_write) | 64 | 63 | } | 65 | 64 | State::Done { .. } => Ok(0), | |
Collapse file Copy file name to clipboardExpand all lines: src/composed/message/reader/signed_many.rs +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -256,8 +256,11 @@ impl<'a> SignatureManyReader<'a> { | 256 | 256 | let mut buffer = BytesMut::with_capacity(BUFFER_SIZE); | 257 | 257 | let read = fill_buffer_bytes(&mut source, &mut buffer, BUFFER_SIZE)?; | 258 | 258 |
| | 259 | + let data = buffer | | 260 | + .get(..read) | | 261 | + .ok_or_else(|| io::Error::other("read more than the buffer holds"))?; | 259 | 262 | for hasher in hashers.iter_mut().filter_map(|h| h.as_mut()) { | 260 | | - hasher.hash_buf(&buffer[..read]); | | 263 | + hasher.hash_buf(data); | 261 | 264 | } | 262 | 265 |
| 263 | 266 | *self = Self::Body { | @@ -287,8 +290,11 @@ impl<'a> SignatureManyReader<'a> { | 287 | 290 |
| 288 | 291 | let read = fill_buffer_bytes(&mut source, &mut buffer, BUFFER_SIZE)?; | 289 | 292 |
| | 293 | + let data = buffer | | 294 | + .get(..read) | | 295 | + .ok_or_else(|| io::Error::other("read more than the buffer holds"))?; | 290 | 296 | for hasher in hashers.iter_mut().filter_map(|h| h.as_mut()) { | 291 | | - hasher.hash_buf(&buffer[..read]); | | 297 | + hasher.hash_buf(data); | 292 | 298 | } | 293 | 299 |
| 294 | 300 | if read == 0 { | @@ -540,8 +546,7 @@ impl Read for SignatureManyReader<'_> { | 540 | 546 | match self { | 541 | 547 | Self::Init { .. } => unreachable!("invalid state"), | 542 | 548 | Self::Body { buffer, .. } => { | 543 | | - let to_write = buffer.remaining().min(buf.len()); | 544 | | - buffer.copy_to_slice(&mut buf[..to_write]); | | 549 | + let to_write = buffer.reader().read(buf)?; | 545 | 550 | Ok(to_write) | 546 | 551 | } | 547 | 552 | Self::Done { .. } => Ok(0), | |
Collapse file Copy file name to clipboardExpand all lines: src/composed/message/types.rs +23-11Lines changed: 23 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -857,7 +857,7 @@ impl<'a> Message<'a> { | 857 | 857 | // high 16 bits in the signature packet, even though RFC 9580 doesn't strictly require this) | 858 | 858 | ensure_eq!( | 859 | 859 | signed_hash_value, | 860 | | - &calculated_hash[0..2], | | 860 | + calculated_hash.get(0..2).unwrap_or_default(), | 861 | 861 | "signature: invalid signed hash value" | 862 | 862 | ); | 863 | 863 |
| @@ -1213,7 +1213,9 @@ impl TheRing<'_> { | 1213 | 1213 | // Do we have a session key already? | 1214 | 1214 | if !self.session_keys.is_empty() { | 1215 | 1215 | let session_key = self.session_keys.remove(0); | 1216 | | - result.session_keys[0] = InnerRingResult::Ok; | | 1216 | + if let Some(first) = result.session_keys.first_mut() { | | 1217 | + *first = InnerRingResult::Ok; | | 1218 | + } | 1217 | 1219 | return Ok((Some(session_key), result)); | 1218 | 1220 | } | 1219 | 1221 | } | @@ -1244,8 +1246,13 @@ impl TheRing<'_> { | 1244 | 1246 |
| 1245 | 1247 | for esk in &pkesks { | 1246 | 1248 | debug!("checking esk: {:?}/{:?}", esk.id(), esk.fingerprint()); | 1247 | | - for (i, key) in self.secret_keys.iter().enumerate() { | 1248 | | - result.secret_keys[i] = InnerRingResult::NoMatch; | | 1249 | + for (i, (key, key_result)) in self | | 1250 | + .secret_keys | | 1251 | + .iter() | | 1252 | + .zip(&mut result.secret_keys) | | 1253 | + .enumerate() | | 1254 | + { | | 1255 | + *key_result = InnerRingResult::NoMatch; | 1249 | 1256 |
| 1250 | 1257 | let typ = match esk.version() { | 1251 | 1258 | PkeskVersion::V3 => EskType::V3_4, | @@ -1271,7 +1278,7 @@ impl TheRing<'_> { | 1271 | 1278 | key.primary_key.secret_params().is_encrypted(), | 1272 | 1279 | ); | 1273 | 1280 |
| 1274 | | - result.secret_keys[i] = res; | | 1281 | + *key_result = res; | 1275 | 1282 |
| 1276 | 1283 | if let Some(session_key) = session_key { | 1277 | 1284 | pkesk_session_keys.push((i, session_key)); | @@ -1282,7 +1289,7 @@ impl TheRing<'_> { | 1282 | 1289 | for subkey in &key.secret_subkeys { | 1283 | 1290 | // Once we have found a session key with `key`, stop considering more subkeys. | 1284 | 1291 | // (This avoids potentially overwriting a good decryption result with failed ones.) | 1285 | | - if result.secret_keys[i] == InnerRingResult::Ok { | | 1292 | + if *key_result == InnerRingResult::Ok { | 1286 | 1293 | break; | 1287 | 1294 | } | 1288 | 1295 |
| @@ -1296,7 +1303,7 @@ impl TheRing<'_> { | 1296 | 1303 | { | 1297 | 1304 | debug!("draft_forwarding unset in DecryptionOptions, not trying forwardee key: {:?}", subkey.key.public_key().fingerprint()); | 1298 | 1305 |
| 1299 | | - result.secret_keys[i] = InnerRingResult::Invalid; | | 1306 | + *key_result = InnerRingResult::Invalid; | 1300 | 1307 | continue; // go to next subkey | 1301 | 1308 | } | 1302 | 1309 |
| @@ -1307,7 +1314,7 @@ impl TheRing<'_> { | 1307 | 1314 | subkey.secret_params().is_encrypted(), | 1308 | 1315 | ); | 1309 | 1316 |
| 1310 | | - result.secret_keys[i] = res; | | 1317 | + *key_result = res; | 1311 | 1318 |
| 1312 | 1319 | if let Some(session_key) = session_key { | 1313 | 1320 | pkesk_session_keys.push((i, session_key)); | @@ -1321,7 +1328,12 @@ impl TheRing<'_> { | 1321 | 1328 | let mut skesk_session_keys: Vec<(usize, PlainSessionKey)> = Vec::new(); | 1322 | 1329 |
| 1323 | 1330 | for esk in skesks { | 1324 | | - for (i, pw) in self.message_password.iter().enumerate() { | | 1331 | + for (i, (pw, pw_result)) in self | | 1332 | + .message_password | | 1333 | + .iter() | | 1334 | + .zip(&mut result.message_password) | | 1335 | + .enumerate() | | 1336 | + { | 1325 | 1337 | // Skip v5 SKESK unless gnupg_aead is enabled | 1326 | 1338 | if !self.decrypt_options.gnupg_aead && esk.version() == SkeskVersion::V5 { | 1327 | 1339 | debug!("skipping v5 SKESK because allow_gnupg_aead is not enabled: {esk:?}"); | @@ -1331,11 +1343,11 @@ impl TheRing<'_> { | 1331 | 1343 | match decrypt_session_key_with_password(esk, pw) { | 1332 | 1344 | Ok(session_key) => { | 1333 | 1345 | skesk_session_keys.push((i, session_key)); | 1334 | | - result.message_password[i] = InnerRingResult::Ok; | | 1346 | + *pw_result = InnerRingResult::Ok; | 1335 | 1347 | break; | 1336 | 1348 | } | 1337 | 1349 | Err(_err) => { | 1338 | | - result.message_password[i] = InnerRingResult::Invalid; | | 1350 | + *pw_result = InnerRingResult::Invalid; | 1339 | 1351 | } | 1340 | 1352 | } | 1341 | 1353 | } | |
|