sunflower-embedded-system-emulator icon indicating copy to clipboard operation
sunflower-embedded-system-emulator copied to clipboard

Wrong size in `snprintf` in `uncertain_upe.c`

Open phillipstanleymarbell opened this issue 5 years ago • 1 comments

Wrong size in snprintf in uncertain_upe.c:

uncertain_upe.c:589:49: warning: ‘%-*d’ directive output truncated writing between 5 and 11 bytes into a region of size 4 [-Wformat-truncation=]
  result = snprintf(buffer, PRINT_DIGITS + 1, " u%-*d", PRINT_DIGITS, row);
                                                 ^~~~
uncertain_upe.c:589:9: note: ‘snprintf’ output between 8 and 14 bytes into a destination of size 6
  result = snprintf(buffer, PRINT_DIGITS + 1, " u%-*d", PRINT_DIGITS, row);
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uncertain_upe.c:614:49: warning: ‘%#-*.*f’ directive output truncated writing between 7 and 316 bytes into a region of size 6 [-Wformat-truncation=]
    result = snprintf(buffer, PRINT_DIGITS + 1, "%#-*.*f",
                                                 ^~~~~~~
uncertain_upe.c:614:11: note: ‘snprintf’ output between 8 and 317 bytes into a destination of size 6
    result = snprintf(buffer, PRINT_DIGITS + 1, "%#-*.*f",
    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        PRINT_DIGITS, PRINT_DIGITS, var);
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uncertain_upe.c:630:50: warning: ‘%#-*.*f’ directive output truncated writing between 7 and 316 bytes into a region of size 6 [-Wformat-truncation=]
     result = snprintf(buffer, PRINT_DIGITS + 1, "%#-*.*f",
                                                  ^~~~~~~
uncertain_upe.c:630:12: note: ‘snprintf’ output between 8 and 317 bytes into a destination of size 6
     result = snprintf(buffer, PRINT_DIGITS + 1, "%#-*.*f",
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         PRINT_DIGITS, PRINT_DIGITS, covar);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

phillipstanleymarbell avatar Apr 19 '20 08:04 phillipstanleymarbell

Sorry! This code is a bit of a mess from me. I tried to workout my original intention but I have no idea what I was trying to do.

Some thoughts to try and resolve my sense of responsibility for these:

  • I think the PRINT_DIGITS constant is meant to control how many decimal places we use when print the floating point values of variances/covariance.

  • Some of the warnings are spurious as row must be in the range [0, 32) but the compiler has to assume that row might be 2_000_000. That is not to say that we should ignore the warning but only that the solution probably isn't just to increase the padding because then we would have really long rows with whitespace that will never have any contents.

  • Also I think that none of these could cause memory unsafety, because I used snprintf and the PRINT_DIGITS + 1 value I passed as the second argument is always less than the size of buffer. I think this function could print incorrect values to stdout but should segfault etc.

harrysarson avatar Apr 19 '20 11:04 harrysarson