PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/run-fail/bug-2470-bounds-check-overflow.rs

https://bitbucket.org/graydon/rust
Rust | 24 lines | 13 code | 4 blank | 7 comment | 0 complexity | f470f062ba2493a1ccbb4ba46d1938c1 MD5 | raw file
  1. // error-pattern:index out of bounds
  2. fn main() {
  3. // This should cause a bounds-check failure, but may not if we do our
  4. // bounds checking by comparing the scaled index to the vector's
  5. // address-bounds, since we've scaled the index to wrap around to the
  6. // address of the 0th cell in the array (even though the index is
  7. // huge).
  8. let x = ~[1u,2u,3u];
  9. do vec::as_imm_buf(x) |p, _len| {
  10. let base = p as uint; // base = 0x1230 say
  11. let idx = base / sys::size_of::<uint>(); // idx = 0x0246 say
  12. error!("ov1 base = 0x%x", base);
  13. error!("ov1 idx = 0x%x", idx);
  14. error!("ov1 sizeof::<uint>() = 0x%x", sys::size_of::<uint>());
  15. error!("ov1 idx * sizeof::<uint>() = 0x%x",
  16. idx * sys::size_of::<uint>());
  17. // This should fail.
  18. error!("ov1 0x%x", x[idx]);
  19. }
  20. }