Skip to content

If SerialPort is destroyed before Open has been called, it attempts to close using an uninitialised file descriptor #57

@solbjorg

Description

@solbjorg

See title. A simple fix is just to ensure that fileDesc_ is always initialised, as that avoids invoking any undefined behaviour:

diff --git a/include/CppLinuxSerial/SerialPort.hpp b/include/CppLinuxSerial/SerialPort.hpp
index 2dc2350..afdc4d9 100644
--- a/include/CppLinuxSerial/SerialPort.hpp
+++ b/include/CppLinuxSerial/SerialPort.hpp
@@ -249,7 +249,7 @@ namespace mn {
             SoftwareFlowControl softwareFlowControl_ = SoftwareFlowControl::OFF;

             /// \brief      The file descriptor for the open file. This gets written to when Open() is called.
-            int fileDesc_;
+            int fileDesc_ = -1;

             bool echo_;

This way SerialPort::Close() will just be a no-op if Open() has not been run. (Alternatively the destructor could avoid running Close() if state_ == State::CLOSED.) As far as I can tell this doesn't incidentally change any behaviour elsewhere.

I'll attach a PR with this diff that can be merged if you agree this is a bug. This is definitely an edge case, but it probably should be allowed to create and destroy a SerialPort object without doing anything with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions