Commit Graph

67 Commits

Author SHA1 Message Date
Ian Jackson 42df88d6bf Run maint/add_warning to add lint block everywhere 2023-08-23 10:34:00 +01:00
Emil Engler 73059827f0
tor-bytes: deprecate always_bug with From trait
This commit deprecates the `EncodeError::always_bug` function with a
`From<EncodeError> for Bug` trait, which is a more semantically correct
way to perform this action.
2023-08-17 13:52:29 +02:00
Nick Mathewson cec6d0ce33 Run add_warnings on all files. 2023-08-04 07:45:04 -04:00
Ian Jackson 473447a82e Run maint/add_warning to actually apply new lint allows 2023-07-10 13:49:51 +01:00
Nick Mathewson 03f9f9987a Run add_warning to remove `missing_panics_doc` deny.
Closes #950.
2023-07-06 14:32:23 -04:00
Ian Jackson 161b9844da lints: Run maint/add_warning to actually apply new lints 2023-06-21 12:15:41 +01:00
Nick Mathewson 244ec5dce2 tor-bytes: Remove use of arrayref
Part of #872: Now that const generics are in, we have better ways to
express converting slices into array-references.
2023-05-25 14:48:57 -04:00
Nick Mathewson 7da5d7a8d7 Serval rustdoc link fixes. 2023-03-08 14:21:33 -05:00
Nick Mathewson dab21bc624 tor-bytes: defend against misuse of extract_n().
Previously, if somebody wrote this code, an attacker could easily
use it to cause an OOM panic:

```
let n = r.take_u64();
let items: Vec<Foo> = r.extract_n(n as usize)?;
```

The first line of defense here is not to write protocols like that:
we don't actually _have_ any 32-bit counters in our protocol
AFAICT.

The second line of defense is to pre-check `n` for reasonableness
before calling `extract_n`.

Here we add a third line of defense: whereas previously we would do
`Vec::with_capacity(n)` in `extract_n`, we now allocate an initial
capacity of `min(n, r.remaining())`.  This ensures that the size of
the allocation can't exceed the remaining length of the message,
which (for our cell types at least) should prevent it from
overflowing or running OOM.
2023-03-06 12:33:10 -05:00
Nick Mathewson b36082255b tor-bytes: Clarify that Cursor is not a good thing, and could be neater. 2023-03-01 11:21:52 -05:00
Ian Jackson bab32c6038 clarify results of misusing cursors 2023-03-01 15:57:14 +00:00
Nick Mathewson 0f88c5131f tor-bytes: impl Readable and Writeable for CtByteArray. 2023-02-28 11:23:44 -05:00
Nick Mathewson 0bf1ae70ba tor-bytes: Add cursor functionality to Reader
We'll use this to implement signature and MAC checking for
EstablishIntro cells.
2023-02-28 11:23:44 -05:00
Nick Mathewson 48ab7b0463 Change tor_bytes::Error::BadMessage to a Cow.
Actually, to avoid making a breaking change, I'm deprecating
BadMessage and creating a new InvalidMessage variant that takes a
Cow.  This way I don't need to track every crate that re-exposes
tor_bytes::Error and call this a breaking change in those.

