[winpr,collections] fix PubSub_OnEvent return checks

* proper return checks on use
* fix return on invalid input arguments
* fix return on no event registered
This commit is contained in:
Armin Novak
2026-03-02 13:17:00 +01:00
parent 32b2bd22aa
commit 17163d3738
16 changed files with 152 additions and 58 deletions

View File

@@ -592,12 +592,14 @@ static UINT rdpgfx_recv_reset_graphics_pdu(GENERIC_CHANNEL_CALLBACK* callback, w
error);
}
free(pdu.monitorDefArray);
/* some listeners may be interested (namely the display channel) */
EventArgsInit(&graphicsReset, "libfreerdp");
graphicsReset.width = pdu.width;
graphicsReset.height = pdu.height;
PubSub_OnGraphicsReset(gfx->rdpcontext->pubSub, gfx->rdpcontext, &graphicsReset);
free(pdu.monitorDefArray);
if (PubSub_OnGraphicsReset(gfx->rdpcontext->pubSub, gfx->rdpcontext, &graphicsReset) < 0)
return ERROR_INTERNAL_ERROR;
return error;
}

View File

@@ -91,7 +91,9 @@ static CGContextRef mac_create_bitmap_context(rdpContext *context);
EventArgsInit(&e, "mfreerdp");
e.embed = TRUE;
e.handle = (void *)self;
PubSub_OnEmbedWindow(context->pubSub, context, &e);
if (PubSub_OnEmbedWindow(context->pubSub, context, &e) < 0)
return -1;
NSScreen *screen = [[NSScreen screens] objectAtIndex:0];
NSRect screenFrame = [screen frame];
@@ -1495,7 +1497,8 @@ BOOL mac_desktop_resize(rdpContext *context)
EventArgsInit(&e, "mfreerdp");
e.width = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
e.height = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
PubSub_OnResizeWindow(context->pubSub, context, &e);
if (PubSub_OnResizeWindow(context->pubSub, context, &e) < 0)
return FALSE;
return TRUE;
}

View File

@@ -477,7 +477,9 @@ static BOOL wf_post_connect(freerdp* instance)
EventArgsInit(&e, "wfreerdp");
e.embed = FALSE;
e.handle = (void*)wfc->hwnd;
PubSub_OnEmbedWindow(context->pubSub, context, &e);
if (PubSub_OnEmbedWindow(context->pubSub, context, &e) < 0)
return FALSE;
#ifdef WITH_PROGRESS_BAR
if (wfc->taskBarList)
{

View File

@@ -927,8 +927,7 @@ static BOOL wf_pub_mouse_event(wfContext* wfc, UINT16 flags, UINT16 x, UINT16 y)
eventArgs.flags = flags;
eventArgs.x = x;
eventArgs.y = y;
PubSub_OnMouseEvent(wfc->common.context.pubSub, &wfc->common.context, &eventArgs);
return TRUE;
return PubSub_OnMouseEvent(wfc->common.context.pubSub, &wfc->common.context, &eventArgs) >= 0;
}
static BOOL wf_scale_mouse_event(wfContext* wfc, UINT16 flags, INT32 x, INT32 y)

View File

@@ -826,7 +826,10 @@ void xf_toggle_fullscreen(xfContext* xfc)
xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
EventArgsInit(&e, "xfreerdp");
e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
PubSub_OnWindowStateChange(context->pubSub, context, &e);
if (PubSub_OnWindowStateChange(context->pubSub, context, &e) < 0)
{
WLog_Print(xfc->log, WLOG_ERROR, "PubSub_OnWindowStateChange failed");
}
}
void xf_minimize(xfContext* xfc)
@@ -845,7 +848,10 @@ void xf_minimize(xfContext* xfc)
xf_SetWindowMinimized(xfc, xfc->window);
e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
PubSub_OnWindowStateChange(context->pubSub, context, &e);
if (PubSub_OnWindowStateChange(context->pubSub, context, &e) < 0)
{
WLog_Print(xfc->log, WLOG_ERROR, "PubSub_OnWindowStateChange failed");
}
}
void xf_lock_x11_(xfContext* xfc, WINPR_ATTR_UNUSED const char* fkt)
@@ -1471,8 +1477,7 @@ static BOOL xf_post_connect(freerdp* instance)
WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
e.height =
WINPR_ASSERTING_INT_CAST(int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
PubSub_OnResizeWindow(context->pubSub, xfc, &e);
return TRUE;
return PubSub_OnResizeWindow(context->pubSub, xfc, &e) >= 0;
}
static void xf_post_disconnect(freerdp* instance)

