Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segmentation fault on invalid SMARTS #19

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdkit-sys/tests/test_atoms.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[test]
fn test_atoms() {
cxx::let_cxx_string!(smiles = "c1ccccc1CCCCCCCC");
let romol = rdkit_sys::ro_mol_ffi::smiles_to_mol(&smiles).unwrap();
let mut romol = rdkit_sys::ro_mol_ffi::smiles_to_mol(&smiles).unwrap();

let num_atoms = rdkit_sys::ro_mol_ffi::get_num_atoms(&romol, true);

Expand Down
28 changes: 25 additions & 3 deletions src/graphmol/rw_mol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ pub struct RWMol {
pub(crate) ptr: SharedPtr<rdkit_sys::rw_mol_ffi::RWMol>,
}

#[derive(Debug, PartialEq, thiserror::Error)]
pub enum RWMolError {
#[error("Could not convert smarts to RWMol (nullptr)")]
UnknownConversionError,
#[error("could not convert smarts to RWMol (exception)")]
ConversionException(String),
#[error("Could not convert smarts to RWMol (empty smarts)")]
EmptyInputError,
}

impl RWMol {
pub fn from_mol_block(
mol_block: &str,
Expand Down Expand Up @@ -48,11 +58,23 @@ impl RWMol {
ROMol { ptr }
}

pub fn from_smarts(smarts: &str) -> Result<Self, Box<dyn std::error::Error>> {
pub fn from_smarts(smarts: &str) -> Result<Self, RWMolError> {
if smarts.is_empty() {
return Err(RWMolError::EmptyInputError);
}
aleebberg marked this conversation as resolved.
Show resolved Hide resolved
let_cxx_string!(smarts = smarts);

let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts)?;
Ok(RWMol { ptr })
let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts);
match ptr {
Ok(ptr) => {
if ptr.is_null() {
Err(RWMolError::UnknownConversionError)
} else {
Ok(RWMol { ptr })
}
}
Err(e) => Err(RWMolError::ConversionException(e.to_string())),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_atom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[test]
fn test_atom() {
let romol = rdkit::ROMol::from_smiles("C").unwrap();
let mut romol = rdkit::ROMol::from_smiles("C").unwrap();

let atom = romol.atom_with_idx(0);

Expand Down
18 changes: 16 additions & 2 deletions tests/test_graphmol.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rdkit::{
detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters,
MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, SubstructMatchParameters,
TautomerEnumerator, Uncharger,
MolSanitizeException, ROMol, ROMolError, RWMol, RWMolError, SmilesParserParams,
SubstructMatchParameters, TautomerEnumerator, Uncharger,
};

#[test]
Expand Down Expand Up @@ -294,3 +294,17 @@ fn test_building_rwmol_from_smarts() {
let result = substruct_match(&ro_mol, &query_mol, &SubstructMatchParameters::default());
assert_eq!(result.len(), 0);
}

#[test]
fn test_building_rwmol_from_invalid_smarts() {
let smarts = "string";
let rw_mol = RWMol::from_smarts(smarts);
assert_eq!(rw_mol.err(), Some(RWMolError::UnknownConversionError))
}

#[test]
fn test_building_rwmol_from_empty_smarts() {
let smarts = "";
let rw_mol = RWMol::from_smarts(smarts);
assert_eq!(rw_mol.err(), Some(RWMolError::EmptyInputError))
}