Skip to content

Commit c2a64c6

Browse files
committed
Use non-fallible conversion in display page to create string of contents
1 parent d15fbc8 commit c2a64c6

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
before starting web server
2525
- This feels a bit nicer because there's no need to reference count a type
2626
which must live for the entire program anyways.
27+
- Don't potentially try to create a string with `usize::MAX` capacity when
28+
displaying a file
29+
- Pretty sure that since all relevant (>= 32 bits) platforms can fit 1048576
30+
in a usize, that this bug would never actually happen. But it shouldn't
31+
exist in the code.
2732

2833
## [v0.2.12] - 2024-11-04
2934

bobashare-web/src/views/display.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ pub async fn display(
140140
.syntax_set
141141
.find_syntax_by_extension(extension)
142142
.unwrap_or_else(|| state.syntax_set.find_syntax_plain_text());
143-
let mut contents = String::with_capacity(size.try_into().unwrap_or(usize::MAX));
143+
// should be alright to assume that 1,048,576 fits in usize on relevant
144+
// platforms
145+
let mut contents = String::with_capacity(size as usize);
144146
upload
145147
.file
146148
.read_to_string(&mut contents)

0 commit comments

Comments
 (0)