View File

@@ -1369,7 +1369,10 @@ BOOL xf_event_process(freerdp* instance, const XEvent* event)
xf_cliprdr_handle_xevent(xfc, event);
if (!xf_floatbar_check_event(floatbar, event) && !xf_floatbar_is_locked(floatbar))
xf_input_handle_event(xfc, event);
{
if (xf_input_handle_event(xfc, event) < 0)
return FALSE;
}
LogDynAndXSync(xfc->log, xfc->display, FALSE);
return status;

View File

@@ -321,7 +321,8 @@ static void xf_input_save_last_event(xfContext* xfc, const XGenericEventCookie*
xfc->lastEvent.event_y = event->event_y;
}
static void xf_input_detect_pan(xfContext* xfc)
WINPR_ATTR_NODISCARD
static BOOL xf_input_detect_pan(xfContext* xfc)
{
WINPR_ASSERT(xfc);
rdpContext* ctx = &xfc->common.context;
@@ -329,7 +330,7 @@ static void xf_input_detect_pan(xfContext* xfc)
if (xfc->active_contacts != 2)
{
return;
return TRUE;
}
const double dx[] = { xfc->contacts[0].pos_x - xfc->contacts[0].last_x,
@@ -352,7 +353,8 @@ static void xf_input_detect_pan(xfContext* xfc)
EventArgsInit(&e, "xfreerdp");
e.dx = 5;
e.dy = 0;
PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e) < 0)
return FALSE;
}
xfc->px_vector = 0;
xfc->py_vector = 0;
@@ -365,7 +367,8 @@ static void xf_input_detect_pan(xfContext* xfc)
EventArgsInit(&e, "xfreerdp");
e.dx = -5;
e.dy = 0;
PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e) < 0)
return FALSE;
}
xfc->px_vector = 0;
xfc->py_vector = 0;
@@ -382,7 +385,8 @@ static void xf_input_detect_pan(xfContext* xfc)
EventArgsInit(&e, "xfreerdp");
e.dx = 0;
e.dy = 5;
PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e) < 0)
return FALSE;
}
xfc->py_vector = 0;
xfc->px_vector = 0;
@@ -395,16 +399,19 @@ static void xf_input_detect_pan(xfContext* xfc)
EventArgsInit(&e, "xfreerdp");
e.dx = 0;
e.dy = -5;
PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e) < 0)
return FALSE;
}
xfc->py_vector = 0;
xfc->px_vector = 0;
xfc->z_vector = 0;
}
}
return TRUE;
}
static void xf_input_detect_pinch(xfContext* xfc)
WINPR_ATTR_NODISCARD
static BOOL xf_input_detect_pinch(xfContext* xfc)
{
ZoomingChangeEventArgs e = WINPR_C_ARRAY_INIT;
@@ -415,7 +422,7 @@ static void xf_input_detect_pinch(xfContext* xfc)
if (xfc->active_contacts != 2)
{
xfc->firstDist = -1.0;
return;
return TRUE;
}
/* first calculate the distance */
@@ -449,7 +456,8 @@ static void xf_input_detect_pinch(xfContext* xfc)
{
EventArgsInit(&e, "xfreerdp");
e.dx = e.dy = -10;
PubSub_OnZoomingChange(ctx->pubSub, xfc, &e);
if (PubSub_OnZoomingChange(ctx->pubSub, xfc, &e) < 0)
return FALSE;
xfc->z_vector = 0;
xfc->px_vector = 0;
xfc->py_vector = 0;
@@ -459,12 +467,14 @@ static void xf_input_detect_pinch(xfContext* xfc)
{
EventArgsInit(&e, "xfreerdp");
e.dx = e.dy = 10;
PubSub_OnZoomingChange(ctx->pubSub, xfc, &e);
if (PubSub_OnZoomingChange(ctx->pubSub, xfc, &e) < 0)
return FALSE;
xfc->z_vector = 0;
xfc->px_vector = 0;
xfc->py_vector = 0;
}
}
return TRUE;
}
static void xf_input_touch_begin(xfContext* xfc, const XIDeviceEvent* event)
@@ -484,7 +494,8 @@ static void xf_input_touch_begin(xfContext* xfc, const XIDeviceEvent* event)
}
}
static void xf_input_touch_update(xfContext* xfc, const XIDeviceEvent* event)
WINPR_ATTR_NODISCARD
static BOOL xf_input_touch_update(xfContext* xfc, const XIDeviceEvent* event)
{
WINPR_ASSERT(xfc);
WINPR_ASSERT(event);
@@ -498,11 +509,15 @@ static void xf_input_touch_update(xfContext* xfc, const XIDeviceEvent* event)
xfc->contacts[i].last_y = xfc->contacts[i].pos_y;
xfc->contacts[i].pos_x = event->event_x;
xfc->contacts[i].pos_y = event->event_y;
xf_input_detect_pinch(xfc);
xf_input_detect_pan(xfc);
if (!xf_input_detect_pinch(xfc))
return FALSE;
if (!xf_input_detect_pan(xfc))
return FALSE;
break;
}
}
return TRUE;
}
static void xf_input_touch_end(xfContext* xfc, const XIDeviceEvent* event)
@@ -543,7 +558,10 @@ static int xf_input_handle_event_local(xfContext* xfc, const XEvent* event)
case XI_TouchUpdate:
if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
xf_input_touch_update(xfc, cookie.cc->data);
{
if (!xf_input_touch_update(xfc, cookie.cc->data))
return -1;
}
xf_input_save_last_event(xfc, cookie.cc);
break;

View File

@@ -27,8 +27,13 @@
#include <X11/extensions/XInput2.h>
#endif
WINPR_ATTR_NODISCARD
int xf_input_init(xfContext* xfc, Window window);
WINPR_ATTR_NODISCARD
int xf_input_handle_event(xfContext* xfc, const XEvent* event);
WINPR_ATTR_NODISCARD
bool xf_use_rel_mouse(xfContext* xfc);
#endif /* FREERDP_CLIENT_X11_INPUT_H */

View File

@@ -1466,7 +1466,8 @@ BOOL xf_keyboard_handle_special_keys(xfContext* xfc, KeySym keysym)
EventArgsInit(&e, "xfreerdp");
e.dx = pdx;
e.dy = pdy;
PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e)<0)
return FALSE;
return TRUE;
}
@@ -1476,7 +1477,8 @@ BOOL xf_keyboard_handle_special_keys(xfContext* xfc, KeySym keysym)
EventArgsInit(&e, "xfreerdp");
e.dx = zdx;
e.dy = zdy;
PubSub_OnZoomingChange(ctx->pubSub, xfc, &e);
if (PubSub_OnZoomingChange(ctx->pubSub, xfc, &e)<0)
return FALSE;
return TRUE;
}
}

View File

@@ -223,12 +223,21 @@ static UINT freerdp_drdynvc_on_channel_connected(DrdynvcClientContext* context,
{
UINT status = CHANNEL_RC_OK;
ChannelConnectedEventArgs e = WINPR_C_ARRAY_INIT;
WINPR_ASSERT(context);
rdpChannels* channels = (rdpChannels*)context->custom;
WINPR_ASSERT(channels);
freerdp* instance = channels->instance;
WINPR_ASSERT(instance);
WINPR_ASSERT(instance->context);
EventArgsInit(&e, "freerdp");
e.name = name;
e.pInterface = pInterface;
PubSub_OnChannelConnected(instance->context->pubSub, instance->context, &e);
if (PubSub_OnChannelConnected(instance->context->pubSub, instance->context, &e) < 0)
return ERROR_INTERNAL_ERROR;
return status;
}
@@ -242,12 +251,20 @@ static UINT freerdp_drdynvc_on_channel_disconnected(DrdynvcClientContext* contex
{
UINT status = CHANNEL_RC_OK;
ChannelDisconnectedEventArgs e = WINPR_C_ARRAY_INIT;
WINPR_ASSERT(context);
rdpChannels* channels = (rdpChannels*)context->custom;
WINPR_ASSERT(channels);
freerdp* instance = channels->instance;
WINPR_ASSERT(instance);
WINPR_ASSERT(instance->context);
EventArgsInit(&e, "freerdp");
e.name = name;
e.pInterface = pInterface;
PubSub_OnChannelDisconnected(instance->context->pubSub, instance->context, &e);
if (PubSub_OnChannelDisconnected(instance->context->pubSub, instance->context, &e) < 0)
return ERROR_INTERNAL_ERROR;
return status;
}
@@ -256,12 +273,20 @@ static UINT freerdp_drdynvc_on_channel_attached(DrdynvcClientContext* context, c
{
UINT status = CHANNEL_RC_OK;
ChannelAttachedEventArgs e = WINPR_C_ARRAY_INIT;
WINPR_ASSERT(context);
rdpChannels* channels = (rdpChannels*)context->custom;
WINPR_ASSERT(channels);
freerdp* instance = channels->instance;
WINPR_ASSERT(instance);
WINPR_ASSERT(instance->context);
EventArgsInit(&e, "freerdp");
e.name = name;
e.pInterface = pInterface;
PubSub_OnChannelAttached(instance->context->pubSub, instance->context, &e);
if (PubSub_OnChannelAttached(instance->context->pubSub, instance->context, &e) < 0)
return ERROR_INTERNAL_ERROR;
return status;
}
@@ -270,12 +295,20 @@ static UINT freerdp_drdynvc_on_channel_detached(DrdynvcClientContext* context, c
{
UINT status = CHANNEL_RC_OK;
ChannelDetachedEventArgs e = WINPR_C_ARRAY_INIT;
WINPR_ASSERT(context);
rdpChannels* channels = (rdpChannels*)context->custom;
WINPR_ASSERT(channels);
freerdp* instance = channels->instance;
WINPR_ASSERT(instance);
WINPR_ASSERT(instance->context);
EventArgsInit(&e, "freerdp");
e.name = name;
e.pInterface = pInterface;
PubSub_OnChannelDetached(instance->context->pubSub, instance->context, &e);
if (PubSub_OnChannelDetached(instance->context->pubSub, instance->context, &e) < 0)
return ERROR_INTERNAL_ERROR;
return status;
}
@@ -363,14 +396,19 @@ UINT freerdp_channels_attach(freerdp* instance)
CHANNEL_EVENT_ATTACHED, cnv.pv, (UINT)hostnameLength);
}
if (getChannelError(instance->context) != CHANNEL_RC_OK)
error = getChannelError(instance->context);
if (error != CHANNEL_RC_OK)
goto fail;
pChannelOpenData = &channels->openDataList[index];
EventArgsInit(&e, "freerdp");
e.name = pChannelOpenData->name;
e.pInterface = pChannelOpenData->pInterface;
PubSub_OnChannelAttached(instance->context->pubSub, instance->context, &e);
if (PubSub_OnChannelAttached(instance->context->pubSub, instance->context, &e) < 0)
{
error = ERROR_INTERNAL_ERROR;
goto fail;
}
}
fail:
@@ -426,14 +464,19 @@ UINT freerdp_channels_detach(freerdp* instance)
CHANNEL_EVENT_DETACHED, cnv.pv, (UINT)hostnameLength);
}
if (getChannelError(context) != CHANNEL_RC_OK)
error = getChannelError(context);
if (error != CHANNEL_RC_OK)
goto fail;
pChannelOpenData = &channels->openDataList[index];
EventArgsInit(&e, "freerdp");
e.name = pChannelOpenData->name;
e.pInterface = pChannelOpenData->pInterface;
PubSub_OnChannelDetached(context->pubSub, context, &e);
if (PubSub_OnChannelDetached(context->pubSub, context, &e) < 0)
{
error = ERROR_INTERNAL_ERROR;
goto fail;
}
}
fail:
@@ -495,7 +538,11 @@ UINT freerdp_channels_post_connect(rdpChannels* channels, freerdp* instance)
EventArgsInit(&e, "freerdp");
e.name = pChannelOpenData->name;
e.pInterface = pChannelOpenData->pInterface;
PubSub_OnChannelConnected(instance->context->pubSub, instance->context, &e);
if (PubSub_OnChannelConnected(instance->context->pubSub, instance->context, &e) < 0)
{
error = ERROR_INTERNAL_ERROR;
goto fail;
}
}
channels->drdynvc = (DrdynvcClientContext*)freerdp_channels_get_static_channel_interface(
@@ -889,7 +936,8 @@ UINT freerdp_channels_disconnect(rdpChannels* channels, freerdp* instance)
EventArgsInit(&e, "freerdp");
e.name = pChannelOpenData->name;
e.pInterface = pChannelOpenData->pInterface;
PubSub_OnChannelDisconnected(instance->context->pubSub, instance->context, &e);
if (PubSub_OnChannelDisconnected(instance->context->pubSub, instance->context, &e) < 0)
error = ERROR_INTERNAL_ERROR;
}
channels->connected = FALSE;

View File

@@ -1419,7 +1419,8 @@ BOOL rdp_client_transition_to_state(rdpRdp* rdp, CONNECTION_STATE state)
EventArgsInit(&activatedEvent, "libfreerdp");
activatedEvent.firstActivation =
!rdp_finalize_is_flag_set(rdp, FINALIZE_DEACTIVATE_REACTIVATE);
PubSub_OnActivated(rdp->pubSub, context, &activatedEvent);
if (PubSub_OnActivated(rdp->pubSub, context, &activatedEvent) < 0)
return FALSE;
}
break;
@@ -1434,7 +1435,8 @@ BOOL rdp_client_transition_to_state(rdpRdp* rdp, CONNECTION_STATE state)
EventArgsInit(&stateEvent, "libfreerdp");
stateEvent.state = WINPR_ASSERTING_INT_CAST(int32_t, rdp_get_state(rdp));
stateEvent.active = rdp_is_active_state(rdp);
PubSub_OnConnectionStateChange(rdp->pubSub, context, &stateEvent);
if (PubSub_OnConnectionStateChange(rdp->pubSub, context, &stateEvent) < 0)
return FALSE;
}
return TRUE;

View File

@@ -303,7 +303,8 @@ BOOL freerdp_connect(freerdp* instance)
freerdp_connect_finally:
EventArgsInit(&e, "freerdp");
e.result = status ? 0 : -1;
PubSub_OnConnectionResult(rdp->pubSub, instance->context, &e);
if (PubSub_OnConnectionResult(rdp->pubSub, instance->context, &e) < 0)
return FALSE;
if (!status)
freerdp_disconnect(instance);
@@ -373,7 +374,8 @@ BOOL freerdp_check_fds(freerdp* instance)
WLog_Print(context->log, WLOG_DEBUG, "rdp_check_fds() - %i", status);
EventArgsInit(&e, "freerdp");
e.code = 0;
PubSub_OnTerminate(rdp->pubSub, context, &e);
if (PubSub_OnTerminate(rdp->pubSub, context, &e) < 0)
return FALSE;
return FALSE;
}

