mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-11-09 19:10:48 +00:00
a29bfe9c42
Fixes: https://github.com/dankamongmen/notcurses/issues/1009 Whenever a widget is created with its `*_create` function it currently claims full ownership of the passed panel, including its destruction. However, the C++ wrapper around the panel is not aware of this and will attempt to destroy the native panel in the destructor, leading to segfaults. Fix this by introduction of a `Widget` class which contains the logic to properly modify the `Panel` instance to not double-destroy the native panel. The solution is a bit fragile since the `Panel` instance is left intact (we can't free it for the user) in a state that's safe for the C++ wrapper, but calling any C function via the wrapper **will** pass a `NULL` pointer in the panel argument - therefore the C functions MUST be proofed against this. The proofing belongs in the C backend code since this protects also C and other language binding users from such abuse. The Widget class will first verify that the passed `Plane` instance hasn't already been "disowned" and will throw an exception to the effect if it was. Next, it will proceed to take over ownership of the native panel instance and mark the passed `Panel` as "invalid" (i.e. not owning any native panel instance anymore) The above changes require modification of `Panel` instances and so all the widget constructors taking `const*` or `const&` have been removed from widget classes.
100 lines
2.9 KiB
C++
100 lines
2.9 KiB
C++
#ifndef __NCPP_SUBPROC_HH
|
|
#define __NCPP_SUBPROC_HH
|
|
|
|
#include <notcurses/notcurses.h>
|
|
|
|
#include "Root.hh"
|
|
#include "Plane.hh"
|
|
#include "Widget.hh"
|
|
#include "Utilities.hh"
|
|
|
|
namespace ncpp
|
|
{
|
|
class NCPP_API_EXPORT Subproc : public Widget
|
|
{
|
|
public:
|
|
static ncsubproc_options default_options;
|
|
|
|
public:
|
|
explicit Subproc (Plane* plane, const char* bin, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Subproc (plane, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
|
|
{}
|
|
|
|
explicit Subproc (Plane* plane, const char* bin, const ncsubproc_options* opts, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Widget (Utilities::get_notcurses_cpp (plane))
|
|
{
|
|
ensure_valid_plane (plane);
|
|
create_subproc (*plane, bin, opts, use_path, arg, env, cbfxn, donecbfxn);
|
|
take_plane_ownership (plane);
|
|
}
|
|
|
|
explicit Subproc (Plane& plane, const char* bin, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Subproc (plane, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
|
|
{}
|
|
|
|
explicit Subproc (Plane& plane, const char* bin, const ncsubproc_options* opts, bool use_path = true,
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
|
: Widget (Utilities::get_notcurses_cpp (plane))
|
|
{
|
|
ensure_valid_plane (plane);
|
|
create_subproc (plane, bin, opts, use_path, arg, env, cbfxn, donecbfxn);
|
|
take_plane_ownership (plane);
|
|
}
|
|
|
|
~Subproc ()
|
|
{
|
|
if (is_notcurses_stopped ())
|
|
return;
|
|
|
|
ncsubproc_destroy (subproc);
|
|
}
|
|
|
|
Plane* get_plane () const noexcept
|
|
{
|
|
return Plane::map_plane (ncsubproc_plane (subproc));
|
|
}
|
|
|
|
private:
|
|
void create_subproc (Plane& n, const char* bin, const ncsubproc_options* opts, bool use_path,
|
|
char* const arg[], char* const env[],
|
|
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
|
|
{
|
|
if (bin == nullptr)
|
|
throw invalid_argument ("'bin' must be a valid pointer");
|
|
|
|
if (opts == nullptr)
|
|
opts = &default_options;
|
|
|
|
if (use_path) {
|
|
if (env != nullptr) {
|
|
subproc = ncsubproc_createvpe (
|
|
n, opts, bin, arg, env, cbfxn, donecbfxn
|
|
);
|
|
} else {
|
|
subproc = ncsubproc_createvp (
|
|
n, opts, bin, arg, cbfxn, donecbfxn
|
|
);
|
|
}
|
|
} else {
|
|
subproc = ncsubproc_createv (
|
|
n, opts, bin, arg, cbfxn, donecbfxn
|
|
);
|
|
}
|
|
|
|
if (subproc == nullptr)
|
|
throw new init_error ("Notcurses failed to create ncsubproc instance");
|
|
}
|
|
|
|
private:
|
|
ncsubproc *subproc;
|
|
};
|
|
}
|
|
#endif // __NCPP_SUBPROC_HH
|