Skip to content

MSSpectrum::get...DataArrayByName segfaults if name not found #3002

Closed

Description

@hendrikweisser

See e.g. the implementation here:

return *std::find_if(integer_data_arrays_.begin(), integer_data_arrays_.end(),

The result of find_if always gets dereferenced, even if it's the past-the-end iterator (if name wasn't found).

Activity

  1. enetz commented on Nov 13, 2017

    @enetz
    Contributor

    first some context, this was implemented and discussed here #2847 and then here #2855 .
    @timosachsenberg @hendrikweisser should we implement has...DataArrayWithName() functions? would that help?

  2. hroest commented on Nov 13, 2017

    @hroest
    Contributor

    I am not sure how the iterator solution is a good idea as it exposes the internal workings to the user and at this point we might as well give the user raw access to the data structure and ask them to use find themselves. I suggest the interface should be

    void hasIntegerDataArray(const String& name);
    // may throw InvalidArgumentException, you need to check hasIntegerDataArray first
    IntegerDataArray& getIntegerDataArray(const String& name);
    

    what would the alternative interface look like?

    // may return invalid iterator, you need to compare against getIntegerDataArrayEndIterator
    IntegerDataArray::iterator getIntegerDataArray(const String& name);
    IntegerDataArray::iterator getIntegerDataArrayEndIterator(const String& name);
    

    now I think the first interface is clearer and it is easier to check that people adhere to it.

  3. hroest commented on Nov 13, 2017

    @hroest
    Contributor

    I am not sure how the iterator solution is a good idea as it exposes the internal workings to the user and at this point we might as well give the user raw access to the data structure and ask them to use find themselves. I suggest the interface should be

    void hasIntegerDataArray(const String& name);
    // may throw InvalidArgumentException, you need to check hasIntegerDataArray first
    IntegerDataArray& getIntegerDataArray(const String& name);
    

    what would the alternative interface look like?

    // may return invalid iterator, you need to compare against getIntegerDataArrayEndIterator
    IntegerDataArray::iterator getIntegerDataArray(const String& name);
    IntegerDataArray::iterator getIntegerDataArrayEndIterator(const String& name);
    

    now I think the first interface is clearer and it is easier to check that people adhere to it. In either case, the current interface is pretty messy and contains multiple ways to access the data, either by getting a single array or by getting all of them ...

    const IntegerDataArrays& getIntegerDataArrays() const;
    IntegerDataArrays& getIntegerDataArrays();
    void setIntegerDataArrays(const IntegerDataArrays& ida);
    inline IntegerDataArray& getIntegerDataArrayByName(String name)
    inline const IntegerDataArray& getIntegerDataArrayByName(String name) const
    
  4. hendrikweisser commented on Nov 13, 2017

    @hendrikweisser
    ContributorAuthor
    void hasIntegerDataArray(const String& name);
    // may throw InvalidArgumentException, you need to check hasIntegerDataArray first
    IntegerDataArray& getIntegerDataArray(const String& name);
    

    The problem that I have with this is that the look-up has to happen twice - first to check if the name exists, then again to return the array. What's wrong with returning an iterator and then checking?
    if (spectrum.getIntegerDataArrayByName(name) != spectrum.getIntegerDataArrays().end())

  5. hendrikweisser commented on Nov 13, 2017

    @hendrikweisser
    ContributorAuthor

    Or we could be old-school and return a pointer that may be zero...

  6. timosachsenberg commented on Nov 13, 2017

    @timosachsenberg
    Contributor

    The pointer has the nice property that it could also be passed to (e.g. scoring) methods that may use this information (e.g., an annotated charge array).
    Downside is, that reallocation invalidates these pointers.
    Another alternative would be returning an index or -1

  7. added
    major
    on Jul 27, 2018
  8. timosachsenberg commented on Jul 14, 2026

    @timosachsenberg
    Contributor

    Resolved on develop: commit 61cd113 removed the unsafe MSSpectrum::get*DataArrayByName getters, so a missing name can no longer be dereferenced through this API. The current API exposes the data-array containers for explicit checked lookup. Closing as completed.

    61cd11352b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

OpenMS - librarydefectmajor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions