[PATCH] lib: fwts_dump_data: tidy up code, ensure we limit nbytes to 16

Keng-Yu Lin kengyu at canonical.com
Wed Dec 12 05:20:27 UTC 2012


On Tue, Dec 11, 2012 at 6:59 AM, Colin King <colin.king at canonical.com> wrote:
> From: Colin Ian King <colin.king at 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 at 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 at canonical.com>



More information about the fwts-devel mailing list