-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
u16
instead of usize
for line width
Using an `usize` for the line width causes problems for the optimal-fit wrapping algorithm. The problem is that the algorithm works with integer values formed by (line_width - text_width)**2 When `line_width` is near `usize::max_value()`, this computation will overflow an `usize`. By limiting the line width to an `u16`, we can do the internal computations with `u64` and avoid the overflows.
- Loading branch information
Showing
13 changed files
with
302 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#![no_main] | ||
use libfuzzer_sys::fuzz_target; | ||
use textwrap::core; | ||
use textwrap::wrap_algorithms::wrap_first_fit; | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
struct Word(u16); | ||
|
||
impl core::Fragment for Word { | ||
fn width(&self) -> u16 { | ||
self.0 | ||
} | ||
fn whitespace_width(&self) -> u16 { | ||
1 | ||
} | ||
fn penalty_width(&self) -> u16 { | ||
0 | ||
} | ||
} | ||
|
||
fuzz_target!(|input: (u16, Vec<u16>)| { | ||
let width = input.0; | ||
let word_widths = input.1; | ||
// The words here can easily have a combined width which doesn't | ||
// fit in an u16. | ||
let words = word_widths | ||
.iter() | ||
.map(|&width| Word(width)) | ||
.collect::<Vec<Word>>(); | ||
let _ = wrap_first_fit(&words, &[width]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#![no_main] | ||
use arbitrary::Arbitrary; | ||
use libfuzzer_sys::fuzz_target; | ||
use textwrap::core; | ||
use textwrap::wrap_algorithms::{wrap_optimal_fit, OptimalFit}; | ||
|
||
#[derive(Arbitrary, Debug)] | ||
struct Penalties { | ||
nline_penalty: u32, | ||
overflow_penalty: u32, | ||
short_last_line_fraction: u64, | ||
short_last_line_penalty: u32, | ||
hyphen_penalty: u32, | ||
} | ||
|
||
impl Into<OptimalFit> for Penalties { | ||
fn into(self) -> OptimalFit { | ||
OptimalFit { | ||
nline_penalty: self.nline_penalty, | ||
overflow_penalty: self.overflow_penalty, | ||
short_last_line_fraction: std::cmp::max(1, self.short_last_line_fraction), | ||
short_last_line_penalty: self.short_last_line_penalty, | ||
hyphen_penalty: self.hyphen_penalty, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Arbitrary, Debug, Eq, PartialEq)] | ||
struct Word { | ||
width: u16, | ||
whitespace_width: u16, | ||
penalty_width: u16, | ||
} | ||
|
||
impl core::Fragment for Word { | ||
fn width(&self) -> u16 { | ||
self.width | ||
} | ||
fn whitespace_width(&self) -> u16 { | ||
self.whitespace_width | ||
} | ||
fn penalty_width(&self) -> u16 { | ||
self.penalty_width | ||
} | ||
} | ||
|
||
fuzz_target!(|input: (u16, Vec<Word>, Penalties)| { | ||
let width = input.0; | ||
// The words here have a combined width much longer than an u16. | ||
let words = input.1; | ||
let penalties = input.2.into(); | ||
|
||
let _ = wrap_optimal_fit(&words, &[width], &penalties); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.