1
//! Error type from parsing a document, and the position where it occurred
2
use thiserror::Error;
3

            
4
use crate::types::policy::PolicyError;
5
use std::{borrow::Cow, fmt, sync::Arc};
6

            
7
/// A position within a directory object. Used to tell where an error
8
/// occurred.
9
10
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
10
#[non_exhaustive]
11
pub enum Pos {
12
    /// The error did not occur at any particular position.
13
    ///
14
    /// This can happen when the error is something like a missing entry:
15
    /// the entry is supposed to go _somewhere_, but we can't say where.
16
    None,
17
    /// The error occurred at an unknown position.
18
    ///
19
    /// We should avoid using this case.
20
    Unknown,
21
    /// The error occurred at an invalid offset within the string, or
22
    /// outside the string entirely.
23
    ///
24
    /// This can only occur because of an internal error of some kind.
25
    Invalid(usize),
26
    /// The error occurred at a particular byte within the string.
27
    ///
28
    /// We try to convert these to a Pos before displaying them to the user.
29
    Byte {
30
        /// Byte offset within a string.
31
        off: usize,
32
    },
33
    /// The error occurred at a particular line (and possibly at a
34
    /// particular byte within the line.)
35
    PosInLine {
36
        /// Line offset within a string.
37
        line: usize,
38
        /// Byte offset within the line.
39
        byte: usize,
40
    },
41
    /// The error occurred at a position in memory.  This shouldn't be
42
    /// exposed to the user, but rather should be mapped to a position
43
    /// in the string.
44
    Raw {
45
        /// A raw pointer to the position where the error occurred.
46
        ptr: *const u8,
47
    },
48
}
49

            
50
// It's okay to send a Pos to another thread, even though its Raw
51
// variant contains a pointer. That's because we never dereference the
52
// pointer: we only compare it to another pointer representing a
53
// string.
54
//
55
// TODO: Find a better way to have Pos work.
56
unsafe impl Send for Pos {}
57
unsafe impl Sync for Pos {}
58

            
59
impl Pos {
60
    /// Construct a Pos from an offset within a &str slice.
61
37
    pub fn from_offset(s: &str, off: usize) -> Self {
62
37
        if off > s.len() || !s.is_char_boundary(off) {
63
            Pos::Invalid(off)
64
        } else {
65
37
            let s = &s[..off];
66
37
            let last_nl = s.rfind('\n');
67
37
            match last_nl {
68
18
                Some(pos) => {
69
4456
                    let newlines = s.bytes().filter(|b| *b == b'\n').count();
70
18
                    Pos::PosInLine {
71
18
                        line: newlines + 1,
72
18
                        byte: off - pos,
73
18
                    }
74
                }
75
19
                None => Pos::PosInLine {
76
19
                    line: 1,
77
19
                    byte: off + 1,
78
19
                },
79
            }
80
        }
81
37
    }
82
    /// Construct a Pos from a slice of some other string.  This
83
    /// Pos won't be terribly helpful, but it may be converted
84
    /// into a useful Pos with `within`.
85
1503
    pub fn at(s: &str) -> Self {
86
1503
        let ptr = s.as_ptr();
87
1503
        Pos::Raw { ptr }
88
1503
    }
89
    /// Construct Pos from the end of some other string.
90
211
    pub fn at_end_of(s: &str) -> Self {
91
211
        let ending = &s[s.len()..];
92
211
        Pos::at(ending)
93
211
    }
94
    /// Construct a position from a byte offset.
95
    pub fn from_byte(off: usize) -> Self {
96
        Pos::Byte { off }
97
    }
98
    /// Construct a position from a line and a byte offset within that line.
99
42
    pub fn from_line(line: usize, byte: usize) -> Self {
100
42
        Pos::PosInLine { line, byte }
101
42
    }
102
    /// If this position appears within `s`, and has not yet been mapped to
103
    /// a line-and-byte position, return its offset.
104
262
    pub(crate) fn offset_within(&self, s: &str) -> Option<usize> {
105
262
        match self {
106
            Pos::Byte { off } => Some(*off),
107
262
            Pos::Raw { ptr } => offset_in(*ptr, s),
108
            _ => None,
109
        }
110
262
    }
111
    /// Given a position, if it was at a byte offset, convert it to a
112
    /// line-and-byte position within `s`.
113
    ///
114
    /// Requires that this position was actually generated from `s`.
115
    /// If it was not, the results here may be nonsensical.
116
    ///
117
    /// TODO: I wish I knew an efficient safe way to do this that
118
    /// guaranteed that we we always talking about the right string.
119
    #[must_use]
120
5
    pub fn within(self, s: &str) -> Self {
121
5
        match self {
122
            Pos::Byte { off } => Self::from_offset(s, off),
123
5
            Pos::Raw { ptr } => {
124
5
                if let Some(off) = offset_in(ptr, s) {
125
5
                    Self::from_offset(s, off)
126
                } else {
127
                    self
128
                }
129
            }
130
            _ => self,
131
        }
132
5
    }
133
}
134

            
135
/// If `ptr` is within `s`, return its byte offset.
136
267
fn offset_in(ptr: *const u8, s: &str) -> Option<usize> {
137
267
    // We need to confirm that 'ptr' falls within 's' in order
138
267
    // to subtract it meaningfully and find its offset.
139
267
    // Otherwise, we'll get a bogus result.
140
267
    //
141
267
    // Fortunately, we _only_ get a bogus result: we don't
142
267
    // hit unsafe behavior.
143
267
    let ptr_u = ptr as usize;
144
267
    let start_u = s.as_ptr() as usize;
145
267
    let end_u = (s.as_ptr() as usize) + s.len();
146
267
    if start_u <= ptr_u && ptr_u < end_u {
147
267
        Some(ptr_u - start_u)
148
    } else {
149
        None
150
    }
151
267
}
152

            
153
impl fmt::Display for Pos {
154
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155
        use Pos::*;
156
        match self {
157
            None => write!(f, ""),
158
            Unknown => write!(f, " at unknown position"),
159
            Invalid(off) => write!(f, " at invalid offset at index {}", off),
160
            Byte { off } => write!(f, " at byte {}", off),
161
            PosInLine { line, byte } => write!(f, " on line {}, byte {}", line, byte),
162
            Raw { ptr } => write!(f, " at {:?}", ptr),
163
        }
164
    }
