lttng-dev.lists.lttng.org archive mirror
 help / color / mirror / Atom feed
* [PATCH lttng-tools] Fix: initialize sessions pointer to NULL
@ 2019-10-25 21:56 Jonathan Rajotte
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Rajotte @ 2019-10-25 21:56 UTC (permalink / raw)
  To: lttng-dev; +Cc: jgalar

lttng_list_sessions does not set the passed pointer to NULL on empty
return. This lead to deallocation of non-allocated memory (segfault).

For returns of size 0, the value of the passed argument should be
considered "undefined".

Refactor error handling a bit by removing the "error" jump. Always call
free on the sessions object.

Fixes #1205

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
---
 src/bin/lttng/commands/list.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c
index 28166c8be..65d8ea6f5 100644
--- a/src/bin/lttng/commands/list.c
+++ b/src/bin/lttng/commands/list.c
@@ -1825,7 +1825,7 @@ static int list_sessions(const char *session_name)
 	int ret = CMD_SUCCESS;
 	int count, i;
 	unsigned int session_found = 0;
-	struct lttng_session *sessions;
+	struct lttng_session *sessions = NULL;
 
 	count = lttng_list_sessions(&sessions);
 	DBG("Session count %d", count);
@@ -1838,7 +1838,7 @@ static int list_sessions(const char *session_name)
 	if (lttng_opt_mi) {
 		/* Mi */
 		if (session_name == NULL) {
-			/* List all session */
+			/* List all sessions */
 			ret = mi_list_sessions(sessions, count);
 		} else {
 			/* Note : this return an open session element */
@@ -1846,7 +1846,7 @@ static int list_sessions(const char *session_name)
 		}
 		if (ret) {
 			ret = CMD_ERROR;
-			goto error;
+			goto end;
 		}
 	} else {
 		/* Pretty print */
@@ -1893,7 +1893,7 @@ static int list_sessions(const char *session_name)
 		if (!session_found && session_name != NULL) {
 			ERR("Session '%s' not found", session_name);
 			ret = CMD_ERROR;
-			goto error;
+			goto end;
 		}
 
 		if (session_name == NULL) {
@@ -1901,9 +1901,8 @@ static int list_sessions(const char *session_name)
 		}
 	}
 
-error:
-	free(sessions);
 end:
+	free(sessions);
 	return ret;
 }
 
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH lttng-tools] Fix: initialize sessions pointer to NULL
       [not found] <20191025215626.19148-1-jonathan.rajotte-julien@efficios.com>
@ 2019-11-05  5:25 ` Jérémie Galarneau
  0 siblings, 0 replies; 2+ messages in thread
From: Jérémie Galarneau @ 2019-11-05  5:25 UTC (permalink / raw)
  To: Jonathan Rajotte; +Cc: lttng-dev, jgalar

Merged in master, stable-2.11, and stable-2.10.

Thanks!
Jérémie

On Fri, Oct 25, 2019 at 05:56:26PM -0400, Jonathan Rajotte wrote:
> lttng_list_sessions does not set the passed pointer to NULL on empty
> return. This lead to deallocation of non-allocated memory (segfault).
> 
> For returns of size 0, the value of the passed argument should be
> considered "undefined".
> 
> Refactor error handling a bit by removing the "error" jump. Always call
> free on the sessions object.
> 
> Fixes #1205
> 
> Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
> ---
>  src/bin/lttng/commands/list.c | 11 +++++------
>  1 file changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c
> index 28166c8be..65d8ea6f5 100644
> --- a/src/bin/lttng/commands/list.c
> +++ b/src/bin/lttng/commands/list.c
> @@ -1825,7 +1825,7 @@ static int list_sessions(const char *session_name)
>  	int ret = CMD_SUCCESS;
>  	int count, i;
>  	unsigned int session_found = 0;
> -	struct lttng_session *sessions;
> +	struct lttng_session *sessions = NULL;
>  
>  	count = lttng_list_sessions(&sessions);
>  	DBG("Session count %d", count);
> @@ -1838,7 +1838,7 @@ static int list_sessions(const char *session_name)
>  	if (lttng_opt_mi) {
>  		/* Mi */
>  		if (session_name == NULL) {
> -			/* List all session */
> +			/* List all sessions */
>  			ret = mi_list_sessions(sessions, count);
>  		} else {
>  			/* Note : this return an open session element */
> @@ -1846,7 +1846,7 @@ static int list_sessions(const char *session_name)
>  		}
>  		if (ret) {
>  			ret = CMD_ERROR;
> -			goto error;
> +			goto end;
>  		}
>  	} else {
>  		/* Pretty print */
> @@ -1893,7 +1893,7 @@ static int list_sessions(const char *session_name)
>  		if (!session_found && session_name != NULL) {
>  			ERR("Session '%s' not found", session_name);
>  			ret = CMD_ERROR;
> -			goto error;
> +			goto end;
>  		}
>  
>  		if (session_name == NULL) {
> @@ -1901,9 +1901,8 @@ static int list_sessions(const char *session_name)
>  		}
>  	}
>  
> -error:
> -	free(sessions);
>  end:
> +	free(sessions);
>  	return ret;
>  }
>  
> -- 
> 2.17.1
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-11-05  5:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-25 21:56 [PATCH lttng-tools] Fix: initialize sessions pointer to NULL Jonathan Rajotte
     [not found] <20191025215626.19148-1-jonathan.rajotte-julien@efficios.com>
2019-11-05  5:25 ` Jérémie Galarneau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).