Commit Graph

6611 Commits

Author SHA1 Message Date
rillig 134116ce30 lint: clean up 2024-03-28 21:04:48 +00:00
rillig 8910413e27 lint: clean up; extend overflow test 2024-03-27 20:09:43 +00:00
rillig e57698da95 lint: don't use 'long' in diagnostics
The size of 'long' differs between 64-bit and 32-bit platforms.
Eliminate this possible platform-dependency.
2024-03-27 19:28:20 +00:00
rillig e6155eab48 t_strptime: fix typo in test description 2024-03-26 21:52:23 +00:00
rillig 216c89c02b lint: fix warnings about loss of accuracy on bit-field operations 2024-03-25 23:39:13 +00:00
rillig 6167e0cc12 tests/lint: demonstrate wrong warnings about lossy bit field operations 2024-03-25 22:46:23 +00:00
rillig 64230f3217 lint: rename snprintb 'directives' to 'conversions'
This aligns the terminology with the snprintf function.
2024-03-25 22:37:43 +00:00
rillig b050579e9d snprintb: mark the end of the buffer if the buffer is too small
This avoids confusion in case the buffer ends with an incomplete number.
2024-03-25 20:39:26 +00:00
martin 56e0726f28 The fetestexcept_trap test case only makes sense on FPUs that implement
exceptions.
2024-03-18 16:33:54 +00:00
riastradh 086a7f840a strptime(3): Exercise some edge cases in the automatic tests.
Unfortunately, we can't quite use strptime as a black box to detect
the cases that triggered undefined behaviour, because strptime just
fails in that case anyway since the number that would go in .tm_year
is far out of the representable range.

PR lib/58041
2024-03-16 00:06:37 +00:00
riastradh f1f68b211d libcrypto: Fix buffer overrun in truncated SHA-512 functions.
Further fallout from the libc/openssl sha2 symbol collision.

PR lib/58039
2024-03-15 18:10:37 +00:00
riastradh 1e152b590b libcrypto: Add some trivial tests for truncated SHA-512 variants.
These should use more of the test vectors from

https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#Testing

but this will do for now to detect the buffer overrun rake we left
lying around for ourselves.

PR lib/58039
2024-03-15 15:32:07 +00:00
rillig 083828859c stat: don't allow numeric formatting flags on strings
Calling snprintf with "%+s" invokes undefined behavior.
2024-03-14 21:17:54 +00:00
rillig 897cdf26ba tests/stat: test the left-aligned and right-aligned 'S' format 2024-03-14 21:00:32 +00:00
rillig 9ca08a6937 tests/lint: ensure that lint correctly decays array parameter types 2024-03-13 06:56:24 +00:00
rillig 9ed4dea22d tests/lint: extend test for previously wrong warning about bit-fields 2024-03-12 20:35:29 +00:00
rillig 9b239cc71c lint: fix warning about loss of conversion for unsigned bit-fields
Since decl.c 1.180 from 2021-05-02.
2024-03-12 07:56:08 +00:00
rillig fc2f5e14be tests/lint: demonstrate wrong warning when comparing a bit-field
Since decl.c 1.180 from 2021-05-02, which allowed arbitrary integer
types to be used as the underlying type for bit-fields in GCC mode.
2024-03-12 07:29:39 +00:00
rillig dd848dec1a lint: add details to the message about integer overflow
Having only the operator was too unspecific to be actionable, so add the
actual numbers and the data type.
2024-03-10 19:45:14 +00:00
rillig bd9743d3db lint: saturate signed integer overflow
In array address calculations, this prevents a 'array subscript cannot
be negative' for large array subscripts.
2024-03-10 16:06:13 +00:00
rillig 08d1bd5c72 lint: fix integer overflow detection
Previously, an unsigned operation that had a negative result went
undetected in a few cases. Now, all results that are not representable
by their type are considered overflows.

