Skip to content

Commit

Permalink
chore: Remove some unwraps
Browse files Browse the repository at this point in the history
In functions that return `Result`.
  • Loading branch information
larseggert committed Dec 19, 2024
1 parent bb45c74 commit af99cbc
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion neqo-crypto/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ impl SecretAgent {
self.secrets.register(self.fd)?;
self.raw = Some(r);
Ok(())
} else if self.raw.unwrap() == r {
} else if self.raw.ok_or(Error::InternalError)? == r {
Ok(())
} else {
Err(Error::MixedHandshakeMethod)
Expand Down
16 changes: 10 additions & 6 deletions neqo-http3/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ impl Http3Connection {

send_message
.http_stream()
.unwrap()
.ok_or(Error::Internal)?
.send_headers(&final_headers, conn)?;

self.add_streams(
Expand Down Expand Up @@ -1212,8 +1212,12 @@ impl Http3Connection {
stream_id,
events,
self.role,
self.recv_streams.remove(&stream_id).unwrap(),
self.send_streams.remove(&stream_id).unwrap(),
self.recv_streams
.remove(&stream_id)
.ok_or(Error::Internal)?,
self.send_streams
.remove(&stream_id)
.ok_or(Error::Internal)?,
)));
self.add_streams(
stream_id,
Expand Down Expand Up @@ -1284,7 +1288,7 @@ impl Http3Connection {
.map_err(|e| Error::map_stream_create_errors(&e))?;
// Set outgoing WebTransport streams to be fair (share bandwidth)
// This really can't fail, panics if it does
conn.stream_fairness(stream_id, true).unwrap();
conn.stream_fairness(stream_id, true)?;

self.webtransport_create_stream_internal(
wt,
Expand Down Expand Up @@ -1602,7 +1606,7 @@ impl Http3Connection {
let stream = self.recv_streams.remove(&stream_id);
if let Some(ref s) = stream {
if s.stream_type() == Http3StreamType::ExtendedConnect {
self.send_streams.remove(&stream_id).unwrap();
self.send_streams.remove(&stream_id)?;
if let Some(wt) = s.webtransport() {
self.remove_extended_connect(&wt, conn);
}
Expand All @@ -1619,7 +1623,7 @@ impl Http3Connection {
let stream = self.send_streams.remove(&stream_id);
if let Some(ref s) = stream {
if s.stream_type() == Http3StreamType::ExtendedConnect {
if let Some(wt) = self.recv_streams.remove(&stream_id).unwrap().webtransport() {
if let Some(wt) = self.recv_streams.remove(&stream_id)?.webtransport() {
self.remove_extended_connect(&wt, conn);
}
}
Expand Down
3 changes: 2 additions & 1 deletion neqo-http3/src/connection_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,8 @@ impl Http3Client {
/// This cannot panic. The max varint length is 8.
pub fn webtransport_max_datagram_size(&self, session_id: StreamId) -> Res<u64> {
Ok(self.conn.max_datagram_size()?
- u64::try_from(Encoder::varint_len(session_id.as_u64())).unwrap())
- u64::try_from(Encoder::varint_len(session_id.as_u64()))
.map_err(|_| Error::Internal)?)
}

/// Sets the `SendOrder` for a given stream
Expand Down
6 changes: 3 additions & 3 deletions neqo-http3/src/control_stream_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::collections::{HashMap, VecDeque};
use neqo_common::{qtrace, Encoder};
use neqo_transport::{Connection, StreamId, StreamType};

use crate::{frames::HFrame, BufferedStream, Http3StreamType, RecvStream, Res};
use crate::{frames::HFrame, BufferedStream, Error, Http3StreamType, RecvStream, Res};

pub const HTTP3_UNI_STREAM_TYPE_CONTROL: u64 = 0x0;

Expand Down Expand Up @@ -73,7 +73,7 @@ impl ControlStreamLocal {
update_stream.stream_type(),
Http3StreamType::Http | Http3StreamType::Push
));
let stream = update_stream.http_stream().unwrap();
let stream = update_stream.http_stream().ok_or(Error::Internal)?;

// in case multiple priority_updates were issued, ignore now irrelevant
if let Some(hframe) = stream.priority_update_frame() {
Expand All @@ -95,7 +95,7 @@ impl ControlStreamLocal {
qtrace!([self], "Create a control stream.");
self.stream.init(conn.stream_create(StreamType::UniDi)?);
self.stream
.buffer(&[u8::try_from(HTTP3_UNI_STREAM_TYPE_CONTROL).unwrap()]);
.buffer(&[u8::try_from(HTTP3_UNI_STREAM_TYPE_CONTROL).map_err(|_| Error::Internal)?]);
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl WebTransportSession {
pub fn send_request(&mut self, headers: &[Header], conn: &mut Connection) -> Res<()> {
self.control_stream_send
.http_stream()
.unwrap()
.ok_or(Error::Internal)?
.send_headers(headers, conn)
}

Expand All @@ -158,7 +158,7 @@ impl WebTransportSession {
let (out, _) = self
.control_stream_recv
.http_stream()
.unwrap()
.ok_or(Error::Internal)?
.header_unblocked(conn)?;
debug_assert!(out == ReceiveOutput::NoOutput);
self.maybe_check_headers();
Expand All @@ -175,8 +175,7 @@ impl WebTransportSession {

fn priority_update_frame(&mut self) -> Option<HFrame> {
self.control_stream_recv
.http_stream()
.unwrap()
.http_stream()?
.priority_update_frame()
}

Expand Down
2 changes: 1 addition & 1 deletion neqo-http3/src/priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Priority {
Some(ListEntry::Item(Item {
bare_item: BareItem::Integer(u),
..
})) if (0..=7).contains(u) => u8::try_from(*u).unwrap(),
})) if (0..=7).contains(u) => u8::try_from(*u).map_err(|_| Error::Internal)?,
_ => 3,
};
let incremental = match dict.get("i") {
Expand Down
2 changes: 1 addition & 1 deletion neqo-http3/src/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl SendStream for SendMessage {
// cheap, thus not worth optimizing.
conn.stream_set_writable_event_low_watermark(
self.stream_id(),
NonZeroUsize::new(MIN_DATA_FRAME_SIZE).unwrap(),
NonZeroUsize::new(MIN_DATA_FRAME_SIZE).ok_or(Error::Internal)?,
)?;
return Ok(0);
}
Expand Down
4 changes: 2 additions & 2 deletions neqo-http3/src/server_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
connection::{Http3State, WebTransportSessionAcceptAction},
connection_server::Http3ServerHandler,
features::extended_connect::SessionCloseReason,
Http3StreamInfo, Http3StreamType, Priority, Res,
Error, Http3StreamInfo, Http3StreamType, Priority, Res,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -382,7 +382,7 @@ impl WebTransportRequest {
- u64::try_from(Encoder::varint_len(
self.stream_handler.stream_id().as_u64(),
))
.unwrap())
.map_err(|_| Error::Internal)?)
}
}

Expand Down
5 changes: 4 additions & 1 deletion neqo-qpack/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ impl QPackDecoder {
}
if self.send_buf.len() != 0 && self.local_stream_id.is_some() {
let r = conn
.stream_send(self.local_stream_id.unwrap(), &self.send_buf[..])
.stream_send(
self.local_stream_id.ok_or(Error::Internal)?,
&self.send_buf[..],
)
.map_err(|_| Error::DecoderStream)?;
qdebug!([self], "{} bytes sent.", r);
self.send_buf.read(r);
Expand Down
2 changes: 1 addition & 1 deletion neqo-qpack/src/huffman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn decode_huffman(input: &[u8]) -> Res<Vec<u8>> {
if c == 256 {
return Err(Error::HuffmanDecompressionFailed);
}
output.push(u8::try_from(c).unwrap());
output.push(u8::try_from(c).map_err(|_| Error::InternalError)?);
}
}

Expand Down
7 changes: 4 additions & 3 deletions neqo-qpack/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,14 @@ impl HeaderTable {
base: self.base,
refs: 0,
};
if u64::try_from(entry.size()).unwrap() > self.capacity
|| !self.evict_to(self.capacity - u64::try_from(entry.size()).unwrap())
if u64::try_from(entry.size()).map_err(|_| Error::Internal)? > self.capacity
|| !self
.evict_to(self.capacity - u64::try_from(entry.size()).map_err(|_| Error::Internal)?)
{
return Err(Error::DynamicTableFull);
}
self.base += 1;
self.used += u64::try_from(entry.size()).unwrap();
self.used += u64::try_from(entry.size()).map_err(|_| Error::Internal)?;
let index = entry.index();
self.dynamic.push_front(entry);
Ok(index)
Expand Down
2 changes: 1 addition & 1 deletion neqo-transport/src/pmtud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl Pmtud {
qinfo!(
"PMTUD stopped, PLPMTU is now {}, raise timer {:?}",
self.mtu,
self.raise_timer.unwrap()
self.raise_timer
);
}

Expand Down
2 changes: 1 addition & 1 deletion neqo-transport/src/recv_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ impl RecvStream {
session_fc,
} => {
let bytes_read = recv_buf.read(buf);
Self::flow_control_retire_data(u64::try_from(bytes_read).unwrap(), fc, session_fc);
Self::flow_control_retire_data(u64::try_from(bytes_read)?, fc, session_fc);
let fin_read = if data_recvd_state {
if recv_buf.buffered() == 0 {
let received = recv_buf.received();
Expand Down
4 changes: 2 additions & 2 deletions neqo-transport/src/send_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,13 @@ impl TxBuffer {
pub fn next_bytes(&mut self) -> Option<(u64, &[u8])> {
let (start, maybe_len) = self.ranges.first_unmarked_range();

if start == self.retired() + u64::try_from(self.buffered()).unwrap() {
if start == self.retired() + u64::try_from(self.buffered()).ok()? {
return None;
}

// Convert from ranges-relative-to-zero to
// ranges-relative-to-buffer-start
let buff_off = usize::try_from(start - self.retired()).unwrap();
let buff_off = usize::try_from(start - self.retired()).ok()?;

// Deque returns two slices. Create a subslice from whichever
// one contains the first unmarked data.
Expand Down

0 comments on commit af99cbc

Please sign in to comment.