Skip to content

Commit 7913868

Browse files
committed
Check bounds when calling Comment::get_child (fix #142)
1 parent 19a1eb1 commit 7913868

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/clang.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,13 @@ impl Comment {
796796
}
797797

798798
/// Get this comment's `idx`th child comment
799-
pub fn get_child(&self, idx: c_uint) -> Comment {
800-
unsafe {
801-
Comment { x: clang_Comment_getChild(self.x, idx) }
799+
pub fn get_child(&self, idx: c_uint) -> Option<Comment> {
800+
if idx >= self.num_children() {
801+
None
802+
} else {
803+
unsafe {
804+
Some(Comment { x: clang_Comment_getChild(self.x, idx) })
805+
}
802806
}
803807
}
804808

src/ir/annotations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ impl Annotations {
153153
}
154154

155155
for i in 0..comment.num_children() {
156-
self.parse(&comment.get_child(i), matched);
156+
let child = &comment.get_child(i).expect("index higher than number of children");
157+
self.parse(child, matched);
157158
}
158159
}
159160
}

0 commit comments

Comments
 (0)