Skip to content

Commit 7951718

Browse files
committed
Don't increment the iterator while a Section/Relocation/Symbol is using it.
Fixes rust-lang#172.
1 parent 777ef66 commit 7951718

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/object_file.rs

+31-16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl Drop for ObjectFile {
4848
pub struct SectionIterator {
4949
section_iterator: LLVMSectionIteratorRef,
5050
object_file: LLVMObjectFileRef,
51+
before_first: bool,
5152
}
5253

5354
impl SectionIterator {
@@ -56,7 +57,8 @@ impl SectionIterator {
5657

5758
SectionIterator {
5859
section_iterator,
59-
object_file
60+
object_file,
61+
before_first: true
6062
}
6163
}
6264
}
@@ -65,7 +67,14 @@ impl Iterator for SectionIterator {
6567
type Item = Section;
6668

6769
fn next(&mut self) -> Option<Self::Item> {
68-
// REVIEW: Should it compare against 1? End checking order might also be off
70+
if self.before_first {
71+
self.before_first = false;
72+
} else {
73+
unsafe {
74+
LLVMMoveToNextSection(self.section_iterator);
75+
}
76+
}
77+
6978
let at_end = unsafe {
7079
LLVMIsSectionIteratorAtEnd(self.object_file, self.section_iterator) == 1
7180
};
@@ -76,10 +85,6 @@ impl Iterator for SectionIterator {
7685

7786
let section = Section::new(self.section_iterator, self.object_file);
7887

79-
unsafe {
80-
LLVMMoveToNextSection(self.section_iterator)
81-
}
82-
8388
Some(section)
8489
}
8590
}
@@ -151,6 +156,7 @@ pub struct RelocationIterator {
151156
relocation_iterator: LLVMRelocationIteratorRef,
152157
section_iterator: LLVMSectionIteratorRef,
153158
object_file: LLVMObjectFileRef,
159+
before_first: bool,
154160
}
155161

156162
impl RelocationIterator {
@@ -161,6 +167,7 @@ impl RelocationIterator {
161167
relocation_iterator,
162168
section_iterator,
163169
object_file,
170+
before_first: true
164171
}
165172
}
166173
}
@@ -169,7 +176,14 @@ impl Iterator for RelocationIterator {
169176
type Item = Relocation;
170177

171178
fn next(&mut self) -> Option<Self::Item> {
172-
// REVIEW: Should it compare against 1? End checking order might also be off
179+
if self.before_first {
180+
self.before_first = false;
181+
} else {
182+
unsafe {
183+
LLVMMoveToNextRelocation(self.relocation_iterator)
184+
}
185+
}
186+
173187
let at_end = unsafe {
174188
LLVMIsRelocationIteratorAtEnd(self.section_iterator, self.relocation_iterator) == 1
175189
};
@@ -180,10 +194,6 @@ impl Iterator for RelocationIterator {
180194

181195
let relocation = Relocation::new(self.relocation_iterator, self.object_file);
182196

183-
unsafe {
184-
LLVMMoveToNextRelocation(self.relocation_iterator)
185-
}
186-
187197
Some(relocation)
188198
}
189199
}
@@ -249,6 +259,7 @@ impl Relocation {
249259
pub struct SymbolIterator {
250260
symbol_iterator: LLVMSymbolIteratorRef,
251261
object_file: LLVMObjectFileRef,
262+
before_first: bool,
252263
}
253264

254265
impl SymbolIterator {
@@ -258,6 +269,7 @@ impl SymbolIterator {
258269
SymbolIterator {
259270
symbol_iterator,
260271
object_file,
272+
before_first: true
261273
}
262274
}
263275
}
@@ -266,7 +278,14 @@ impl Iterator for SymbolIterator {
266278
type Item = Symbol;
267279

268280
fn next(&mut self) -> Option<Self::Item> {
269-
// REVIEW: Should it compare against 1? End checking order might also be off
281+
if self.before_first {
282+
self.before_first = false;
283+
} else {
284+
unsafe {
285+
LLVMMoveToNextSymbol(self.symbol_iterator)
286+
}
287+
}
288+
270289
let at_end = unsafe {
271290
LLVMIsSymbolIteratorAtEnd(self.object_file, self.symbol_iterator) == 1
272291
};
@@ -277,10 +296,6 @@ impl Iterator for SymbolIterator {
277296

278297
let symbol = Symbol::new(self.symbol_iterator);
279298

280-
unsafe {
281-
LLVMMoveToNextSymbol(self.symbol_iterator)
282-
}
283-
284299
Some(symbol)
285300
}
286301
}

0 commit comments

Comments
 (0)