Patchwork lib: fwts_dump_data: tidy up code, ensure we limit nbytes to 16

login
register
mail settings
Submitter Colin King
Date Dec. 10, 2012, 10:59 p.m.
Message ID <1355180384-15367-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/205052/
State Accepted
Headers show

Comments

Colin King - Dec. 10, 2012, 10:59 p.m.
From: Colin Ian King <colin.king@canonical.com>

Tidy up this a little.  Make buffer sizes type size_t. Make sure
that we can only dump a maximum of 16 bytes.  Tidy up the formatting
of the function declaration.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/include/fwts_dump_data.h |  2 +-
 src/lib/src/fwts_dump_data.c     | 19 +++++++++++++------
 2 files changed, 14 insertions(+), 7 deletions(-)
Keng-Yu Lin - Dec. 12, 2012, 5:20 a.m.
On Tue, Dec 11, 2012 at 6:59 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Tidy up this a little.  Make buffer sizes type size_t. Make sure
> that we can only dump a maximum of 16 bytes.  Tidy up the formatting
> of the function declaration.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/include/fwts_dump_data.h |  2 +-
>  src/lib/src/fwts_dump_data.c     | 19 +++++++++++++------
>  2 files changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/src/lib/include/fwts_dump_data.h b/src/lib/include/fwts_dump_data.h
> index 8c5b6d0..7d0420a 100644
> --- a/src/lib/include/fwts_dump_data.h
> +++ b/src/lib/include/fwts_dump_data.h
> @@ -22,6 +22,6 @@
>
>  #include "fwts.h"
>
> -void fwts_dump_raw_data(char *buffer, const int len, const uint8_t *data, const int where, const int bytes);
> +void fwts_dump_raw_data(char *buffer, const size_t len, const uint8_t *data, const int where, const size_t bytes);
>
>  #endif
> diff --git a/src/lib/src/fwts_dump_data.c b/src/lib/src/fwts_dump_data.c
> index 23c3a89..c398b53 100644
> --- a/src/lib/src/fwts_dump_data.c
> +++ b/src/lib/src/fwts_dump_data.c
> @@ -24,28 +24,35 @@
>
>  /*
>   *  fwts_dump_raw_data()
> - *     print raw uint8 data of length `nbytes` into a buffer (length len) as a hex dump. nbytes must
> - *     be no more than 16. The address/offset of the buffer in memory is annotated by addr.
> + *     print raw uint8 data of length `nbytes` into a buffer (length len)
> + *      as a hex dump. nbytes must be no more than 16. The address/offset
> + *      of the buffer in memory is annotated by addr.
>   */
> -void fwts_dump_raw_data(char *buffer, const int len, const uint8_t *data, const int addr, const int nbytes)
> +void fwts_dump_raw_data(
> +       char *buffer,           /* buffer to contained formatted dump */
> +       const size_t len,       /* Length of buffer */
> +       const uint8_t *data,    /* Octects to dump */
> +       const int addr,         /* Original buffer data address */
> +       const size_t nbytes)    /* Number of bytes to dump, max 16 */
>  {
>          int i;
>         int n = 0;
> +       int nbytes_max = nbytes > 16 ? 16 : nbytes;
>
>         n = snprintf(buffer, len, "  %4.4x: ", addr);
>
>         /* Hex dump */
> -        for (i=0;i<nbytes;i++)
> +        for (i = 0; i < nbytes_max; i++)
>                  n += snprintf(buffer + n, len - n, "%2.2x ", data[i]);
>
>         /* Padding */
> -        for (;i<16;i++)
> +        for (; i < 16; i++)
>                  n += snprintf(buffer + n, len - n, "   ");
>
>          n += snprintf(buffer + n, len - n, " ");
>
>         /* printable ASCII dump */
> -        for (i=0;i<nbytes;i++)
> +        for (i = 0; i < nbytes_max; i++)
>                 buffer[n++] = (data[i] < 32 || data[i] > 126) ? '.' : data[i];
>         buffer[n] = '\0';
>  }
> --
> 1.8.0
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Alex Hung - Dec. 12, 2012, 6:35 a.m.
On 12/11/2012 06:59 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Tidy up this a little.  Make buffer sizes type size_t. Make sure
> that we can only dump a maximum of 16 bytes.  Tidy up the formatting
> of the function declaration.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/include/fwts_dump_data.h |  2 +-
>   src/lib/src/fwts_dump_data.c     | 19 +++++++++++++------
>   2 files changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/src/lib/include/fwts_dump_data.h b/src/lib/include/fwts_dump_data.h
> index 8c5b6d0..7d0420a 100644
> --- a/src/lib/include/fwts_dump_data.h
> +++ b/src/lib/include/fwts_dump_data.h
> @@ -22,6 +22,6 @@
>
>   #include "fwts.h"
>
> -void fwts_dump_raw_data(char *buffer, const int len, const uint8_t *data, const int where, const int bytes);
> +void fwts_dump_raw_data(char *buffer, const size_t len, const uint8_t *data, const int where, const size_t bytes);
>
>   #endif
> diff --git a/src/lib/src/fwts_dump_data.c b/src/lib/src/fwts_dump_data.c
> index 23c3a89..c398b53 100644
> --- a/src/lib/src/fwts_dump_data.c
> +++ b/src/lib/src/fwts_dump_data.c
> @@ -24,28 +24,35 @@
>
>   /*
>    *  fwts_dump_raw_data()
> - *	print raw uint8 data of length `nbytes` into a buffer (length len) as a hex dump. nbytes must
> - *	be no more than 16. The address/offset of the buffer in memory is annotated by addr.
> + *	print raw uint8 data of length `nbytes` into a buffer (length len)
> + *      as a hex dump. nbytes must be no more than 16. The address/offset
> + *      of the buffer in memory is annotated by addr.
>    */
> -void fwts_dump_raw_data(char *buffer, const int len, const uint8_t *data, const int addr, const int nbytes)
> +void fwts_dump_raw_data(
> +	char *buffer,		/* buffer to contained formatted dump */
> +	const size_t len,	/* Length of buffer */
> +	const uint8_t *data,	/* Octects to dump */
> +	const int addr,		/* Original buffer data address */
> +	const size_t nbytes)	/* Number of bytes to dump, max 16 */
>   {
>           int i;
>   	int n = 0;
> +	int nbytes_max = nbytes > 16 ? 16 : nbytes;
>
>   	n = snprintf(buffer, len, "  %4.4x: ", addr);
>
>   	/* Hex dump */
> -        for (i=0;i<nbytes;i++)
> +        for (i = 0; i < nbytes_max; i++)
>                   n += snprintf(buffer + n, len - n, "%2.2x ", data[i]);
>
>   	/* Padding */
> -        for (;i<16;i++)
> +        for (; i < 16; i++)
>                   n += snprintf(buffer + n, len - n, "   ");
>
>           n += snprintf(buffer + n, len - n, " ");
>
>   	/* printable ASCII dump */
> -        for (i=0;i<nbytes;i++)
> +        for (i = 0; i < nbytes_max; i++)
>   		buffer[n++] = (data[i] < 32 || data[i] > 126) ? '.' : data[i];
>   	buffer[n] = '\0';
>   }
>
Acked-by: Alex Hung <alex.hung@canonical.com>

Patch

diff --git a/src/lib/include/fwts_dump_data.h b/src/lib/include/fwts_dump_data.h
index 8c5b6d0..7d0420a 100644
--- a/src/lib/include/fwts_dump_data.h
+++ b/src/lib/include/fwts_dump_data.h
@@ -22,6 +22,6 @@ 
 
 #include "fwts.h"
 
-void fwts_dump_raw_data(char *buffer, const int len, const uint8_t *data, const int where, const int bytes);
+void fwts_dump_raw_data(char *buffer, const size_t len, const uint8_t *data, const int where, const size_t bytes);
 
 #endif
diff --git a/src/lib/src/fwts_dump_data.c b/src/lib/src/fwts_dump_data.c
index 23c3a89..c398b53 100644
--- a/src/lib/src/fwts_dump_data.c
+++ b/src/lib/src/fwts_dump_data.c
@@ -24,28 +24,35 @@ 
 
 /*
  *  fwts_dump_raw_data()
- *	print raw uint8 data of length `nbytes` into a buffer (length len) as a hex dump. nbytes must
- *	be no more than 16. The address/offset of the buffer in memory is annotated by addr.
+ *	print raw uint8 data of length `nbytes` into a buffer (length len)
+ *      as a hex dump. nbytes must be no more than 16. The address/offset
+ *      of the buffer in memory is annotated by addr.
  */
-void fwts_dump_raw_data(char *buffer, const int len, const uint8_t *data, const int addr, const int nbytes)
+void fwts_dump_raw_data(
+	char *buffer,		/* buffer to contained formatted dump */
+	const size_t len,	/* Length of buffer */
+	const uint8_t *data,	/* Octects to dump */
+	const int addr,		/* Original buffer data address */
+	const size_t nbytes)	/* Number of bytes to dump, max 16 */
 {
         int i;
 	int n = 0;
+	int nbytes_max = nbytes > 16 ? 16 : nbytes;
 
 	n = snprintf(buffer, len, "  %4.4x: ", addr);
 
 	/* Hex dump */
-        for (i=0;i<nbytes;i++)
+        for (i = 0; i < nbytes_max; i++)
                 n += snprintf(buffer + n, len - n, "%2.2x ", data[i]);
 
 	/* Padding */
-        for (;i<16;i++)
+        for (; i < 16; i++)
                 n += snprintf(buffer + n, len - n, "   ");
 
         n += snprintf(buffer + n, len - n, " ");
 
 	/* printable ASCII dump */
-        for (i=0;i<nbytes;i++)
+        for (i = 0; i < nbytes_max; i++)
 		buffer[n++] = (data[i] < 32 || data[i] > 126) ? '.' : data[i];
 	buffer[n] = '\0';
 }