Making this change will allow tor_bytes errors to be much more
helpful.
2023-02-09 10:20:31 -05:00
Nick Mathewson a6dd92843e Allow clippy::unchecked_duration_subtraction in tests
This panics on error, and we're fine with a panic on misbehavior in
tests.
2023-01-27 08:28:02 -05:00
Nick Mathewson bf04641c68 Disable clippy::unlinlined-format-args
This warning kind of snuck up on us! (See #748)  For now, let's
disable it.  (I've cleaned it up in a couple of examples, since
those are meant to be more idiomatic and user-facing.)

Closes #748.
2023-01-27 08:27:47 -05:00
Ian Jackson 043581fe0e tor-bytes: impl Writeable for &impl Writeable 2023-01-24 16:15:17 +00:00
Ian Jackson a4a4b19f21 tor-bytes: Implement conversion from EncodeError to Bug 2023-01-24 16:15:17 +00:00
Ian Jackson 125a455bdc test lint blocks: Add many many automatically
This is precisely the result of running the rune in
maint/adhoc-add-lint-blocks.
2022-12-12 18:00:30 +00:00
Nick Mathewson 932fe48eaf Run add_warnings. 2022-11-03 11:06:02 -04:00
Emil Engler 362d822ee4
tor-bytes: Avoid redundant allocation
See c489e1d911
2022-10-25 16:24:42 +02:00
Nick Mathewson 8f267ba166 Fix some rustdoc errors.
In addition to the usual "You named that method wrong!" errors, we
have a new rustdoc error that complains about bogus "HTML tags" that
are actually unquoted usage of types like `Result<Foo>`.
2022-10-13 09:08:46 -04:00
Ian Jackson f84d8777db cargo fmt to remove blank lines
Apparently cargo fmt doesn't like these, which my perl rune didn't
delete.

This commit is precisely the result of `cargo fmt`.
2022-10-12 15:29:04 +01:00
Ian Jackson d9910dba08 Replace all README copies in src/lib.rs with includes
The feature we want is `#[doc = include_str!("README.md")]`, which is
stable since 1.54 and our MSRV is now 1.56.

This commit is precisely the result of the following Perl rune:
  perl -i~ -0777 -pe 's{(^//!(?!.*\@\@).*\n)+}{#![doc = include_str!("../README.md")]\n}m' crates/*/src/lib.rs
2022-10-12 15:29:03 +01:00
trinity-1686a 7f939fa480 enable doc_auto_cfg feature on every crate when documenting for docs.rs 2022-08-24 18:22:41 +02:00
Nick Mathewson f1c6447403 New SecretBuf type in tor-bytes
This Writer is a simple wrapper around `Vec<u8>` that makes sure
that its contents are cleared whenever they are dropped _or
reallocated_.

The reallocation is the important part here: without that, we risk
not zeroizing the first allocation of the buffer.
2022-08-01 15:41:22 -04:00
eta d6b0116b71 Apply 1 suggestion(s) to 1 file(s) 2022-07-19 19:33:15 +00:00
Nick Mathewson 4dce7557b6 Remove the last vestiges of write_infallible.
Now that everything has been converted to fallible writers, we get
to finally remove write_infallible() from tor_bytes.
2022-07-11 11:18:51 -04:00
Nick Mathewson 9b02cf004c tor-bytes::impls: Remove usage of infallible writers. 2022-07-11 11:18:51 -04:00
Nick Mathewson 5a61a6d73a Remove "write_and_consume_infallible".
There were only a few of these.  Removing it required porting
everything to use `write_and_consume` instead, and handling its
(potential) errors.
2022-07-11 11:18:51 -04:00
Nick Mathewson 8810af7f65 Convert each write_onto_infallible implementation into write_onto. 2022-07-11 11:18:51 -04:00
Nick Mathewson e1337bee8f Convert each write_into_infallible implementation into write_into.
(There was only one.)
2022-07-11 11:18:51 -04:00
Nick Mathewson 26c81edeea Define new write_into and write_onto methods with correct APIs. 2022-07-11 11:18:51 -04:00
Nick Mathewson 7938f65c66 Rename "write" methods on tor-bytes to "write_infallible".
This comprises four renames:

```
write_onto -> write_onto_infallible
write_into -> write_into_infallible
write -> write_infallible
writer_and_consume -> write_and_consume_infallible.
```

The rest of this branch will be concerned with replacing these
`_infallible` methods with ones that return a `Result`.  This is
part of #513.
2022-07-11 11:18:51 -04:00
Nick Mathewson fa72365f0e Add a Bug variant to tor-bytes::EncodeError.
This will help down the line as we make more writers fallible.
2022-07-11 11:18:51 -04:00
Ian Jackson 4831f9d38c Merge branch 'clippy' into 'main'
Fix clippy nightly again

See merge request tpo/core/arti!603
2022-06-24 14:13:58 +00:00
Ian Jackson 589c6e52bb Run maint/add_warning crates/*/src/{lib,main}.rs
Update all lint blocks
2022-06-23 19:15:42 +01:00
Nick Mathewson f699988c41 tor-bytes: Split EncodeError from Error
Since there is currently only one error type that can occur when
encoding, it doesn't make sense to use the full Error type here.

This split will help us downstream, as we no longer need to
categorize tor_bytes::Error as "an error in encoding or decoding".

I considered renaming Error to DecodeError, but that had pretty huge
downstream effects, and didn't seem to be worth it.
2022-06-23 08:58:25 -04:00
Nick Mathewson 3120f1092a Style fixes to tor-bytes errors.
Also note an issue with the design of tor-bytes::Error that should
probably go in a separate MR.
2022-06-22 09:00:59 -04:00
Ian Jackson 03a19f3877 tor-bytes: read_nested_*: rustfmt
I disagree with almost all of these layout decisions...
2022-06-10 12:30:22 +01:00
Ian Jackson 8fe63b3192 tor-bytes: read_nested_*: Take a closure
This eliminates the possibility of writing the bug of failing to call
`should_be_exhausted`.

As per this discussion
  https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/572#note_2811464

Fixes #498
2022-06-10 12:27:00 +01:00
Nick Mathewson a2c6bd08fe Fix typos in docs and comments 2022-06-09 17:08:56 +01:00
Ian Jackson 6f92bf9e86 tor-bytes: Provide nested writers 2022-06-09 17:08:56 +01:00
Ian Jackson a6a48c3c28 tor-bytes: Provide nested readers 2022-06-09 17:08:56 +01:00
Ian Jackson 04bca2e75a tor-bytes: Reader: Provide take_rest
We'll want this in a moment.
2022-06-09 17:08:56 +01:00
Ian Jackson 655804f804 tor-bytes: Prepare errors for nested reader/writer
Writing is going to be able to give errors too.
2022-06-09 17:08:56 +01:00
Ian Jackson 4f42101554 lints: Add let_unit_value allow to all crates
From running add_warning, with manual picking of the right
hunks/lines.
2022-05-31 15:23:52 +01:00
Ian Jackson ba0843da4a lints: Add lint block delimiters to every crate
This was the result of:
  maint/add_warning crates/*/src/{lib,main}.rs
and then manually curating the results.
2022-05-31 13:00:31 +01:00
Ian Jackson 4a06ce01e1 clippy: Drop an unused lifetime 2022-05-11 18:51:07 +01:00
Samanta Navarro c53818d496 Fix grammar and typos 2022-04-27 13:52:13 +01:00