diff mbox

[1/3] example: odp_l2fwd: replace strtok_r with strtok and fix leaks

Message ID 1426081924-12252-2-git-send-email-ciprian.barbu@linaro.org
State New
Headers show

Commit Message

Ciprian Barbu March 11, 2015, 1:52 p.m. UTC
The odp_l2fwd example leaks some strings allocated during parse_args.
https://bugs.linaro.org/show_bug.cgi?id=1117
CID 56899:  Resource leak  (RESOURCE_LEAK)

Signed-off-by: Ciprian Barbu <ciprian.barbu@linaro.org>
---
 example/l2fwd/odp_l2fwd.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

Comments

Ciprian Barbu March 19, 2015, 3:32 p.m. UTC | #1
Any takers? Stuart?

On Wed, Mar 11, 2015 at 3:52 PM, Ciprian Barbu <ciprian.barbu@linaro.org> wrote:
> The odp_l2fwd example leaks some strings allocated during parse_args.
> https://bugs.linaro.org/show_bug.cgi?id=1117
> CID 56899:  Resource leak  (RESOURCE_LEAK)
>
> Signed-off-by: Ciprian Barbu <ciprian.barbu@linaro.org>
> ---
>  example/l2fwd/odp_l2fwd.c | 29 +++++++++++++++--------------
>  1 file changed, 15 insertions(+), 14 deletions(-)
>
> diff --git a/example/l2fwd/odp_l2fwd.c b/example/l2fwd/odp_l2fwd.c
> index d062a72..a5f85c0 100644
> --- a/example/l2fwd/odp_l2fwd.c
> +++ b/example/l2fwd/odp_l2fwd.c
> @@ -73,6 +73,7 @@ typedef struct {
>         int if_count;           /**< Number of interfaces to be used */
>         char **if_names;        /**< Array of pointers to interface names */
>         int mode;               /**< Packet IO mode */
> +       char *if_str;           /**< Storage for interface names */
>  } appl_args_t;
>
>  /**
> @@ -415,6 +416,8 @@ int main(int argc, char *argv[])
>         /* Master thread waits for other threads to exit */
>         odph_linux_pthread_join(thread_tbl, num_workers);
>
> +       free(gbl_args->appl.if_names);
> +       free(gbl_args->appl.if_str);
>         printf("Exit\n\n");
>
>         return 0;
> @@ -462,7 +465,7 @@ static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
>  {
>         int opt;
>         int long_index;
> -       char *names, *str, *token, *save;
> +       char *token;
>         size_t len;
>         int i;
>         static struct option longopts[] = {
> @@ -495,19 +498,19 @@ static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
>                         }
>                         len += 1;       /* add room for '\0' */
>
> -                       names = malloc(len);
> -                       if (names == NULL) {
> +                       appl_args->if_str = malloc(len);
> +                       if (appl_args->if_str == NULL) {
>                                 usage(argv[0]);
>                                 exit(EXIT_FAILURE);
>                         }
>
>                         /* count the number of tokens separated by ',' */
> -                       strcpy(names, optarg);
> -                       for (str = names, i = 0;; str = NULL, i++) {
> -                               token = strtok_r(str, ",", &save);
> -                               if (token == NULL)
> -                                       break;
> -                       }
> +                       strcpy(appl_args->if_str, optarg);
> +                       for (token = strtok(appl_args->if_str, ","), i = 0;
> +                            token != NULL;
> +                            token = strtok(NULL, ","), i++)
> +                               ;
> +
>                         appl_args->if_count = i;
>
>                         if (appl_args->if_count == 0) {
> @@ -520,11 +523,9 @@ static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
>                             calloc(appl_args->if_count, sizeof(char *));
>
>                         /* store the if names (reset names string) */
> -                       strcpy(names, optarg);
> -                       for (str = names, i = 0;; str = NULL, i++) {
> -                               token = strtok_r(str, ",", &save);
> -                               if (token == NULL)
> -                                       break;
> +                       strcpy(appl_args->if_str, optarg);
> +                       for (token = strtok(appl_args->if_str, ","), i = 0;
> +                            token != NULL; token = strtok(NULL, ","), i++) {
>                                 appl_args->if_names[i] = token;
>                         }
>                         break;
> --
> 1.8.3.2
>
Mike Holmes March 23, 2015, 7:14 p.m. UTC | #2
http://linux.die.net/man/3/strtok_r  string.h is no longer needed

On 11 March 2015 at 09:52, Ciprian Barbu <ciprian.barbu@linaro.org> wrote:

> The odp_l2fwd example leaks some strings allocated during parse_args.
> https://bugs.linaro.org/show_bug.cgi?id=1117
> CID 56899:  Resource leak  (RESOURCE_LEAK)
>
> Signed-off-by: Ciprian Barbu <ciprian.barbu@linaro.org>
> ---
>  example/l2fwd/odp_l2fwd.c | 29 +++++++++++++++--------------
>  1 file changed, 15 insertions(+), 14 deletions(-)
>
> diff --git a/example/l2fwd/odp_l2fwd.c b/example/l2fwd/odp_l2fwd.c
> index d062a72..a5f85c0 100644
> --- a/example/l2fwd/odp_l2fwd.c
> +++ b/example/l2fwd/odp_l2fwd.c
> @@ -73,6 +73,7 @@ typedef struct {
>         int if_count;           /**< Number of interfaces to be used */
>         char **if_names;        /**< Array of pointers to interface names
> */
>         int mode;               /**< Packet IO mode */
> +       char *if_str;           /**< Storage for interface names */
>  } appl_args_t;
>
>  /**
> @@ -415,6 +416,8 @@ int main(int argc, char *argv[])
>         /* Master thread waits for other threads to exit */
>         odph_linux_pthread_join(thread_tbl, num_workers);
>
> +       free(gbl_args->appl.if_names);
> +       free(gbl_args->appl.if_str);
>         printf("Exit\n\n");
>
>         return 0;
> @@ -462,7 +465,7 @@ static void parse_args(int argc, char *argv[],
> appl_args_t *appl_args)
>  {
>         int opt;
>         int long_index;
> -       char *names, *str, *token, *save;
> +       char *token;
>         size_t len;
>         int i;
>         static struct option longopts[] = {
> @@ -495,19 +498,19 @@ static void parse_args(int argc, char *argv[],
> appl_args_t *appl_args)
>                         }
>                         len += 1;       /* add room for '\0' */
>
> -                       names = malloc(len);
> -                       if (names == NULL) {
> +                       appl_args->if_str = malloc(len);
> +                       if (appl_args->if_str == NULL) {
>                                 usage(argv[0]);
>                                 exit(EXIT_FAILURE);
>                         }
>
>                         /* count the number of tokens separated by ',' */
> -                       strcpy(names, optarg);
> -                       for (str = names, i = 0;; str = NULL, i++) {
> -                               token = strtok_r(str, ",", &save);
> -                               if (token == NULL)
> -                                       break;
> -                       }
> +                       strcpy(appl_args->if_str, optarg);
> +                       for (token = strtok(appl_args->if_str, ","), i = 0;
> +                            token != NULL;
> +                            token = strtok(NULL, ","), i++)
> +                               ;
> +
>                         appl_args->if_count = i;
>
>                         if (appl_args->if_count == 0) {
> @@ -520,11 +523,9 @@ static void parse_args(int argc, char *argv[],
> appl_args_t *appl_args)
>                             calloc(appl_args->if_count, sizeof(char *));
>
>                         /* store the if names (reset names string) */
> -                       strcpy(names, optarg);
> -                       for (str = names, i = 0;; str = NULL, i++) {
> -                               token = strtok_r(str, ",", &save);
> -                               if (token == NULL)
> -                                       break;
> +                       strcpy(appl_args->if_str, optarg);
> +                       for (token = strtok(appl_args->if_str, ","), i = 0;
> +                            token != NULL; token = strtok(NULL, ","),
> i++) {
>                                 appl_args->if_names[i] = token;
>                         }
>                         break;
> --
> 1.8.3.2
>
>
> _______________________________________________
> lng-odp mailing list
> lng-odp@lists.linaro.org
> http://lists.linaro.org/mailman/listinfo/lng-odp
>
diff mbox

Patch

diff --git a/example/l2fwd/odp_l2fwd.c b/example/l2fwd/odp_l2fwd.c
index d062a72..a5f85c0 100644
--- a/example/l2fwd/odp_l2fwd.c
+++ b/example/l2fwd/odp_l2fwd.c
@@ -73,6 +73,7 @@  typedef struct {
 	int if_count;		/**< Number of interfaces to be used */
 	char **if_names;	/**< Array of pointers to interface names */
 	int mode;		/**< Packet IO mode */
+	char *if_str;		/**< Storage for interface names */
 } appl_args_t;
 
 /**
@@ -415,6 +416,8 @@  int main(int argc, char *argv[])
 	/* Master thread waits for other threads to exit */
 	odph_linux_pthread_join(thread_tbl, num_workers);
 
+	free(gbl_args->appl.if_names);
+	free(gbl_args->appl.if_str);
 	printf("Exit\n\n");
 
 	return 0;
@@ -462,7 +465,7 @@  static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
 {
 	int opt;
 	int long_index;
-	char *names, *str, *token, *save;
+	char *token;
 	size_t len;
 	int i;
 	static struct option longopts[] = {
@@ -495,19 +498,19 @@  static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
 			}
 			len += 1;	/* add room for '\0' */
 
-			names = malloc(len);
-			if (names == NULL) {
+			appl_args->if_str = malloc(len);
+			if (appl_args->if_str == NULL) {
 				usage(argv[0]);
 				exit(EXIT_FAILURE);
 			}
 
 			/* count the number of tokens separated by ',' */
-			strcpy(names, optarg);
-			for (str = names, i = 0;; str = NULL, i++) {
-				token = strtok_r(str, ",", &save);
-				if (token == NULL)
-					break;
-			}
+			strcpy(appl_args->if_str, optarg);
+			for (token = strtok(appl_args->if_str, ","), i = 0;
+			     token != NULL;
+			     token = strtok(NULL, ","), i++)
+				;
+
 			appl_args->if_count = i;
 
 			if (appl_args->if_count == 0) {
@@ -520,11 +523,9 @@  static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
 			    calloc(appl_args->if_count, sizeof(char *));
 
 			/* store the if names (reset names string) */
-			strcpy(names, optarg);
-			for (str = names, i = 0;; str = NULL, i++) {
-				token = strtok_r(str, ",", &save);
-				if (token == NULL)
-					break;
+			strcpy(appl_args->if_str, optarg);
+			for (token = strtok(appl_args->if_str, ","), i = 0;
+			     token != NULL; token = strtok(NULL, ","), i++) {
 				appl_args->if_names[i] = token;
 			}
 			break;