Skip to content

Commit 35cda6d

Browse files
authored
Merge pull request #374 from dtolnay/cfgfuzzing
Disable sourcemap thread_local during fuzzing
2 parents eb49335 + da3949d commit 35cda6d

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

src/fallback.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::location::LineColumn;
33
use crate::parse::{self, Cursor};
44
use crate::rcvec::{RcVec, RcVecBuilder, RcVecIntoIter, RcVecMut};
55
use crate::{Delimiter, Spacing, TokenTree};
6-
#[cfg(span_locations)]
6+
#[cfg(all(span_locations, not(fuzzing)))]
77
use core::cell::RefCell;
88
#[cfg(span_locations)]
99
use core::cmp;
@@ -160,7 +160,11 @@ impl TokenStreamBuilder {
160160

161161
#[cfg(span_locations)]
162162
fn get_cursor(src: &str) -> Cursor {
163+
#[cfg(fuzzing)]
164+
return Cursor { rest: src, off: 1 };
165+
163166
// Create a dummy file & add it to the source map
167+
#[cfg(not(fuzzing))]
164168
SOURCE_MAP.with(|cm| {
165169
let mut cm = cm.borrow_mut();
166170
let span = cm.add_file(src);
@@ -331,7 +335,7 @@ impl Debug for SourceFile {
331335
}
332336
}
333337

334-
#[cfg(span_locations)]
338+
#[cfg(all(span_locations, not(fuzzing)))]
335339
thread_local! {
336340
static SOURCE_MAP: RefCell<SourceMap> = RefCell::new(SourceMap {
337341
// NOTE: We start with a single dummy file which all call_site() and
@@ -344,14 +348,14 @@ thread_local! {
344348
});
345349
}
346350

347-
#[cfg(span_locations)]
351+
#[cfg(all(span_locations, not(fuzzing)))]
348352
struct FileInfo {
349353
source_text: String,
350354
span: Span,
351355
lines: Vec<usize>,
352356
}
353357

354-
#[cfg(span_locations)]
358+
#[cfg(all(span_locations, not(fuzzing)))]
355359
impl FileInfo {
356360
fn offset_line_column(&self, offset: usize) -> LineColumn {
357361
assert!(self.span_within(Span {
@@ -384,7 +388,7 @@ impl FileInfo {
384388

385389
/// Computes the offsets of each line in the given source string
386390
/// and the total number of characters
387-
#[cfg(span_locations)]
391+
#[cfg(all(span_locations, not(fuzzing)))]
388392
fn lines_offsets(s: &str) -> (usize, Vec<usize>) {
389393
let mut lines = vec![0];
390394
let mut total = 0;
@@ -399,12 +403,12 @@ fn lines_offsets(s: &str) -> (usize, Vec<usize>) {
399403
(total, lines)
400404
}
401405

402-
#[cfg(span_locations)]
406+
#[cfg(all(span_locations, not(fuzzing)))]
403407
struct SourceMap {
404408
files: Vec<FileInfo>,
405409
}
406410

407-
#[cfg(span_locations)]
411+
#[cfg(all(span_locations, not(fuzzing)))]
408412
impl SourceMap {
409413
fn next_start_pos(&self) -> u32 {
410414
// Add 1 so there's always space between files.
@@ -498,6 +502,12 @@ impl Span {
498502

499503
#[cfg(procmacro2_semver_exempt)]
500504
pub fn source_file(&self) -> SourceFile {
505+
#[cfg(fuzzing)]
506+
return SourceFile {
507+
path: PathBuf::from("<unspecified>"),
508+
};
509+
510+
#[cfg(not(fuzzing))]
501511
SOURCE_MAP.with(|cm| {
502512
let cm = cm.borrow();
503513
let path = cm.filepath(*self);
@@ -507,6 +517,10 @@ impl Span {
507517

508518
#[cfg(span_locations)]
509519
pub fn start(&self) -> LineColumn {
520+
#[cfg(fuzzing)]
521+
return LineColumn { line: 0, column: 0 };
522+
523+
#[cfg(not(fuzzing))]
510524
SOURCE_MAP.with(|cm| {
511525
let cm = cm.borrow();
512526
let fi = cm.fileinfo(*self);
@@ -516,6 +530,10 @@ impl Span {
516530

517531
#[cfg(span_locations)]
518532
pub fn end(&self) -> LineColumn {
533+
#[cfg(fuzzing)]
534+
return LineColumn { line: 0, column: 0 };
535+
536+
#[cfg(not(fuzzing))]
519537
SOURCE_MAP.with(|cm| {
520538
let cm = cm.borrow();
521539
let fi = cm.fileinfo(*self);
@@ -550,6 +568,13 @@ impl Span {
550568

551569
#[cfg(span_locations)]
552570
pub fn join(&self, other: Span) -> Option<Span> {
571+
#[cfg(fuzzing)]
572+
return {
573+
let _ = other;
574+
None
575+
};
576+
577+
#[cfg(not(fuzzing))]
553578
SOURCE_MAP.with(|cm| {
554579
let cm = cm.borrow();
555580
// If `other` is not within the same FileInfo as us, return None.
@@ -570,10 +595,16 @@ impl Span {
570595

571596
#[cfg(span_locations)]
572597
pub fn source_text(&self) -> Option<String> {
573-
if self.is_call_site() {
574-
None
575-
} else {
576-
Some(SOURCE_MAP.with(|cm| cm.borrow().fileinfo(*self).source_text(*self)))
598+
#[cfg(fuzzing)]
599+
return None;
600+
601+
#[cfg(not(fuzzing))]
602+
{
603+
if self.is_call_site() {
604+
None
605+
} else {
606+
Some(SOURCE_MAP.with(|cm| cm.borrow().fileinfo(*self).source_text(*self)))
607+
}
577608
}
578609
}
579610

0 commit comments

Comments
 (0)