The implementation of signed shift-right had been wrong for a few
commits.
2024-03-10 15:49:12 +00:00
rillig 51858eeb3e lint: detect more cases of integer overflow in constant expressions
For unsigned integers, detect when 'a + b' wraps around.
2024-03-10 14:32:30 +00:00
rillig 7d57e750b8 tests/lint: enable test for integer overflow in array index 2024-03-10 10:39:19 +00:00
rillig 25f6e58591 lint: remove wrong warning about overflow in unary '-' for unsigned 2024-03-10 10:31:29 +00:00
rillig 34885a599d lint: fix integer overflow in integer overflow check 2024-03-10 10:15:51 +00:00
rillig 16f849b643 tests/lint: test integer overflow when folding constants
The test is still incomplete, yet it has discovered several bugs that
are worth fixing.
2024-03-10 09:58:30 +00:00
rillig 3e856647d2 lint: fix excessive overflow warning after division by zero 2024-03-09 23:55:11 +00:00
rillig 2b5c0f6a77 tests/lint: demonstrate integer overflow on lp64 platforms 2024-03-09 17:34:01 +00:00
rillig d6cf182bfe tests/lint: test check for out-of-bounds array index 2024-03-09 16:47:09 +00:00
rillig e572db3830 snprintb: error out on unknown new-style formatting directive 2024-03-04 21:35:28 +00:00
rillig 5926386128 lint: clean up string parsing and snprintb check 2024-03-03 16:09:01 +00:00
rillig 3d5fc26390 lint: warn about escaped snprintb directive
Repurpose message 362, as the previous version was redundant since null
bytes in old-style formats are already covered by message 371 (bit
position out of range) and 377 (redundant '\0' at the end).
2024-03-03 13:09:22 +00:00
rillig a8d07ea3d1 lint: fix warning about "empty" single-letter snprintb descriptions 2024-03-03 10:27:18 +00:00
rillig c9de07fbb0 lint: check for unreachable bits and fields in snprintb formats
While here, clean up a few existing checks.
2024-03-03 00:50:41 +00:00
rillig 1ec17c714c lint: check snprintb formats for overlapping bits and fields 2024-03-02 11:56:37 +00:00
rillig 38c0bdf232 lint: test format strings from snprintb calls
The functions snprintb and snprintb_m are specific to NetBSD, and their
format strings are tricky to get correct.  Provide some assistance in
catching the most common mistakes.
2024-03-01 19:39:28 +00:00
rillig 9e211f3599 tests/lint: remove redundant documentation 2024-03-01 17:22:55 +00:00
rillig d13846be6a tests/snprintb: clean up tests
Always write the value to be formatted in hexadecimal, to make it easily
distinguishable from the buffer size and maximum line length.

Use consistent wording in the comments of the test cases. Be stricter
about mistakes in a format.

Fix some wrong numbers in the snprintb_m tests for limited line length.
2024-02-24 13:00:00 +00:00
rillig e7ed3a67af snprintb: allow non-ASCII descriptions on all platforms
Previously, these descriptions were only allowed on platforms where
plain 'char' is unsigned. On platforms where plain 'char' is signed,
they invoked undefined behavior or terminated the output early.
2024-02-24 12:40:00 +00:00
rillig 69c5b3b0c1 snprintb: always null-terminate output
Always null-terminate the output in the buffer, even in error cases. The
wording in the manual page has been promising this since 2008. For
snprintb_m, ensure that the output is terminated with two null
characters, to gracefully handle situations in which the caller does not
check whether snprintb returned an error.

If the buffer size is zero, allow the buffer to be a null pointer,
analogous to snprintf.

Fix an out-of-bounds memory read if the bitfmt ends with a '*' directive
(since today).

In the tests, merge the helper functions for snprintb, snprintb_m, as
they were similar enough.

Fix a few 'line_max exceeded' tests, ensuring that they output a '#'
marker, and that the 'complete' tests don't.
2024-02-22 21:04:23 +00:00
rillig d101133e9b snprintb: error out on out-of-bounds bit shifts
Previously, these invoked undefined behavior, now they lead to an early
return. An example of out-of-bounds bit number is in SCZ_PCICTRL_BITS.
Bit fields that extend beyond the msb are still allowed.

