From 2c3e5e8301983790530a0e3da38a5676dfcdd3f5 Mon Sep 17 00:00:00 2001 From: Netaji Panigrahi <114923459+NetajiPanigrahi@users.noreply.github.com> Date: Wed, 20 Nov 2024 19:43:55 +0530 Subject: [PATCH] Rbus fix memory leak (#237) * : Handled error conditions and fixed memory leaks. --- src/rbus/rbus_subscriptions.c | 6 +++++- src/rtmessage/rtrouteBase.c | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/rbus/rbus_subscriptions.c b/src/rbus/rbus_subscriptions.c index 5e6636de..8f88996c 100644 --- a/src/rbus/rbus_subscriptions.c +++ b/src/rbus/rbus_subscriptions.c @@ -621,7 +621,7 @@ static void rbusSubscriptions_loadCache(rbusSubscriptions_t subscriptions) rbusBuffer_Destroy(buff); if(sub) - free(sub); + subscriptionFree(sub); if(remove(filePath) != 0) RBUSLOG_ERROR("failed to remove %s", filePath); @@ -665,7 +665,11 @@ static void rbusSubscriptions_saveCache(rbusSubscriptions_t subscriptions) { rtListItem_GetData(item, (void**)&sub); if(!sub) + { + rbusBuffer_Destroy(buff); + fclose(file); return; + } rbusBuffer_WriteStringTLV(buff, sub->listener, strlen(sub->listener)+1); rbusBuffer_WriteStringTLV(buff, sub->eventName, strlen(sub->eventName)+1); rbusBuffer_WriteInt32TLV(buff, sub->componentId); diff --git a/src/rtmessage/rtrouteBase.c b/src/rtmessage/rtrouteBase.c index f9d1c700..29df1be8 100644 --- a/src/rtmessage/rtrouteBase.c +++ b/src/rtmessage/rtrouteBase.c @@ -61,12 +61,18 @@ rtRouteBase_BindListener(char const* socket_name, int no_delay, int indefinite_r unsigned int num_retries = 1; listener = (rtListener *)rt_malloc(sizeof(rtListener)); + if (!listener) + return rtErrorFromErrno(ENOMEM); + listener->fd = -1; memset(&listener->local_endpoint, 0, sizeof(struct sockaddr_storage)); err = rtSocketStorage_FromString(&listener->local_endpoint, socket_name); if (err != RT_OK) - return err; + { + free(listener); + return err; + } rtLog_Debug("binding listener:%s", socket_name); @@ -130,7 +136,7 @@ rtRouteBase_BindListener(char const* socket_name, int no_delay, int indefinite_r { rtLog_Warn("failed to set socket to listen mode. %s", rtStrError(errno)); rtRouteBase_CloseListener(listener); - free(listener); + free(listener); return RT_FAIL; } @@ -577,6 +583,8 @@ rtRouteDirect_StartInstance(const char* socket_name, rtDriectClientHandler messa } route = (rtRouteEntry *)rt_malloc(sizeof(rtRouteEntry)); + if (!route) + return rtErrorFromErrno(ENOMEM); route->subscription = NULL; strncpy(route->expression, "_RTDIRECT>", RTMSG_MAX_EXPRESSION_LEN-1); route->message_handler = _rtdirect_OnMessage;