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

tagvalue null pointer #33

Open
jblachly opened this issue Nov 2, 2019 · 8 comments
Open

tagvalue null pointer #33

jblachly opened this issue Nov 2, 2019 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@jblachly
Copy link
Member

jblachly commented Nov 2, 2019

bam_aux_get may return nullpointer; ctor checks for this, but the other member functions (to, etc.) do not. they must check for null pointer (or will segfault), or else the ctor should allocate (probably a bad idea)

@jblachly jblachly added the bug Something isn't working label Nov 2, 2019
@jblachly
Copy link
Member Author

currently there are assert(this.data !is null) in each member function (added after this issue raised) , but this is not adequate for production build that will have asserts removed , and variable user data (i.e. a different BAM file) could lead to segfault

@jblachly
Copy link
Member Author

I will note that exists does include a runtime check and returns true/false. Obvoiusly we can recommend that library consumer check this first, but cannot enforce it

How do you feel about exceptions/errors ? ppl writing ultra high performance code tend to avoid , and other parts of our library and codebases are @nothrow

@charlesgregory
Copy link
Contributor

My expectation was that the library user should make the check. In some cases a bam may always have a tag they are looking for. With that in mind we could avoid a branch from the check. Exceptions are nice for opening and closing files with dhtslib but I prefer performance where it can be gained. Though I have a more intimate understanding of some of htslib's inner workings than a user might. We could always make such checks debug only as with D the assumption is that you will test your code in debug mode and things such as array out of bounds checks are removed in production.

@charlesgregory charlesgregory added this to the Version 0.11.2 milestone Apr 19, 2021
charlesgregory added a commit that referenced this issue Apr 21, 2021
@jblachly
Copy link
Member Author

After a lot of thought I want to reopen this. We don't have to fix right away, or even decide right away, but the appearance of a null-pointer which is dependent on specific user input (i.e., the bamfile) is very dangerous and may never be caught by a developer during testing.

As Rust has shown us, safety does not have to come with much or any speed penalty, and overall CPUs these days are excellent at branch prediction

What do you think about wrapping it in Nullable or something along those lines?

@jblachly jblachly reopened this Apr 21, 2021
@charlesgregory
Copy link
Contributor

A fair point, and a decent idea. Will have to find a nullable to use though. I think the dlang Nullable is rather new? I don't want to hold the library back by requiring a high dlang library version requirement.

@jblachly
Copy link
Member Author

Also currently we don't have any external library deps -- only the stdlib -- that is a huge plus IMO.

That being said, there are several nullable implementations

phobos https://dlang.org/phobos/std_typecons.html
vibe.d https://vibed.org/api/taggedalgebraic.taggedalgebraic/TaggedAlgebraic
mir http://mir-core.libmir.org/mir_algebraic.html
optional https://code.dlang.org/packages/optional

charlesgregory added a commit that referenced this issue Apr 21, 2021
… safety, added rc, fixed toString, added more unittesting, addresses #33 though likely causes a performance deficit (should revisit with an Optional type)
@charlesgregory
Copy link
Contributor

Commit 4f5a29c introduces enforce protections for most (all I think) functions with TagValue. This results in exceptions thrown if the data pointer is null or if you are trying to convert the data to the wrong type. We should still consider rewriting with an Optional. Though there is an argument to be made to wrap this into a larger github issue as there are several places where an Optional could be used in dhtslib. At the very least TagValue should be considerably safer.

@charlesgregory
Copy link
Contributor

With the closing of #66 and the prior mentioned commit, TagValue should be considerably safer. I won't close this issue as I don't know if this is the best solution. But I am going to remove it from the v0.12.0 milestone so we can proceed with release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants