Skip to content

Commit

Permalink
Prevent infinite loop in citation rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Jun 9, 2019
1 parent d735a5b commit 2398475
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/data/citation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::syntax::bibtex::BibtexSyntaxTree;
use lsp_types::*;
use std::borrow::Cow;
use std::fs;
Expand All @@ -7,7 +8,7 @@ use tempfile::tempdir;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CitationError {
WriteFailed,
InitializationFailed,
InvalidEntry,
NodeNotInstalled,
ScriptFaulty,
Expand All @@ -25,9 +26,14 @@ impl<'a> Citation<'a> {
}

pub fn render(&self) -> Result<MarkupContent, CitationError> {
let directory = tempdir().map_err(|_| CitationError::WriteFailed)?;
let entry = BibtexSyntaxTree::from(self.entry_code);
if entry.entries().iter().any(|entry| entry.fields.len() == 0) {
return Err(CitationError::InvalidEntry);
}

let directory = tempdir().map_err(|_| CitationError::InitializationFailed)?;
let entry_path = directory.path().join("entry.bib");
fs::write(entry_path, self.entry_code).map_err(|_| CitationError::WriteFailed)?;
fs::write(entry_path, self.entry_code).map_err(|_| CitationError::InitializationFailed)?;

let mut process = Command::new("node")
.stdin(Stdio::piped())
Expand Down Expand Up @@ -80,4 +86,10 @@ mod tests {
let citation = Citation::new("@article{}");
assert_eq!(citation.render(), Err(CitationError::InvalidEntry));
}

#[test]
fn test_empty() {
let citation = Citation::new("@article{foo,}");
assert_eq!(citation.render(), Err(CitationError::InvalidEntry));
}
}
2 changes: 1 addition & 1 deletion tests/synchronization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use futures::executor::block_on;
use jsonrpc::server::ActionHandler;
use lsp_types::*;
use texlab::scenario::Scenario;
use std::sync::Arc;
use texlab::scenario::Scenario;

async fn run_completion(
scenario: &Scenario,
Expand Down

0 comments on commit 2398475

Please sign in to comment.