On Sat, May 8, 2021 at 6:54 AM Benno Schulenberg <
bensberg@telfort.nl> wrote:
> Op 08-05-2021 om 08:18 schreef Hussam al-Homsi:
> > - sprintf(info, "%4ju %cB", (intmax_t)result, modifier);
> > + sprintf(info, "%4jd %cB", (intmax_t)result, modifier);
>
> It should not be possible for a file to have a negative size, so %d should
> not be used. The 'result' is of type off_t, which I expect to be unsigned
I expected that, too, but then I found
https://man7.org/linux/man-pages/man7/system_data_types.7.html, which for off_t says:
"Used for file sizes. According to POSIX, this shall be a signed integer type."
So the cast to intmax_t is fine as it matches the signedness, and then "%jd" matches the type.
> > - sprintf(hexadecimal, "U+%04X", (unsigned char)*this_position);
> > + sprintf(hexadecimal, "U+%04hhX", (unsigned char)*this_position);
>
> When using "hh", the cast is superfluous. And the other way around.
> I prefer the cast.
Not really. The length modifier specifies the size of the argument to sprintf().
For hh, page 229 in
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf says:
"Specifies that a following d, i, o, u, x, or X conversion specifier applies to a signed char or unsigned char argument"
By the way, the following lines in src/winio.c should instead cast to (unsigned int) because "%X" expects that...
sprintf(hexadecimal, "U+%04X", (int)widecode);
sprintf(hexadecimal, "|%04X", (int)widecode);
sprintf(hexadecimal, "|%04X", (int)widecode);
...as page 230 in the last link says. Maybe for another patch.