Prevent OOM abort when printing large strings

jmap
Manos Pitsidianakis 2019-11-22 14:17:09 +02:00
parent 424b244bb7
commit f3c938d8c3
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 23 additions and 2 deletions

View File

@ -7,3 +7,16 @@ pub mod wcwidth;
pub use grapheme_clusters::*; pub use grapheme_clusters::*;
pub use line_break::*; pub use line_break::*;
pub use wcwidth::*; pub use wcwidth::*;
pub trait Truncate {
fn truncate_at_boundary(self, new_len: usize);
}
impl Truncate for &mut String {
fn truncate_at_boundary(self, mut new_len: usize) {
while new_len > 0 && !self.is_char_boundary(new_len) {
new_len -= 1;
}
String::truncate(self, new_len);
}
}

View File

@ -172,6 +172,11 @@ impl CellBuffer {
self.rows = newrows; self.rows = newrows;
return; return;
} }
if newlen >= 200_000 {
return;
}
let mut newbuf: Vec<Cell> = Vec::with_capacity(newlen); let mut newbuf: Vec<Cell> = Vec::with_capacity(newlen);
for y in 0..newrows { for y in 0..newrows {
for x in 0..newcols { for x in 0..newcols {

View File

@ -8,6 +8,7 @@ use melib::async_workers::{Work, WorkContext};
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;
use std::thread; use std::thread;
use text_processing::Truncate;
const MAX_WORKER: usize = 4; const MAX_WORKER: usize = 4;
@ -235,7 +236,8 @@ impl WorkController {
} }
} }
recv(set_name_rx) -> new_name => { recv(set_name_rx) -> new_name => {
if let Ok((thread_id, new_name)) = new_name { if let Ok((thread_id, mut new_name)) = new_name {
new_name.truncate_at_boundary(256);
let mut threads = threads_lock.lock().unwrap(); let mut threads = threads_lock.lock().unwrap();
let mut static_threads = _static_threads_lock.lock().unwrap(); let mut static_threads = _static_threads_lock.lock().unwrap();
if threads.contains_key(&thread_id) { if threads.contains_key(&thread_id) {
@ -249,7 +251,8 @@ impl WorkController {
} }
} }
recv(set_status_rx) -> new_status => { recv(set_status_rx) -> new_status => {
if let Ok((thread_id, new_status)) = new_status { if let Ok((thread_id, mut new_status)) = new_status {
new_status.truncate_at_boundary(256);
let mut threads = threads_lock.lock().unwrap(); let mut threads = threads_lock.lock().unwrap();
let mut static_threads = _static_threads_lock.lock().unwrap(); let mut static_threads = _static_threads_lock.lock().unwrap();
if threads.contains_key(&thread_id) { if threads.contains_key(&thread_id) {