View File

@@ -432,8 +432,7 @@ static int rpc_client_recv_fragment(rdpRpc* rpc, wStream* fragment)
tsg_set_state(tsg, TSG_STATE_TUNNEL_CLOSE_PENDING);
EventArgsInit(&e, "freerdp");
e.code = 0;
PubSub_OnTerminate(context->rdp->pubSub, context, &e);
rc = 0;
rc = PubSub_OnTerminate(context->rdp->pubSub, context, &e) >= 0 ? 0 : -1;
goto success;
}

View File

@@ -641,8 +641,7 @@ BOOL rdp_read_header(rdpRdp* rdp, wStream* s, UINT16* length, UINT16* channelId)
utils_abort_connect(rdp);
EventArgsInit(&e, "freerdp");
e.code = 0;
PubSub_OnTerminate(rdp->pubSub, context, &e);
return TRUE;
return PubSub_OnTerminate(rdp->pubSub, context, &e) >= 0;
}
if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 5))

View File

@@ -23,6 +23,9 @@
#include <winpr/collections.h>
#include "../log.h"
#define TAG WINPR_TAG("pubsub")
/**
* Events (C# Programming Guide)
* http://msdn.microsoft.com/en-us/library/awbftdfh.aspx
@@ -196,25 +199,23 @@ int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, ...)
int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context, const wEventArgs* e)
{
wEventType* event = nullptr;
int status = -1;
WINPR_ASSERT(pubSub);
WINPR_ASSERT(EventName);
WINPR_ASSERT(e);
if (!pubSub)
{
WLog_ERR(TAG, "pubSub(%s)=nullptr!", EventName);
return -1;
WINPR_ASSERT(e);
}
if (pubSub->synchronized)
PubSub_Lock(pubSub);
event = PubSub_FindEventType(pubSub, EventName);
if (pubSub->synchronized)
PubSub_Unlock(pubSub);
int status = 0;
wEventType* event = PubSub_FindEventType(pubSub, EventName);
if (event)
{
status = 0;
for (size_t index = 0; index < event->EventHandlerCount; index++)
{
if (event->EventHandlers[index])
@@ -224,6 +225,8 @@ int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context, const
}
}
}
if (pubSub->synchronized)
PubSub_Unlock(pubSub);
return status;
}

View File

@@ -55,7 +55,8 @@ int TestPubSub(int argc, char* argv[])
e.x = 64;
e.y = 128;
PubSub_OnMouseMotion(node, nullptr, &e);
if (PubSub_OnMouseMotion(node, nullptr, &e) < 0)
return -1;
}
{
@@ -66,7 +67,8 @@ int TestPubSub(int argc, char* argv[])
e.flags = 7;
e.button = 1;
PubSub_OnMouseButton(node, nullptr, &e);
if (PubSub_OnMouseButton(node, nullptr, &e) < 0)
return -1;
}
PubSub_Free(node);