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
#[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
240
    pub fn from_offset(s: &str, off: usize) -> Self {
62
240
        if off > s.len() || !s.is_char_boundary(off) {
63
            Pos::Invalid(off)
64
        } else {
65
240
            let s = &s[..off];
66
240
            let last_nl = s.rfind('\n');
67
240
            match last_nl {
68
98
                Some(pos) => {
69
103009
                    let newlines = s.bytes().filter(|b| *b == b'\n').count();
70
98
                    Pos::PosInLine {
71
98
                        line: newlines + 1,
72
98
                        byte: off - pos,
73
98
                    }
74
                }
75
142
                None => Pos::PosInLine {
76
142
                    line: 1,
77
142
                    byte: off + 1,
78
142
                },
79
            }
80
        }
81
240
    }
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
49540
    pub fn at(s: &str) -> Self {
86
49540
        let ptr = s.as_ptr();
87
49540
        Pos::Raw { ptr }
88
49540
    }
89
    /// Construct Pos from the end of some other string.
90
520
    pub fn at_end_of(s: &str) -> Self {
91
520
        let ending = &s[s.len()..];
92
520
        Pos::at(ending)
93
520
    }
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
94
    pub fn from_line(line: usize, byte: usize) -> Self {
100
94
        Pos::PosInLine { line, byte }
101
94
    }
102
    /// If this position appears within `s`, and has not yet been mapped to
103
    /// a line-and-byte position, return its offset.
104
1270
    pub(crate) fn offset_within(&self, s: &str) -> Option<usize> {
105
1270
        match self {
106
            Pos::Byte { off } => Some(*off),
107
1270
            Pos::Raw { ptr } => offset_in(*ptr, s),
108
            _ => None,
109
        }
110
1270
    }
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
220
    pub fn within(self, s: &str) -> Self {
121
220
        match self {
122
            Pos::Byte { off } => Self::from_offset(s, off),
123
72
            Pos::Raw { ptr } => {
124
72
                if let Some(off) = offset_in(ptr, s) {
125
72
                    Self::from_offset(s, off)
126
                } else {
127
                    self
128
                }
129
            }
130
148
            _ => self,
131
        }
132
220
    }
