mozilla/rust — pull request #9108 by blake2-ppc (2013-09-11 00:58:34+00:00)
Title: Fix uint overflow bugs in std::vec and std::str

Fix the worst parts of issue #8742, which summarized is: unsafe code in vec and str did assume that a reservation for `X + Y` elements always succeeded, and didn't overflow.

Introduce the method `Vec::reserve_additional(n)` to make it easy to check for overflow in `Vec::push` and `Vec::push_all`.

In std::str, simplify and remove a lot of the unsafe code and use `push_str` instead. This leads to some slowdowns, but the previous code was incorrect.

Mini-benchmarks show some slowdown:

before
test str::bench::bench_push_str ... bench: 138 ns/iter (+/- 1)
test str::bench::bench_connect ... bench: 238 ns/iter (+/- 4)

after
test str::bench::bench_push_str ... bench: 144 ns/iter (+/- 3)
test str::bench::bench_connect ... bench: 316 ns/iter (+/- 8)

I believe there are many places still where `v.reserve(A + B)` still can overflow. This by itself is not an issue unless followed by (unsafe) code that steps aside boundary checks.