From 6af5cb9f38e2664170ea862f692c25e558b3db8b Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 24 Jul 2020 09:18:05 +1000 Subject: [PATCH] strip unwanted control characters when writing fields closes #661 --- rslib/src/notes.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/rslib/src/notes.rs b/rslib/src/notes.rs index 6021cb3e5..23e9f7019 100644 --- a/rslib/src/notes.rs +++ b/rslib/src/notes.rs @@ -24,8 +24,6 @@ use std::{ define_newtype!(NoteID, i64); -// fixme: ensure nulls and x1f not in field contents - #[derive(Default)] pub(crate) struct TransformNoteOutput { pub changed: bool, @@ -88,6 +86,12 @@ impl Note { ))); } + for field in &mut self.fields { + if field.contains(invalid_char_for_field) { + *field = field.replace(invalid_char_for_field, ""); + } + } + if normalize_text { for field in &mut self.fields { ensure_string_in_nfc(field); @@ -226,6 +230,10 @@ fn anki_base91(mut n: u64) -> String { buf.chars().rev().collect() } +fn invalid_char_for_field(c: char) -> bool { + c.is_ascii_control() && c != '\n' && c != '\t' +} + impl Collection { fn canonify_note_tags(&self, note: &mut Note, usn: Usn) -> Result<()> { if !note.tags.is_empty() {