133
}
134

            
135
/// If `ptr` is within `s`, return its byte offset.
136
1342
fn offset_in(ptr: *const u8, s: &str) -> Option<usize> {
137
1342
    // We need to confirm that 'ptr' falls within 's' in order
138
1342
    // to subtract it meaningfully and find its offset.
139
1342
    // Otherwise, we'll get a bogus result.
140
1342
    //
141
1342
    // Fortunately, we _only_ get a bogus result: we don't
142
1342
    // hit unsafe behavior.
143
1342
    let ptr_u = ptr as usize;
144
1342
    let start_u = s.as_ptr() as usize;
145
1342
    let end_u = (s.as_ptr() as usize) + s.len();
146
1342
    if start_u <= ptr_u && ptr_u < end_u {
147
1342
        Some(ptr_u - start_u)
148
    } else {
149
        None
150
    }
151
1342
}
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
#[derive(Copy, Clone, Debug, derive_more::Display, PartialEq, Eq)]
169
#[non_exhaustive]
170
pub enum NetdocErrorKind {
171
    /// An internal error in the parser: these should never happen.
172
    #[display("internal error")]
173
    Internal,
174
    /// Invoked an API in an incorrect manner.
175
    #[display("bad API usage")]
176
    BadApiUsage,
177
    /// An entry was found with no keyword.
178
    #[display("no keyword for entry")]
179
    MissingKeyword,
180
    /// An entry was found with no newline at the end.
181
    #[display("line truncated before newline")]
182
    TruncatedLine,
183
    /// A bad string was found in the keyword position.
184
    #[display("invalid keyword")]
185
    BadKeyword,
186
    /// We found an ill-formed "BEGIN FOO" tag.
187
    #[display("invalid PEM BEGIN tag")]
188
    BadObjectBeginTag,
189
    /// We found an ill-formed "END FOO" tag.
190
    #[display("invalid PEM END tag")]
191
    BadObjectEndTag,
192
    /// We found a "BEGIN FOO" tag with an "END FOO" tag that didn't match.
193
    #[display("mismatched PEM tags")]
194
    BadObjectMismatchedTag,
195
    /// We found a base64 object with an invalid base64 encoding.
196
    #[display("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("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("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("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("entry out of place")]
213
    MisplacedToken,
214
    /// We found more arguments on an entry than it is allowed to have.
215
    #[display("too many arguments")]
216
    TooManyArguments,
217
    /// We didn't fine enough arguments for some entry.
218
    #[display("too few arguments")]
219
    TooFewArguments,
220
    /// We found an object attached to an entry that isn't supposed to
221
    /// have one.
222
    #[display("unexpected object")]
223
    UnexpectedObject,
224
    /// An entry was supposed to have an object, but it didn't.
225
    #[display("missing object")]
226
    MissingObject,
227
    /// We found an object on an entry, but the type was wrong.
228
    #[display("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("missing argument")]
236
    MissingArgument,
237
    /// We found an argument that couldn't be parsed.
238
    #[display("bad argument for entry")]
239
    BadArgument,
240
    /// We found an object that couldn't be parsed after it was decoded.
241
    #[display("bad object for entry")]
242
    BadObjectVal,
243
    /// There was some signature that we couldn't validate.
244
    #[display("couldn't validate signature")]
245
    BadSignature, // TODO(nickm): say which kind of signature.
246
    /// The object is not valid at the required time.
247
    #[display("couldn't validate time bound")]
248
    BadTimeBound,
249
    /// There was a tor version we couldn't parse.
250
    #[display("couldn't parse Tor version")]
251
    BadTorVersion,
252
    /// There was an ipv4 or ipv6 policy entry that we couldn't parse.
253
    #[display("invalid policy entry")]
254
    BadPolicy,
255
    /// An underlying byte sequence couldn't be decoded.
256
    #[display("decoding error")]
257
    Undecodable,
258
    /// Versioned document with an unrecognized version.
259
    #[display("unrecognized document version")]
260
    BadDocumentVersion,
261
    /// Unexpected document type
262
    #[display("unexpected document type")]
263
    BadDocumentType,
264
    /// We expected a kind of entry that we didn't find
265
    #[display("missing entry")]
266
    MissingEntry,
267
    /// Document or section started with wrong token
268
    #[display("Wrong starting token")]
269
    WrongStartingToken,
270
    /// Document or section ended with wrong token
271
    #[display("Wrong ending token")]
272
    WrongEndingToken,
273
    /// Items not sorted as expected
274
    #[display("Incorrect sort order")]
275
    WrongSortOrder,
276
    /// A consensus lifetime was ill-formed.
277
    #[display("Invalid consensus lifetime")]
278
    InvalidLifetime,
279
    /// Found an empty line in the middle of a document
280
    #[display("Empty line")]
281
    EmptyLine,
282
}
283

            
284
/// The underlying source for an [`Error`](struct@Error).
285
#[derive(Clone, Debug, Error)]
286
#[non_exhaustive]
287
pub(crate) enum NetdocErrorSource {
288
    /// An error when parsing a binary object.
289
    #[error("Error parsing binary object")]
290
    Bytes(#[from] tor_bytes::Error),
291
    /// An error when parsing an exit policy.
292
    #[error("Error parsing policy")]
293
    Policy(#[from] PolicyError),
294
    /// An error when parsing an integer.
295
    #[error("Couldn't parse integer")]
296
    Int(#[from] std::num::ParseIntError),
297
    /// An error when parsing an IP or socket address.
298
    #[error("Couldn't parse address")]
299
    Address(#[from] std::net::AddrParseError),
300
    /// An error when validating a signature.
301
    #[error("Invalid signature")]
302
    Signature(#[source] Arc<signature::Error>),
303
    /// An error when validating a signature on an embedded binary certificate.
304
    #[error("Invalid certificate")]
305
    CertSignature(#[from] tor_cert::CertError),
306
    /// An error caused by an expired or not-yet-valid descriptor.
307
    #[error("Descriptor expired or not yet valid")]
308
    UntimelyDescriptor(#[from] tor_checkable::TimeValidityError),
309
    /// Invalid protocol versions.
310
    #[error("Protocol versions")]
311
    Protovers(#[from] tor_protover::ParseError),
312
    /// A bug in our programming, or somebody else's.
313
    #[error("Internal error or bug")]
314
    Bug(#[from] tor_error::Bug),
315
}
316

            
317
impl NetdocErrorKind {
318
    /// Construct a new Error with this kind.
319
    #[must_use]
320
36456
    pub(crate) fn err(self) -> Error {
321
36456
        Error {
322
36456
            kind: self,
323
36456
            msg: None,
324
36456
            pos: Pos::Unknown,
325
36456
            source: None,
326
36456
        }
327
36456
    }
328

            
329
    /// Construct a new error with this kind at a given position.
330
    #[must_use]
331
348
    pub(crate) fn at_pos(self, pos: Pos) -> Error {
332
348
        self.err().at_pos(pos)
333
348
    }
334

            
335
    /// Construct a new error with this kind and a given message.
336
    #[must_use]
337
36104
    pub(crate) fn with_msg<T>(self, msg: T) -> Error
338
36104
    where
339
36104
        T: Into<Cow<'static, str>>,
340
36104
    {
341
36104
        self.err().with_msg(msg)
342
36104
    }
343
}
344

            
345
impl From<signature::Error> for NetdocErrorSource {
346
    fn from(err: signature::Error) -> Self {
347
        NetdocErrorSource::Signature(Arc::new(err))
348
    }
349
}
350

            
351
/// An error that occurred while parsing a directory object of some kind.
352
#[derive(Debug, Clone)]
353
#[non_exhaustive]
354
pub struct Error {
355
    /// What kind of error occurred?
356
    pub(crate) kind: NetdocErrorKind,
357
    /// Do we have more information about the error?>
358
    msg: Option<Cow<'static, str>>,
359
    /// Where did the error occur?
360
    pos: Pos,
361
    /// Was this caused by another error?
362
    source: Option<NetdocErrorSource>,
363
}
364

            
365
impl PartialEq for Error {
366
96
    fn eq(&self, other: &Self) -> bool {
367
96
        self.kind == other.kind && self.msg == other.msg && self.pos == other.pos
368
96
    }
369
}
370

            
371
impl Error {
372
    /// Helper: return this error's position.
373
2
    pub(crate) fn pos(&self) -> Pos {
374
2
        self.pos
375
2
    }
376

            
377
    /// Return a new error based on this one, with any byte-based
378
    /// position mapped to some line within a string.
379
    #[must_use]
380
210
    pub fn within(mut self, s: &str) -> Error {
381
210
        self.pos = self.pos.within(s);
382
210
        self
383
210
    }
384

            
385
    /// Return a new error based on this one, with the position (if
386
    /// any) replaced by 'p'.
387
    #[must_use]
388
36440
    pub fn at_pos(mut self, p: Pos) -> Error {
389
36440
        self.pos = p;
390
36440
        self
391
36440
    }
392

            
393
    /// Return a new error based on this one, with the position (if
394
    /// replaced by 'p' if it had no position before.
395
    #[must_use]
396
8
    pub fn or_at_pos(mut self, p: Pos) -> Error {
397
8
        match self.pos {
398
6
            Pos::None | Pos::Unknown => {
399
6
                self.pos = p;
400
6
            }
401
2
            _ => (),
402
        }
403
8
        self
404
8
    }
405

            
406
    /// Return a new error based on this one, with the message
407
    /// value set to a provided static string.
408
    #[must_use]
409
36218
    pub(crate) fn with_msg<T>(mut self, message: T) -> Error
410
36218
    where
411
36218
        T: Into<Cow<'static, str>>,
412
36218
    {
413
36218
        self.msg = Some(message.into());
414
36218
        self
415
36218
    }
416

            
417
    /// Return a new error based on this one, with the source-error
418
    /// value set to the provided error.
419
    #[must_use]
420
12
    pub(crate) fn with_source<T>(mut self, source: T) -> Error
421
12
    where
422
12
        T: Into<NetdocErrorSource>,
423
12
    {
424
12
        self.source = Some(source.into());
425
12
        self
426
12
    }
427

            
428
    /// Return the [`NetdocErrorKind`] of this error.
429
16
    pub fn netdoc_error_kind(&self) -> NetdocErrorKind {
430
16
        self.kind
431
16
    }
432
}
433

            
434
impl fmt::Display for Error {
435
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
436
        write!(f, "{}{}", self.kind, self.pos)?;
437
        if let Some(msg) = &self.msg {
438
            write!(f, ": {}", msg)?;
439
        }
440
        Ok(())
441
    }
442
}
443

            
444
impl std::error::Error for Error {
445
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
446
        self.source.as_ref().map(|s| s as _)
447
    }
448
}
449

            
450
/// Helper: declare an Into<> implementation to automatically convert a $source
451
/// into an Error with kind $kind.
452
macro_rules! declare_into  {
453
    {$source:ty => $kind:ident} => {
454
        impl From<$source> for Error {
455
6
            fn from(source: $source) -> Error {
456
6
                Error {
457
6
                    kind: NetdocErrorKind::$kind,
458
6
                    msg: None,
459
6
                    pos: Pos::Unknown,
460
6
                    source: Some(source.into())
461
6
                }
462
6
            }
463
        }
464
    }
465
}
466

            
467
declare_into! { signature::Error => BadSignature }
468
declare_into! { tor_checkable::TimeValidityError => BadTimeBound }
469
declare_into! { tor_bytes::Error => Undecodable }
470
declare_into! { std::num::ParseIntError => BadArgument }
471
declare_into! { std::net::AddrParseError => BadArgument }
472
declare_into! { PolicyError => BadPolicy }
473

            
474
impl From<tor_error::Bug> for Error {
475
8
    fn from(err: tor_error::Bug) -> Self {
476
        use tor_error::HasKind;
477
8
        let kind = match err.kind() {
478
            tor_error::ErrorKind::BadApiUsage => NetdocErrorKind::BadApiUsage,
479
8
            _ => NetdocErrorKind::Internal,
480
        };
481

            
482
8
        Error {
483
8
            kind,
484
8
            msg: None,
485
8
            pos: Pos::Unknown,
486
8
            source: Some(err.into()),
487
8
        }
488
8
    }
489
}
490

            
491
/// An error that occurs while trying to construct a network document.
492
#[derive(Clone, Debug, Error)]
493
#[non_exhaustive]
494
pub enum BuildError {
495
    /// We were unable to build the document, probably due to an invalid
496
    /// argument of some kind.
497
    #[error("cannot build document: {0}")]
498
    CannotBuild(&'static str),
499

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