Patchwork [RESEND] lib: fwts_args: remove strlen() calls, more efficient and stops valgrind complaining

login
register
mail settings
Submitter Colin King
Date May 16, 2012, 9:25 a.m.
Message ID <1337160330-25113-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/159554/
State Accepted
Headers show

Comments

Colin King - May 16, 2012, 9:25 a.m.
From: Colin Ian King <colin.king@canonical.com>

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_args.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Keng-Yu Lin - May 17, 2012, 7:01 a.m.
On Wed, May 16, 2012 at 5:25 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_args.c |    5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_args.c b/src/lib/src/fwts_args.c
> index b5ef3e0..b9ae7dc 100644
> --- a/src/lib/src/fwts_args.c
> +++ b/src/lib/src/fwts_args.c
> @@ -101,6 +101,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>        int option_index;
>        int ret = FWTS_OK;
>        char *short_options = NULL;
> +       size_t short_options_len = 0;
>
>        long_options = calloc(1, (total_options + 1) * sizeof(struct option));
>        if (long_options == NULL) {
> @@ -126,7 +127,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>                        if (short_name && (len = strlen(short_name)) > 0) {
>                                if (short_options) {
>                                        short_options = realloc(short_options,
> -                                               strlen(short_options) + len + 1);
> +                                               short_options_len + len + 1);
>                                        if (short_options == NULL) {
>                                                fwts_log_error(fw,
>                                                        "Out of memory "
> @@ -134,6 +135,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>                                                return FWTS_ERROR;
>                                        }
>                                        strcat(short_options, short_name);
> +                                       short_options_len += (len + 1);
>                                } else {
>                                        short_options = calloc(1, len + 1);
>                                        if (short_options == NULL) {
> @@ -143,6 +145,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>                                                return FWTS_ERROR;
>                                        }
>                                        strcpy(short_options, short_name);
> +                                       short_options_len += (len + 1);
>                                }
>                        }
>                }
> --
> 1.7.10
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Alex Hung - May 21, 2012, 8:54 a.m.
On 05/16/2012 05:25 PM, Colin King wrote:
> From: Colin Ian King<colin.king@canonical.com>
>
> Signed-off-by: Colin Ian King<colin.king@canonical.com>
> ---
>   src/lib/src/fwts_args.c |    5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_args.c b/src/lib/src/fwts_args.c
> index b5ef3e0..b9ae7dc 100644
> --- a/src/lib/src/fwts_args.c
> +++ b/src/lib/src/fwts_args.c
> @@ -101,6 +101,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>   	int option_index;
>   	int ret = FWTS_OK;
>   	char *short_options = NULL;
> +	size_t short_options_len = 0;
>
>   	long_options = calloc(1, (total_options + 1) * sizeof(struct option));
>   	if (long_options == NULL) {
> @@ -126,7 +127,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>   			if (short_name&&  (len = strlen(short_name))>  0) {
>   				if (short_options) {
>   					short_options = realloc(short_options,
> -						strlen(short_options) + len + 1);
> +						short_options_len + len + 1);
>   					if (short_options == NULL) {
>   						fwts_log_error(fw,
>   							"Out of memory "
> @@ -134,6 +135,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>   						return FWTS_ERROR;
>   					}
>   					strcat(short_options, short_name);
> +					short_options_len += (len + 1);
>   				} else {
>   					short_options = calloc(1, len + 1);
>   					if (short_options == NULL) {
> @@ -143,6 +145,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>   						return FWTS_ERROR;
>   					}
>   					strcpy(short_options, short_name);
> +					short_options_len += (len + 1);
>   				}
>   			}
>   		}
Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu - May 22, 2012, 5:29 a.m.
On 05/16/2012 05:25 PM, Colin King wrote:
> From: Colin Ian King<colin.king@canonical.com>
>
> Signed-off-by: Colin Ian King<colin.king@canonical.com>
> ---
>   src/lib/src/fwts_args.c |    5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_args.c b/src/lib/src/fwts_args.c
> index b5ef3e0..b9ae7dc 100644
> --- a/src/lib/src/fwts_args.c
> +++ b/src/lib/src/fwts_args.c
> @@ -101,6 +101,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>   	int option_index;
>   	int ret = FWTS_OK;
>   	char *short_options = NULL;
> +	size_t short_options_len = 0;
>
>   	long_options = calloc(1, (total_options + 1) * sizeof(struct option));
>   	if (long_options == NULL) {
> @@ -126,7 +127,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>   			if (short_name&&  (len = strlen(short_name))>  0) {
>   				if (short_options) {
>   					short_options = realloc(short_options,
> -						strlen(short_options) + len + 1);
> +						short_options_len + len + 1);
>   					if (short_options == NULL) {
>   						fwts_log_error(fw,
>   							"Out of memory "
> @@ -134,6 +135,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>   						return FWTS_ERROR;
>   					}
>   					strcat(short_options, short_name);
> +					short_options_len += (len + 1);
>   				} else {
>   					short_options = calloc(1, len + 1);
>   					if (short_options == NULL) {
> @@ -143,6 +145,7 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
>   						return FWTS_ERROR;
>   					}
>   					strcpy(short_options, short_name);
> +					short_options_len += (len + 1);
>   				}
>   			}
>   		}
Acked-by: Ivan Hu<ivan.hu@canonical.com>

Patch

diff --git a/src/lib/src/fwts_args.c b/src/lib/src/fwts_args.c
index b5ef3e0..b9ae7dc 100644
--- a/src/lib/src/fwts_args.c
+++ b/src/lib/src/fwts_args.c
@@ -101,6 +101,7 @@  int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
 	int option_index;
 	int ret = FWTS_OK;
 	char *short_options = NULL;
+	size_t short_options_len = 0;
 
 	long_options = calloc(1, (total_options + 1) * sizeof(struct option));
 	if (long_options == NULL) {
@@ -126,7 +127,7 @@  int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
 			if (short_name && (len = strlen(short_name)) > 0) {
 				if (short_options) {
 					short_options = realloc(short_options,
-						strlen(short_options) + len + 1);
+						short_options_len + len + 1);
 					if (short_options == NULL) {
 						fwts_log_error(fw,
 							"Out of memory "
@@ -134,6 +135,7 @@  int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
 						return FWTS_ERROR;
 					}
 					strcat(short_options, short_name);
+					short_options_len += (len + 1);
 				} else {
 					short_options = calloc(1, len + 1);
 					if (short_options == NULL) {
@@ -143,6 +145,7 @@  int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[])
 						return FWTS_ERROR;
 					}
 					strcpy(short_options, short_name);
+					short_options_len += (len + 1);
 				}
 			}
 		}