165
}
166

            
167
/// A variety of parsing error.
168
42
#[derive(Copy, Clone, Debug, derive_more::Display, PartialEq)]
169
#[non_exhaustive]
170
pub enum ParseErrorKind {
171
    /// An internal error in the parser: these should never happen.
172
    #[display(fmt = "internal error")]
173
    Internal,
174
    /// Invoked an API in an incorrect manner.
175
    #[display(fmt = "bad API usage")]
176
    BadApiUsage,
177
    /// An entry was found with no keyword.
178
    #[display(fmt = "no keyword for entry")]
179
    MissingKeyword,
180
    /// An entry was found with no newline at the end.
181
    #[display(fmt = "line truncated before newline")]
182
    TruncatedLine,
183
    /// A bad string was found in the keyword position.
184
    #[display(fmt = "invalid keyword")]
185
    BadKeyword,
186
    /// We found an ill-formed "BEGIN FOO" tag.
187
    #[display(fmt = "invalid PEM BEGIN tag")]
188
    BadObjectBeginTag,
189
    /// We found an ill-formed "END FOO" tag.
190
    #[display(fmt = "invalid PEM END tag")]
191
    BadObjectEndTag,
192
    /// We found a "BEGIN FOO" tag with an "END FOO" tag that didn't match.
193
    #[display(fmt = "mismatched PEM tags")]
194
    BadObjectMismatchedTag,
195
    /// We found a base64 object with an invalid base64 encoding.
196
    #[display(fmt = "invalid base64 in object")]
197
    BadObjectBase64,
198
    /// The document is not supposed to contain more than one of some
199
    /// kind of entry, but we found one anyway.
200
    #[display(fmt = "duplicate entry")]
201
    DuplicateToken,
202
    /// The document is not supposed to contain any of some particular kind
203
    /// of entry, but we found one anyway.
204
    #[display(fmt = "unexpected entry")]
205
    UnexpectedToken,
206
    /// The document is supposed to contain any of some particular kind
207
    /// of entry, but we didn't find one one anyway.
208
    #[display(fmt = "didn't find required entry")]
209
    MissingToken,
210
    /// The document was supposed to have one of these, but not where we
211
    /// found it.
212
    #[display(fmt = "entry out of place")]
213
    MisplacedToken,
214
    /// We found more arguments on an entry than it is allowed to have.
215
    #[display(fmt = "too many arguments")]
216
    TooManyArguments,
217
    /// We didn't fine enough arguments for some entry.
218
    #[display(fmt = "too few arguments")]
219
    TooFewArguments,
220
    /// We found an object attached to an entry that isn't supposed to
221
    /// have one.
222
    #[display(fmt = "unexpected object")]
223
    UnexpectedObject,
224
    /// An entry was supposed to have an object, but it didn't.
225
    #[display(fmt = "missing object")]
226
    MissingObject,
227
    /// We found an object on an entry, but the type was wrong.
228
    #[display(fmt = "wrong object type")]
229
    WrongObject,
230
    /// We tried to find an argument that we were sure would be there,
231
    /// but it wasn't!
232
    ///
233
    /// This error should never occur in correct code; it should be
234
    /// caught earlier by TooFewArguments.
235
    #[display(fmt = "missing argument")]
236
    MissingArgument,
237
    /// We found an argument that couldn't be parsed.
238
    #[display(fmt = "bad argument for entry")]
239
    BadArgument,
240
    /// We found an object that couldn't be parsed after it was decoded.
241
    #[display(fmt = "bad object for entry")]
242
    BadObjectVal,
243
    /// There was some signature that we couldn't validate.
244
    #[display(fmt = "couldn't validate signature")]
245
    BadSignature, // TODO(nickm): say which kind of signature.
246
    /// There was a tor version we couldn't parse.
247
    #[display(fmt = "couldn't parse Tor version")]
248
    BadTorVersion,
249
    /// There was an ipv4 or ipv6 policy entry that we couldn't parse.
250
    #[display(fmt = "invalid policy entry")]
251
    BadPolicy,
252
    /// An underlying byte sequence couldn't be decoded.
253
    #[display(fmt = "decoding error")]
254
    Undecodable,
255
    /// Versioned document with an unrecognized version.
256
    #[display(fmt = "unrecognized document version")]
257
    BadDocumentVersion,
258
    /// Unexpected document type
259
    #[display(fmt = "unexpected document type")]
260
    BadDocumentType,
261
    /// Document or section started with wrong token
262
    #[display(fmt = "Wrong starting token")]
263
    WrongStartingToken,
264
    /// Document or section ended with wrong token
265
    #[display(fmt = "Wrong ending token")]
266
    WrongEndingToken,
267
    /// Items not sorted as expected
268
    #[display(fmt = "Incorrect sort order")]
269
    WrongSortOrder,
270
    /// A consensus lifetime was ill-formed.
271
    #[display(fmt = "Invalid consensus lifetime")]
272
    InvalidLifetime,
273
}
274

            
275
/// The underlying source for an [`Error`](crate::Error).
276
#[derive(Clone, Debug, Error)]
277
#[non_exhaustive]
278
pub(crate) enum ParseErrorSource {
279
    /// An error when parsing a binary object.
280
    #[error("Error parsing binary object")]
281
    Bytes(#[from] tor_bytes::Error),
282
    /// An error when parsing an exit policy.
283
    #[error("Error parsing policy")]
284
    Policy(#[from] PolicyError),
285
    /// An error when parsing an integer.
286
    #[error("Couldn't parse integer")]
287
    Int(#[from] std::num::ParseIntError),
288
    /// An error when parsing an IP or socket address.
289
    #[error("Couldn't parse address")]
290
    Address(#[from] std::net::AddrParseError),
291
    /// An error when validating a signature.
292
    #[error("Invalid signature")]
293
    Signature(#[source] Arc<signature::Error>),
294
    /// Invalid protocol versions.
295
    #[error("Protocol versions")]
296
    Protovers(#[from] tor_protover::ParseError),
297
    /// A bug in our programming, or somebody else's.
298
    #[error("Internal error or bug")]
299
    Bug(#[from] tor_error::Bug),
300
}
301

            
302
impl ParseErrorKind {
303
    /// Construct a new Error with this kind.
304
    #[must_use]
305
147
    pub(crate) fn err(self) -> Error {
306
147
        Error {
307
147
            kind: self,
308
147
            msg: None,
309
147
            pos: None,
310
147
            source: None,
311
147
        }
312
147
    }
313

            
314
    /// Construct a new error with this kind at a given position.
315
    #[must_use]
316
100
    pub(crate) fn at_pos(self, pos: Pos) -> Error {
317
100
        self.err().at_pos(pos)
318
100
    }
319

            
320
    /// Construct a new error with this kind and a given message.
321
    #[must_use]
322
46
    pub(crate) fn with_msg<T>(self, msg: T) -> Error
323
46
    where
324
46
        T: Into<Cow<'static, str>>,
325
46
    {
326
46
        self.err().with_msg(msg)
327
46
    }
328
}
329

            
330
impl From<signature::Error> for ParseErrorSource {
331
    fn from(err: signature::Error) -> Self {
332
        ParseErrorSource::Signature(Arc::new(err))
333
    }
334
}
335

            
336
/// An error that occurred while parsing a directory object of some kind.
337
#[derive(Debug, Clone)]
338
#[non_exhaustive]
339
pub struct Error {
340
    /// What kind of error occurred?
341
    kind: ParseErrorKind,
342
    /// Do we have more information about the error?>
343
    msg: Option<Cow<'static, str>>,
344
    /// Where did the error occur?
345
    pos: Option<Pos>,
346
    /// Was this caused by another error?
347
    source: Option<ParseErrorSource>,
348
}
349

            
350
impl PartialEq for Error {
351
42
    fn eq(&self, other: &Self) -> bool {
352
42
        self.kind == other.kind && self.msg == other.msg && self.pos == other.pos
353
42
    }
354
}
355

            
356
impl Error {
357
    /// Helper: return a mutable reference to this error's position (if any)
358
199
    fn pos_mut(&mut self) -> Option<&mut Pos> {
359
199
        self.pos.as_mut()
360
199
    }
361

            
362
    /// Helper: return this error's position.
363
1
    pub(crate) fn pos(&self) -> Pos {
364
1
        self.pos.unwrap_or(Pos::Unknown)
365
1
    }
366

            
367
    /// Return a new error based on this one, with any byte-based
368
    /// position mapped to some line within a string.
369
    #[must_use]
370
    pub fn within(mut self, s: &str) -> Error {
371
50
        if let Some(p) = self.pos_mut() {
372
            *p = p.within(s);
373
50
        }
374
50
        self
375
50
    }
376

            
377
    /// Return a new error based on this one, with the position (if
378
    /// any) replaced by 'p'.
379
    #[must_use]
380
    pub fn at_pos(mut self, p: Pos) -> Error {
381
145
        if let Some(mypos) = self.pos_mut() {
382
            *mypos = p;
383
145
        }
384
145
        self
385
145
    }
386

            
387
    /// Return a new error based on this one, with the position (if
388
    /// replaced by 'p' if it had no position before.
389
    #[must_use]
390
    pub fn or_at_pos(mut self, p: Pos) -> Error {
391
4
        if let Some(mypos) = self.pos_mut() {
392
            if *mypos == Pos::None {
393
                *mypos = p;
394
            }
395
4
        }
396
4
        self
397
4
    }
398

            
399
    /// Return a new error based on this one, with the message
400
    /// value set to a provided static string.
401
    #[must_use]
402
83
    pub(crate) fn with_msg<T>(mut self, message: T) -> Error
403
83
    where
404
83
        T: Into<Cow<'static, str>>,
405
83
    {
406
83
        self.msg = Some(message.into());
407
83
        self
408
83
    }
409

            
410
    /// Return a new error based on this one, with the source-error
411
    /// value set to the provided error.
412
    #[must_use]
413
3
    pub(crate) fn with_source<T>(mut self, source: T) -> Error
414
3
    where
415
3
        T: Into<ParseErrorSource>,
416
3
    {
417
3
        self.source = Some(source.into());
418
3
        self
419
3
    }
420
}
421

            
422
impl fmt::Display for Error {
423
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
424
        write!(f, "{}{}", self.kind, self.pos.unwrap_or(Pos::None))?;
425
        if let Some(msg) = &self.msg {
426
            write!(f, ": {}", msg)?;
427
        }
428
        Ok(())
429
    }
430
}
431

            
432
impl std::error::Error for Error {
433
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
434
        match &self.source {
435
            Some(e) => Some(e),
436
            None => None,
437
        }
438
    }
439
}
440

            
441
/// Helper: declare an Into<> implementation to automatically convert a $source
442
/// into an Error with kind $kind.
443
macro_rules! declare_into  {
444
    {$source:ty => $kind:ident} => {
445
        impl From<$source> for Error {
446
3
            fn from(source: $source) -> Error {
447
3
                Error {
448
3
                    kind: ParseErrorKind::$kind,
449
3
                    msg: None,
450
3
                    pos: None,
451
3
                    source: Some(source.into())
452
3
                }
453
3
            }
454
        }
455
    }
456
}
457

            
458
declare_into! { signature::Error => BadSignature }
459
declare_into! { tor_bytes::Error => Undecodable }
460
declare_into! { std::num::ParseIntError => BadArgument }
461
declare_into! { std::net::AddrParseError => BadArgument }
462
declare_into! { PolicyError => BadPolicy }
463

            
464
impl From<tor_error::Bug> for Error {
465
4
    fn from(err: tor_error::Bug) -> Self {
466
        use tor_error::HasKind;
467
4
        let kind = match err.kind() {
468
            tor_error::ErrorKind::BadApiUsage => ParseErrorKind::BadApiUsage,
469
4
            _ => ParseErrorKind::Internal,
470
        };
471

            
472
4
        Error {
473
4
            kind,
474
4
            msg: None,
475
4
            pos: None,
476
4
            source: Some(err.into()),
477
4
        }
478
4
    }
479
}
480

            
481
/// An error that occurs while trying to construct a network document.
482
#[derive(Clone, Debug, Error)]
483
#[non_exhaustive]
484
pub enum BuildError {
485
    /// We were unable to build the document, probably due to an invalid
486
    /// argument of some kind.
487
    #[error("cannot build document: {0}")]
488
    CannotBuild(&'static str),
489

            
490
    /// An argument that was given as a string turned out to be unparsable.
491
    #[error("unable to parse argument")]
492
    Parse(#[from] crate::err::Error),
493
}