rdkit-rs / rdkit

RDKit Made Idiomatic for Rust
12 stars 9 forks source link

Segmentation fault on invalid SMARTS in RWMol::from_smarts() #18

Open aleebberg opened 7 months ago

aleebberg commented 7 months ago

I am still new to Rust as well as to rdkit, but I am having some problems with the conversion of SMARTS to RWMol. I hope you can help me with this.

I work on Mac and use a normal cargo binary crate. My code is very simple. I pass an invalid SMARTS to RWMol::from_smarts() and try to unwrap it. However, instead of having the code throw an error or panic, the execution gets interrupted and I get a segmentation fault:

let invalid_rwmol = RWMol::from_smarts("abdi###bhd").unwrap(); // just an arbitrary string println!("{:?}", invalid_rwmol);

I first started with matching the Result, but it would always come out as Ok(). When I debugged my code I noted that, although the method returned Ok(), the value of the RWMol pointer was null. However the pointer of the RWMol struct is private to the rdkit-crate so I cannot access it to see, if it is null. Also, since RWMol::from_smarts returns a Result<RWMol, Box>, I dont think it is the expected behavior.

The actual goal of my code was to check if some SMARTS I built are valid by simply creating a Mol-Struct out of them. If the SMARTS is not valid, I wanted to match the Err(). In Python this was easy, as Chem.MolFromSmarts() simply returns None if it wasn't successfull and I could go on form there.

As I am doing this as part of my Master's thesis, I would be very grateful for your help!

aleebberg commented 7 months ago

So I opened a PR to try to fix this problem. @JJ-Pineda I would be very grateful, if you could check it out.