Skip to content

Commit 39a89de

Browse files
committed
Fix off by one in index impl
1 parent 21f6667 commit 39a89de

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ pub trait RowIndex {
15211521
impl RowIndex for uint {
15221522
#[inline]
15231523
fn idx(&self, stmt: &Statement) -> Option<uint> {
1524-
if *self > stmt.result_desc.len() {
1524+
if *self >= stmt.result_desc.len() {
15251525
None
15261526
} else {
15271527
Some(*self)

tests/test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ fn test_get_was_null() {
540540
};
541541
}
542542

543+
#[test]
544+
fn test_get_off_by_one() {
545+
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
546+
let stmt = or_panic!(conn.prepare("SELECT 10::INT as id"));
547+
let mut result = or_panic!(stmt.query(&[]));
548+
549+
match result.next().unwrap().get_opt::<uint, i32>(1) {
550+
Err(Error::InvalidColumn) => {}
551+
res => panic!("unexpected result {}", res),
552+
};
553+
}
554+
543555
#[test]
544556
fn test_custom_notice_handler() {
545557
static mut count: uint = 0;

0 commit comments

Comments
 (0)