Changeset cc93c83 in rtems


Ignore:
Timestamp:
Sep 2, 2015, 9:54:24 PM (4 years ago)
Author:
Martin Galvan <martin.galvan@…>
Branches:
master
Children:
28a870c4
Parents:
9147a82
git-author:
Martin Galvan <martin.galvan@…> (09/02/15 21:54:24)
git-committer:
Joel Sherrill <joel.sherrill@…> (09/03/15 17:33:28)
Message:

cpukit/libmisc/dumpbuf/dumpbuf.c: Fix undefined behavior for sprintf()

I also used the 'n' versions of the string functions, #define'd magic numbers
and added a few comments.

Updates #2405.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libmisc/dumpbuf/dumpbuf.c

    r9147a82 rcc93c83  
    77
    88/*
    9  *  COPYRIGHT (c) 1997-2007.
     9 *  COPYRIGHT (c) 1997-2015.
    1010 *  On-Line Applications Research Corporation (OAR).
    1111 *
     
    2525#include <rtems/bspIo.h>
    2626
     27#define HEX_FMT_LENGTH 3   /* Length of the formatted hex string. */
     28#define ASCII_FMT_LENGTH 1 /* Length of the formatted ASCII string. */
     29#define BYTES_PER_ROW 16    /* Amount of bytes from buffer shown in each row. */
     30#define BARS 2             /* Amount of bars in each row. */
     31/* Max length of each row string. */
     32#define ROW_LENGTH (BYTES_PER_ROW * (HEX_FMT_LENGTH + ASCII_FMT_LENGTH) + BARS)
     33
    2734/*
    2835 *  Put the body below rtems_print_buffer so it won't get inlined.
    2936 */
    3037
    31 static inline void Dump_Line(
    32   const unsigned char *buffer,
    33   int                  length
    34 );
     38static void Dump_Line(const unsigned char *buffer, const unsigned int length);
    3539
    36 void rtems_print_buffer(
    37   const unsigned char *buffer,
    38   int                  length
    39 )
     40/**
     41 * @brief Print \p length bytes from \p buffer, both in hex and ASCII.
     42 * Printing will be done in rows, each showing BYTES_PER_ROW bytes.
     43 * @details Non-printable chars will appear as dots.
     44 *
     45 * @param buffer The buffer we'll print.
     46 * @param length Amount of bytes from \p buffer we'll print. This can't be
     47 * unsigned because we don't have a way to check if we're erroneously getting
     48 * a negative \p length.
     49 */
     50void rtems_print_buffer(const unsigned char *buffer, const int length)
    4051{
     52  unsigned int i, mod, max;
    4153
    42   int i, mod, max;
     54  if (length > 0) {
     55    mod = length % BYTES_PER_ROW;
    4356
    44   if ( !length ) return;
     57    max = length - mod;
    4558
    46   mod = length % 16;
     59    /* Print length / BYTES_PER_ROW rows. */
     60    for (i = 0; i < max; i += BYTES_PER_ROW) {
     61      Dump_Line(&buffer[i], BYTES_PER_ROW);
     62    }
    4763
    48   max = length - mod;
    49 
    50   for ( i=0 ; i<max ; i+=16 )
    51     Dump_Line( &buffer[ i ], 16 );
    52 
    53   if ( mod )
    54     Dump_Line( &buffer[ max ], mod );
     64    /* Print another row with the remaining bytes. */
     65    if (mod > 0) {
     66      Dump_Line(&buffer[max], mod);
     67    }
     68  } else {
     69    printk("Error: length must be greater than zero.");
     70  }
    5571}
    5672
    57 static inline void Dump_Line(
    58   const unsigned char *buffer,
    59   int                  length
    60 )
     73/**
     74 * @brief Print \p length bytes from \p buffer, both in hex and ASCII.
     75 * @details Non-printable chars will appear as dots.
     76 *
     77 * @param buffer The buffer we'll print.
     78 * @param length Amount of bytes from \p buffer we'll print.
     79 */
     80static void Dump_Line(const unsigned char *buffer, const unsigned int length)
    6181{
     82  unsigned int i;
     83  static unsigned char line_buffer[ROW_LENGTH] = "";
     84  size_t tmp_len;
    6285
    63   int  i;
    64   char line_buffer[120];
     86  /* Output the hex value of each byte. */
     87  for (i = 0; i < length; ++i) {
     88    snprintf(&line_buffer[i * HEX_FMT_LENGTH], HEX_FMT_LENGTH + 1,
     89             "%02x ", buffer[i]);
     90  }
    6591
    66   line_buffer[0] = '\0';
     92  /* Fill the remaining space with whitespace (if necessary). */
     93  for (; i < BYTES_PER_ROW; ++i) {
     94    strncat(line_buffer, "   ", HEX_FMT_LENGTH);
     95  }
    6796
    68   for( i=0 ; i<length ; i++ )
    69     sprintf( line_buffer, "%s%02x ", line_buffer, buffer[ i ] );
     97  /* Append a bar. */
     98  strncat(line_buffer, "|", 1);
     99  tmp_len = strnlen(line_buffer, ROW_LENGTH);
    70100
    71   for( ; i<16 ; i++ )
    72     strcat( line_buffer, "   " );
     101  /* Now output the ASCII glyphs of printable chars. */
     102  for (i = 0; i < length; ++i) {
     103    snprintf(&line_buffer[tmp_len + i], ASCII_FMT_LENGTH + 1,
     104             "%c", isprint(buffer[i]) ? buffer[i] : '.');
     105  }
    73106
    74   strcat( line_buffer, "|" );
    75   for( i=0 ; i<length ; i++ )
    76     sprintf( line_buffer, "%s%c", line_buffer,
    77              isprint( buffer[ i ] ) ? buffer[ i ] : '.' );
     107  /* Fill the remaining space with whitespace (if necessary). */
     108  for(; i < BYTES_PER_ROW; i++) {
     109    strncat(line_buffer, " ", ASCII_FMT_LENGTH);
     110  }
    78111
    79   for( ; i<16 ; i++ )
    80     strcat( line_buffer, " " );
    81 
    82   strcat( line_buffer, "|\n" );
    83 
    84   printk( line_buffer );
     112  /* Append another bar and print the resulting string. */
     113  printk("%s|\n", line_buffer);
    85114}
Note: See TracChangeset for help on using the changeset viewer.