Patchwork lib: framework: don't check for long string if src is NULL

login
register
mail settings
Submitter Colin King
Date May 20, 2013, 10:06 a.m.
Message ID <1369044406-22696-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/244875/
State Accepted
Headers show

Comments

Colin King - May 20, 2013, 10:06 a.m.
From: Colin Ian King <colin.king@canonical.com>

Commit 8f551a8719f0b0261076f5d873f2fa709008d325 introduced a new
bug.  We must no check for long strings if src is NULL.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_framework.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
Alex Hung - May 21, 2013, 6:11 a.m.
On 05/20/2013 06:06 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Commit 8f551a8719f0b0261076f5d873f2fa709008d325 introduced a new
> bug.  We must no check for long strings if src is NULL.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_framework.c | 15 +++++++--------
>   1 file changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
> index adb46c8..ff1e3b3 100644
> --- a/src/lib/src/fwts_framework.c
> +++ b/src/lib/src/fwts_framework.c
> @@ -291,16 +291,15 @@ static void fwts_framework_show_tests(fwts_framework *fw, bool full)
>    */
>   static void fwts_framework_strtrunc(char *dest, const char *src, size_t max)
>   {
> -	if (src)
> +	if (src) {
>   		strncpy(dest, src, max);
> -	else
> +		if ((max > 3) && (strlen(src) > max)) {
> +			dest[max-1] = 0;
> +			dest[max-2] = '.';
> +			dest[max-3] = '.';
> +		}
> +	} else
>   		strncpy(dest, "", max);
> -
> -	if ((strlen(src) > max) && (max > 3)) {
> -		dest[max-1] = 0;
> -		dest[max-2] = '.';
> -		dest[max-3] = '.';
> -	}
>   }
>
>   /*
>
Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu - May 22, 2013, 2:27 a.m.
On 05/20/2013 06:06 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Commit 8f551a8719f0b0261076f5d873f2fa709008d325 introduced a new
> bug.  We must no check for long strings if src is NULL.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_framework.c | 15 +++++++--------
>   1 file changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
> index adb46c8..ff1e3b3 100644
> --- a/src/lib/src/fwts_framework.c
> +++ b/src/lib/src/fwts_framework.c
> @@ -291,16 +291,15 @@ static void fwts_framework_show_tests(fwts_framework *fw, bool full)
>    */
>   static void fwts_framework_strtrunc(char *dest, const char *src, size_t max)
>   {
> -	if (src)
> +	if (src) {
>   		strncpy(dest, src, max);
> -	else
> +		if ((max > 3) && (strlen(src) > max)) {
> +			dest[max-1] = 0;
> +			dest[max-2] = '.';
> +			dest[max-3] = '.';
> +		}
> +	} else
>   		strncpy(dest, "", max);
> -
> -	if ((strlen(src) > max) && (max > 3)) {
> -		dest[max-1] = 0;
> -		dest[max-2] = '.';
> -		dest[max-3] = '.';
> -	}
>   }
>
>   /*
>

Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch

diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
index adb46c8..ff1e3b3 100644
--- a/src/lib/src/fwts_framework.c
+++ b/src/lib/src/fwts_framework.c
@@ -291,16 +291,15 @@  static void fwts_framework_show_tests(fwts_framework *fw, bool full)
  */
 static void fwts_framework_strtrunc(char *dest, const char *src, size_t max)
 {
-	if (src)
+	if (src) {
 		strncpy(dest, src, max);
-	else
+		if ((max > 3) && (strlen(src) > max)) {
+			dest[max-1] = 0;
+			dest[max-2] = '.';
+			dest[max-3] = '.';
+		}
+	} else
 		strncpy(dest, "", max);
-
-	if ((strlen(src) > max) && (max > 3)) {
-		dest[max-1] = 0;
-		dest[max-2] = '.';
-		dest[max-3] = '.';
-	}
 }
 
 /*