Skip to content
  • Rodrigo Siqueira's avatar
    intel_reg: Fix truncate string in the snprintf · c3b6d69a
    Rodrigo Siqueira authored
    
    
    This patch fix the following GCC warning:
    
    ../tools/intel_reg.c: In function ‘dump_decode’:
    ../tools/intel_reg.c:203:41: warning: ‘snprintf’ output may be truncated
    before the last format character [-Wformat-truncation=]
       snprintf(decode, sizeof(decode), "\n%s", bin);
    [..]
    ../tools/intel_reg.c:200:40: warning: ‘%s’ directive output may be
    truncated writing up to 1023 bytes into a region of size 1022
    [-Wformat-truncation=]
        snprintf(decode, sizeof(decode), " (%s)\n%s", tmp, bin);
    [..]
    ../tools/intel_reg.c:200:4: note: ‘snprintf’ output between 5 and 2051
    bytes into a destination of size 1024
        snprintf(decode, sizeof(decode), " (%s)\n%s", tmp, bin);
    [..]
    
    The decode[] variable contains concatenated contents of bin[] and tmp[],
    both of which are allocated as 1024 bytes.
    
    Allocating 1024 chars for bin[] seems like an overkill, since all it
    ever holds it the output of to_binary().
    
    to_binary outputs fixed format:
    --------------------------------------------------------------------
                   24               16                8                0
      1 1 0 1 1 1 1 0  1 0 1 0 1 1 0 1  1 0 1 1 1 1 1 0  1 1 1 0 1 1 1 1
    --------------------------------------------------------------------
    Which is 138 chars long (sans the new line).
    
    We can limit the size of char bin[] to that number (-ish), and then
    slightly bump the size of decode[] to accommodate for combined sizes of
    tmp[] and bin[].
    
    Changes since V1:
     - Improve commit message
    Changes since V2:
     - updated commit message
     - limit the amount of stack abuse
    
    Signed-off-by: default avatarRodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
    Signed-off-by: default avatarArkadiusz Hiler <arkadiusz.hiler@intel.com>
    Reviewed-by: default avatarRodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
    c3b6d69a