Skip to content

Defensive check for sd_bus_error_set_errno(…, 0) to avoid segfaults on empty error name #516

Description

@JesseRiemens

Describe the bug

Calling sdbus::Error createError(int errNo, std::string customMsg) from src/Error.cpp with an errno value of 0 causes an empty error name to be propagated.

According to the Debian sd_bus_error_set_errno manpage:

sd_bus_error_set_errno() will immediately return 0 if the specified error parameter error is 0. Otherwise, it will set name from an errno-like value that is converted to a D-Bus error. strerror_r(3) will be used to set message. Well-known D-Bus error names will be used for name if applicable, otherwise a name in the "System.Error." namespace will be generated. The sign of the specified error number is ignored and the absolute value is used implicitly. If the specified error error is non-zero, the call always returns a negative value, for convenient usage in return statements. This call might fail due to lack of memory, in which case an SD_BUS_ERROR_NO_MEMORY error is set instead, and -ENOMEM is returned.

Specifically, if errno == 0, the error name is left as null, which means that the resulting sdbus::Error object has an empty name. If this flows through to error reply handlers (e.g. Connection::createErrorReplyMessage), it may result in a crash or assertion failure, because a non-empty error name is required by D-Bus and sd_bus_error_set.

To Reproduce

  • Call or throw an sdbus-cpp error via:
    throw sdbus::createError(0, "Should not happen");
  • The empty error name propagates through the library and can cause a segfault, as observed in my case.

Expected behavior
The library should guard against this misuse, for example:

  • By immediately returning or doing nothing if errno is 0 (mimicking the C API),
  • Or by throwing a developer-friendly exception/warning when an error with errno 0 is about to be emitted, instead of letting the crash propagate downstream.

Actual behavior
Currently, an empty D-Bus error name is emitted, leading to a segfault or assertion failure (undefined behavior).

Additional context
See: src/Error.cpp
See: Debian manpage - sd_bus_error_set_errno

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions