tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: [PATCH] Fix printf(1) for integer larger than INTMAX_MAX



    Date:        Mon, 26 Oct 2020 23:41:20 +0900
    From:        Rin Okuyama <rokuyama.rk%gmail.com@localhost>
    Message-ID:  <d2a076a9-67de-77de-29df-d86aeadc7d6e%gmail.com@localhost>

  | Ah, I didn't know ulong is intentionally. Thanks, I withdraw this part.

It would be useful if you committed the rest of the printf.c changes,
as that is fixing a real bug.

  | Hmm, it seems like tautology if printf(1) itself is used for this test.
  | How about this autoconf like method?

The tests are important, but slightly less so ...

  | cat > test.c << EOF
  | #include <sys/types.h>
  | int main(void) { __CTASSERT(sizeof(intmax_t) == 8); return 0; }
  | EOF

Something like that is one way, though it requires a c compiler.

Using printf to find out what it should be capable of isn't
necessarily a problem though.

The point of the tests is for something to fail if the code breaks.
While it can be useful if the thing that breaks indicates precisely
what the problem is, that actually happens rarely - all that really
matters is that we discover that something is broken, so someone can
investigate and find out what.

That is, we can assume (I believe) that we're only ever going to have
a power of two sized intmax_t type (ie: log2(sizeof(intmax_t)) will be
an integer), so we can do some very crude tests using "middle" values
to guess what sizeof(intmax_t) is - test that there's no error for a
value much smaller than a possible limit, and that there is an error
for a value much bigger (if not, either way guessed possible limit is
wrong).   Once the likely limit is ascertained, then do a bunch of
boundary tests, test the biggest value that should work, and also one
bigger (which should generate an overflow error).   If those tests
work, then we guessed the range correctly, and all is OK (obviously
there can be a bunch more tests of more corner cases and extreme values).
If either of the other tests fail, then either the overflow handling
is broken one way or the other, or the code guessed the size incorrectly,
and someone can investigate and work out which.

kre



Home | Main Index | Thread Index | Old Index