Allow 'f' and 'F' to have fields that are 64 bits wide. This only makes
sense when the field starts at bit 0.

Remove the unused 'val_len', it was only needed before snprintb.c 1.20.
2024-02-22 18:26:15 +00:00
rillig c235936e8d tests/snprintb: test '=' and '*' directives that can never match 2024-02-20 21:45:36 +00:00
rillig 12034681d8 snprintb_m: limit length of generated strings
The manual page promises that the 'max' argument limits the length of
the generated strings, so don't generate any strings that are longer
than that limit. Mark incomplete strings with a trailing '#' to make
them easily recognizable.
2024-02-20 20:31:56 +00:00
rillig c38e738e50 tests/snprintb: fix out-of-bounds memory read (since 2024-02-16)
Before t_snprintb.c 1.20, the buffer size was required to be greater
than zero. Allowing the buffer size to be zero led to buf[-1] being
checked. On amd64, that byte happened to be 0, on i386 it didn't.

Fixes PR lib/57951.
2024-02-20 19:49:10 +00:00
riastradh fa59e25364 fenv(3): Fix fetestexcept to avoid side effects on trap state.
PR port-amd64/57949
2024-02-20 03:53:48 +00:00
rillig 8f391120f6 snprintb: fix how named bit-fields are split into multiple lines 2024-02-19 23:30:56 +00:00
rillig db5059c208 tests/snprintb: add more tests, especially for snprintb_m 2024-02-19 23:22:03 +00:00
riastradh 37dc27dad0 fenv(3): Add test for PR port-amd64/57949. 2024-02-19 23:19:10 +00:00
riastradh 3dfc28084a longjmp(3) t_sigstack: Fix fencepost error.
Extremely unlikely to cause trouble, but let's just turn that into
`never' to keep it easier for readers.

PR lib/57946
2024-02-19 19:43:27 +00:00
riastradh 9ba2d744e7 longjmp(3) t_sigstack: Use a sigaltstack per handler entry.
longjmp evidently doesn't reset the state of whether the process is
executing on the alternate signal stack.  So when we re-enter the
signal handler, the alternate stack appears to be still in use, and
the system chooses the original stack for the second call to the
signal handler -- which trips our assertion asking to verify that the
signal handler is always using an alternate stack.

Not strictly necessary for the signal handler to use an alternate
stack on re-entry, but this makes it clearer that the signal handler
itself is always using the alternate stack so we can verify that the
interrupted code is _not_ in the signal handler.

With this change, the test now passes on aarch64.

PR lib/57946
2024-02-19 13:34:48 +00:00
riastradh 81899325cc longjmp(3) t_sigstack: Note aarch64 seems to DTRT.
But only by code inspection; it appears to have another problem: on
re-entry, the signal handler is called on the normal stack, not on
the alternate signal stack.

PR lib/57946
2024-02-19 12:41:27 +00:00
riastradh 2bb6194384 longjmp(3) t_sigstack: Print which entry failed.
PR lib/57946
2024-02-19 12:41:19 +00:00
riastradh 3026ea4883 longjmp(3): Test signal mask vs stack restore with siglongjmp too.
PR lib/57946
2024-02-19 12:29:48 +00:00
riastradh 6514aab6b2 longjmp(3): Paranoia: more error checking in PR lib/57946 test. 2024-02-19 04:33:21 +00:00
riastradh a7b033762e longjmp(3): Add test for PR lib/57946. 2024-02-19 04:30:38 +00:00
rillig 0b94bf4814 snprintb: eliminate two local variables, allow zero-size buffer
Like snprintf, the buffer size may be zero. Unlike snprintf, the buffer
must not be NULL.
2024-02-16 19:53:40 +00:00
rillig c277ab1882 snprintb: fix integer overflow when writing past a small buffer
Previously, snprintb returned -1 in this case, assuming that the
snprintf used in FMTSTR validates against an overly large buffer size.
2024-02-16 19:20:38 +00:00
rillig 9ec1abff9d tests/snprintb: fix test for 8-bit field value
The previous version tested the behavior on a platform with signed
'char' before the portability fix.
2024-02-16 18:13:47 +00:00
rillig 889b1738f6 snprintb: fix '=' and ':' for 8-bit values on platforms with signed char
Previously, '=' and ':' worked only on platforms where plain 'char' is
unsigned. On platforms where plain 'char' is signed, only 7-bit values
worked.
2024-02-16 18:09:15 +00:00
rillig 935bb26983 tests/snprintb: clean up existing tests, add more tests
Due to the check that any bytes beyond the expected output must be
unmodified, there's no need anymore to explicitly write the "ZZZ" at the
end of the expected output. While here, remove the redundant trailing
"\0".

Add more tests to cover possible situations where an out-of-bounds write
may have occurred. In some cases, the line length specified in
snprintb_m is exceeded.
2024-02-16 01:19:53 +00:00
rillig 26844513e8 snprintb: fix string termination (since today)
In the previous commit, I had accidentally only run the tests for
snprintb_m but not those for snprintb, thereby missing a newly
introduced bug that would not null-terminate the resulting strings.

Add more tests to cover similar situations in which the buffer is too
small to contain the complete output.
2024-02-15 23:48:51 +00:00
rillig 795d5f45da snprintb: fix out-of-bounds write 2024-02-15 22:37:10 +00:00
rillig 1fb4457e9f tests/libutil/snprintb_m: add tests for small line length 2024-02-14 20:51:17 +00:00
andvar 1cd43426d5 Fix various typos in comments, log messages and documentation. 2024-02-10 18:43:51 +00:00
andvar 100a3398b8 fix spelling mistakes, mainly in comments and log messages. 2024-02-09 22:08:30 +00:00
rillig d06187ed85 tests/lint: sync test for integer constant type on ILP32 platforms
The test was broken since lex.c 1.214 from today.
2024-02-07 22:59:28 +00:00
rillig 26428579ad lint: unify rules for determining the type of an integer constant
Previously, in traditional C mode, large decimal numbers were treated as
unsigned, which disagreed with the book from 1978.
2024-02-07 07:42:50 +00:00
rillig 139c6401cd lint: warn about short octal escape followed by '8' or '9'
These mistakes is typically found in snprintb format directives but is
probably a typo everywhere else as well.
2024-02-03 20:10:10 +00:00
rillig 2b097c752f tests/lint: extend tests for hexadecimal character escapes 2024-02-03 19:18:36 +00:00
rillig 29354841ac lint: allow hexadecimal character escapes up to UINT_MAX
Previously, any value larger than INT_MAX was regarded as negative and
thus terminated the loop around get_escaped_char.
2024-02-03 18:58:05 +00:00
rillig 661078afe6 lint: remove unused code for converting multibyte character sequences 2024-02-03 10:56:18 +00:00
rillig c1862de9dd lint: allow larger numbers in escapes for wide characters and strings 2024-02-03 10:01:58 +00:00
rillig bc5123edee tests/lint: test octal escapes in character and wide strings 2024-02-03 09:36:14 +00:00
rillig 03951da31e lint: use the locale for interpreting wide character strings 2024-02-02 23:36:01 +00:00
rillig 3454588e0f tests/lint: test UTF-8 mode 2024-02-02 23:30:38 +00:00
rillig 708a1296c5 tests/lint: add UTF-8 test for string literals 2024-02-02 22:45:48 +00:00
andvar 34908c4889 fix various typos in comments. 2024-02-02 22:39:09 +00:00
andvar 82befdcba0 s/shold/should/ 2024-02-02 21:52:22 +00:00
rillig 183f84fe87 tests/lint: test lexing of characters and strings 2024-02-02 19:07:57 +00:00
rillig 8016349204 lint: fix debug output for strings, reduce code size
The debug output contained too many newlines.

The buffer functions were built into lint2 even though they weren't
used.

Enable the query for invisible characters in string literals, to make
sure that a newline in a string literal does not trigger that query.
2024-02-02 16:25:58 +00:00
rillig 402b4579b3 lint: make newline in a string literal an error in C90 and later 2024-02-02 16:05:37 +00:00
martin e9be82c8ac Skip the dtb set if the /boot partition is not mounted. 2024-01-30 16:57:32 +00:00
riastradh f17b3abd13 tests/usr.bin/mtree: Fixed the set lists, no more xfail.
PR misc/57877
2024-01-28 19:08:06 +00:00
rillig 7caac60117 tests/lint: enforce diagnostics to be listed in chronologic order
While here, fix the tests on uchar platforms.
2024-01-28 08:54:27 +00:00
rillig ea7127375b tests/lint: sort diagnostics in ilp32 tests chronologically 2024-01-28 08:26:07 +00:00
rillig d2c16d5796 tests/lint: sort multiple diagnostics per line chronologically
For now, the chronologic order is not enforced but has to be established
manually, for example by removing all 'expect' comment lines and
regenerating them with 'accept.sh -u'.

While here, clean up a few instances that came up when regenerating the
'expect' comments, such as wrong indentation or needless deviation from
the 'expect+1' form.
2024-01-28 08:17:27 +00:00
rillig 5357c932ab tests/lint: fix tests for integer constants on ILP32 platforms
The comment in msg_218 was both off-topic and wrong, so remove it.
2024-01-28 06:57:41 +00:00
rillig fc60683a64 tests/libutil/snprintb: remove redundant integer constant suffixes
I added these because lint told me so. Lint was wrong though, as it had
not implemented the C99 rules for integer constants.
2024-01-27 21:42:29 +00:00
rillig 8c7fbc585c lint: fix type of large integer constants 2024-01-27 20:03:14 +00:00
rillig a4d99aada1 tests/lint: test integer constants in different language levels 2024-01-27 15:10:56 +00:00
rillig 59290883cd tests/libutil/snprintb: fix test for 32-bit platforms 2024-01-27 10:32:03 +00:00
rillig c7aa2e0520 tests/libutil/snprintb: add more tests
Test all number bases (octal, decimal, hexadecimal), in both old-style
and new-style formats, as well as small buffer sizes.

Document working edge cases such as empty descriptions or descriptions
containing spaces, as well as situations that invoke undefined behavior.

Add comments to the individual test cases, as reading the actual format
strings takes more time.
2024-01-27 08:54:15 +00:00
riastradh e8c974429a tests/usr.bin/mtree: xfail PR 57877; omit needless debug print. 2024-01-26 00:32:46 +00:00
rillig efceecc2b3 tests/libutil/snprintb: clean up and extend tests
In case of a failure, print the details of the test case, including file
and line number of the actual test data. Do not write the format strings
directly to the output, as they contain non-printable bytes and embedded
null bytes.

After a failed test case, continue with the others.

Lay out the format strings according to their structure, to make them
more readable. Remove redundant "\0" at the end of the new-style format
strings.

Fix an off-by-one error in the test data: 0xf is FIFTEEN, not SIXTEEN.

Add a test for performing a restricted subset of rot13 in the format
string, to explore the limits of snprintb formatting.

What's still missing are tests for edge cases and error cases.
2024-01-25 18:13:14 +00:00
riastradh 00b317fce9 mtree(8): Test the installed sets.
Except etc and xetc, which likely won't match for reasons that aren't
great, like etc including empty log files which in an installed
system have probably changed.

This test will probably fail, but we should make sure it doesn't!

PR misc/57877
2024-01-25 00:30:57 +00:00
rillig 09702fc7eb lint: rename symt_t to symbol_kind
It was confusing to have two kinds of "symbol type" (s_type and s_symt),
so rename all related identifiers to be more distinctive.

No functional change.
2024-01-23 19:44:28 +00:00
christos 16b546eeb6 Add range tests with trailing characters. 2024-01-20 16:52:41 +00:00
rillig 45efdafbcc lint: add query for const automatic variables 2024-01-20 10:02:31 +00:00
rillig 6f23d4df44 lint: allow '\e' only in GCC mode 2024-01-19 19:23:34 +00:00
andvar b433853d68 s/rouding/rounding/ in test description. 2024-01-14 12:44:09 +00:00