bcder icon indicating copy to clipboard operation
bcder copied to clipboard

Avoid certain panics in decoding.

Open partim opened this issue 8 months ago • 2 comments

This PR changes most of the decoding code to be mostly panic free.

Specifically, it removes the use of slice indexing as well as unwrap and expect on options and results.

It does not do this for encoding – which requires some redesign in the traits – and for the string types – which will be done in separate PRs to make reviewing of this PR easier.

The PR introduces a breaking change in the decode::Source trait by changing how invalid indexes are treated. The bytes method now returns an optional Bytes, returning None if the indexes are invalid. If the advance method is called with an invalid length, the source is expected to go into some error state. Finally, the error type of take_opt_u8 was changed to allow an explicit error.

This PR raises the minimum supported Rust version to 1.74.

partim avatar Apr 11 '25 12:04 partim

I am now considering changing the whole thing more drastically. I want to get rid of the built-in support for Bytes, so I think we can merge Source::reserve and Source::slice into one method that gives you exactly as many octets as you ask for or errors out. That would leave Source::advance which we could get rid of by having this new method not return a blank slice but a newtype that you then have to either consume or return an error. We’d have to try the latter to see if it actually works in practice.

But it would avoid the need for debug assertions.

partim avatar Apr 14 '25 13:04 partim

The change to Source sounds good. I'm curious to see how the newtype approach works out -- it can be quite tricky.

bal-e avatar Apr 14 '25 13:04 bal-e

This PR has been superseded by #86.

partim avatar May 22 '25 09:05 partim