Skip to content

Commit

Permalink
Close #33
Browse files Browse the repository at this point in the history
See the changelog and test case for more information
  • Loading branch information
myrrlyn committed Dec 4, 2019
1 parent 2db6314 commit a1989fa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes will be documented in this file.

This document is written according to the [Keep a Changelog][kac] style.

## 0.16.1

This is a hotfix for [Issue #33], filed by GitHub user [@jonas-schievink].
`BitVec::reserve` computed an incorrect element count to pass to `Vec::reserve`,
causing `BitVec::resize` to panic when its `BitVec::reserve` call failed to
sufficiently allocate memory before `BitVec::set_len` expanded into the memory
it expected to be present.

## 0.16.0

### Added
Expand Down Expand Up @@ -569,6 +577,7 @@ Initial implementation and release.
[@GeorgeGkas]: https://github.com/GeorgeGkas
[@caelunshun]: https://github.com/caelunshun
[@geq1t]: https://github.com/geq1t
[@jonas-schievink]: https://github.com/jonas-schievink
[@koushiro]: https://github.com/koushiro
[@overminder]: https://github.com/overminder
[@ratorx]: https://github.com/ratorx
Expand All @@ -582,5 +591,6 @@ Initial implementation and release.
[Issue #15]: https://github.com/myrrlyn/bitvec/issues/15
[Issue #16]: https://github.com/myrrlyn/bitvec/issues/16
[Issue #28]: https://github.com/myrrlyn/bitvec/issues/28
[Issue #33]: https://github.com/myrrlyn/bitvec/issues/33
[`Sync`]: https://doc.rust-lang.org/stable/core/marker/trait.Sync.html
[kac]: https://keepachangelog.com/en/1.0.0/
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[package]
name = "bitvec"
version = "0.16.0"
version = "0.16.1"
authors = [
"myrrlyn <self@myrrlyn.dev>",
]
Expand Down
2 changes: 1 addition & 1 deletion src/vec/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where C: Cursor, T: BitStore {
BitPtr::<T>::MAX_BITS,
);
let (total_elts, _) = self.pointer.head().span(newlen);
if let Some(extra) = total_elts.checked_sub(self.capacity) {
if let Some(extra) = total_elts.checked_sub(self.pointer.elements()) {
self.do_unto_vec(|v| v.reserve(extra));
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/issue_33.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*! Test case for [Issue #33], provided by GitHub user [@jonas-schievink].
This report discovered an error in the implementation of `BitVec::reserve`,
which caused it to fail to reällocate in certain conditions.
The error was that the `reserve` method was testing the reservation amount
passed in to `Vec::reserve` against the currently-allocated *capacity*, not the
currently-occupied *element length*. `Vec::reserve` expects the difference to be
against the element length, so `BitVec::reserve` was estimating too few elements
and `Vec::reserve` did not see the request amount as requiring a reällocation.
`BitVec::reserve` now tests the reservation amount against the current element
length, which produces the correct reservation request for `Vec::reserve`,
fixing the error.
[Issue #33]: //github.com/myrrlyn/bitvec/issues/33
[@jonas-schievink]: //github.com/jonas-schievink
!*/

#[cfg(feature = "alloc")]
use bitvec::prelude::*;

#[cfg(feature = "alloc")]
#[test]
fn issue_33() {
let mut swdio = BitVec::<LittleEndian, u8>::new();

swdio.resize(64, true);

let mut seq = 0xE79E; // LSb first
for _ in 0..16 {
swdio.push(seq & 0b1 != 0);
seq >>= 1;
}

swdio.reserve(64);
swdio.resize(swdio.len() + 64, true);

swdio.resize(swdio.len() + 10, false);
}

0 comments on commit a1989fa

Please sign in to comment.