Commit Graph

135 Commits

Author SHA1 Message Date
Matteo Croce
64f7b2961a introduce FOREACH_ELEMENT
Add a FOREACH_ELEMENT() macro which just passes ELEMENTSOF(v)
as third argument to FOREACH_ARRAY().
2024-04-18 17:39:34 +02:00
AtariDreams
d7d6195953
cocci: merge mfree.cocci and mfree_return.cocci (#30838)
It makes sense that these should be one file instead of two, as they both involve mfree()
2024-01-09 12:24:37 +09:00
Frantisek Sumsal
eddecf4f6c coccinelle: add a rule for in_addr_hash_func()
Follow-up for c01a5c0.
2024-01-02 19:12:05 +01:00
Frantisek Sumsal
cd4b16c082 coccinelle: drop a couple of FIXMEs
Turns out Coccinelle can handle compound literals just fine, the parsing
errors were caused by incorrectly parsed macros in code before the
literals, so let's just provide simplified versions for such macros.

The parsing error in `Type *foo[ELEMENTSOF(bar)] = {};` is actually
harmless; it occurs only when creating an array of pointers for a type
that's in an external header and it occurs only on the first parser's
pass, subsequent passes resolve the type correctly.

Also, unset ENABLE_DEBUG_HASHMAP, so Coccinelle doesn't expand the
hashmap debug macros.

As for the remaining FIXMEs, I opened a couple of issues in the
Coccinelle upstream to see if they can be fixed there (or at least
properly analyzed).
2024-01-02 19:12:05 +01:00
Yu Watanabe
0802e9d8de coccinelle: re-indent comments 2023-12-29 04:14:55 +09:00
Yu Watanabe
a1ccd5eed8 coccinelle: fix typo
Follow-up for b25d3b36a2.
2023-12-29 04:13:45 +09:00
Yu Watanabe
51dfa75c6e
Merge pull request #30639 from mrc0mmand/more-cocci-shenanigans
Another batch of Coccinelle tweaks
2023-12-27 20:33:46 +09:00
Frantisek Sumsal
c633361f06 coccinelle: dial back warnings about performance
Turns out I _really_ underestimated the impact of
--include-headers-for-types, as it significantly reduces both runtime
and storage penalties. For example, on my machine the runtime of
uncached run goes down from ~15 minutes to ~2 minutes, and similarly the
total storage needed by the cache goes from ~15 GiB down to ~3 GiB.
2023-12-27 11:15:48 +01:00
Frantisek Sumsal
b25d3b36a2 coccinelle: help Coccinelle with some more complex macros
Drop the original macro file, since it's not needed anymore thanks to
resolving includes properly, but introduce a similar file -
parsing_hacks.h - that helps Coccinelle in some specific corner cases.

This eliminates most of the outstanding parsing errors in source files.
The remaining ones are limitations of the parsing engine (see the FIXMEs
in pasing_hacks.h) and need further investigation.
2023-12-27 11:15:48 +01:00
Frantisek Sumsal
11959eb201 coccinelle: search the system include path for header files as well
Since Coccinelle is originally a kernel tool, it doesn't search the
system include path by default for header files. Without this we're
missing a lot of types provides by stdlib (and other libraries we make
use of).
2023-12-27 11:15:44 +01:00
Frantisek Sumsal
4d3510d00f coccinelle: explicitly undefine SD_BOOT
So Coccinelle doesn't pull in includes guarded by #if SD_BOOT.

For example:

$ head -n5 main.c
 #if FOO
 #include "foo.h"
 #else
 #include "bar.h"
 #endif

$ spatch --verbose-includes --recursive-includes --sp-file zz-drop-braces.cocci main.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: main.c
including ./foo.h
including ./bar.h

$ spatch --verbose-includes --recursive-includes --sp-file zz-drop-braces.cocci main.c --undefined FOO
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: main.c
including ./bar.h
2023-12-26 11:23:19 +01:00
Yu Watanabe
5a4631bd8f coccinelle: fix typo 2023-12-26 09:43:21 +09:00
Yu Watanabe
fe3fcb9492
Merge pull request #30633 from mrc0mmand/cocci-shenanigans
coccinelle: rework how we run the Coccinelle transformations
2023-12-26 05:45:58 +09:00
Frantisek Sumsal
fcd2db31c0 coccinelle: properly drop braces around single-statement if()s 2023-12-25 13:53:02 +01:00
Frantisek Sumsal
6688db4194 coccinelle: fix the log-json rule
As it generated very questionable results.
2023-12-25 13:53:02 +01:00
Frantisek Sumsal
c988ef4cf4 coccinelle: rework how we run the Coccinelle transformations
Turns out that the original way we did things was quite broken, as it
skipped a _lot_ of code. This was because we just threw everything into
one pile and tried to spatch it, but this made Coccinelle sad, like when
man page examples redefined some of our macros, causing typedef
conflicts.

For example, with a minimal reproducer that defines a cleanup macro in
two source files, Coccinelle has no issues when spatch-ing each one
separately:

$ spatch --verbose-parsing --sp-file zz-drop-braces.cocci main.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: main.c
SPECIAL NAMES: adding _cleanup_ as a attribute with arguments
SPECIAL NAMES: adding _cleanup_free_ as a attribute

$ spatch --verbose-parsing --sp-file zz-drop-braces.cocci
logcontrol-example.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: logcontrol-example.c
SPECIAL NAMES: adding _cleanup_ as a attribute with arguments

But when you try to spatch both of them at once, Coccinelle starts
complaining and skipping the "bad" code:

$ spatch --verbose-parsing --sp-file zz-drop-braces.cocci main.c logcontrol-example.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: main.c logcontrol-example.c
SPECIAL NAMES: adding _cleanup_ as a attribute with arguments
SPECIAL NAMES: adding _cleanup_free_ as a attribute
remapping: _cleanup_ to an ident in macro name
ERROR-RECOV: found sync end of #define, line 44
parsing pass2: try again
ERROR-RECOV: found sync end of #define, line 44
parse error
 = File "logcontrol-example.c", line 44, column 21, charpos = 1719
  around = '__attribute__',
  whole content = #define _cleanup_(f) __attribute__((cleanup(f)))
badcount: 2
bad: #include <systemd/sd-journal.h>
bad:
BAD:!!!!! #define _cleanup_(f) __attribute__((cleanup(f)))

This was, unfortunately, hidden as it is visible only with
--verbose-parsing (or --parse-error-msg).

Another issue was how we handled includes. The original way of throwing
them into the pile of source files doesn't really work, leading up to
similar issues as above. The better way is to let Coccinelle properly
resolve all includes by telling it where to find our own include files
(basically the same thing we already do during compilation).

After fixing all this, Coccinelle now has a chance to process much more
of our code (there are still some issues in more complex macros, but
that requires further investigation). However, there's a huge downside
from all of this - doing a _proper_ code analysis is surprisingly time
and resource heavy; meaning that processing just one Coccinelle rule now
takes 15 - 30 minutes.

To make this slightly less painful, Coccinelle supports caching the
generated ASTs, which actually helps a lot - it gets the runtime of one
rule from 15 - 30 minutes down to ~1 minute. It, of course, has its own
downside - the cache is _really_ big (ATTOW the cache takes ~15 GiB).

However, even with the aggressive AST caching you're still looking at
~1 hour for one full Coccinelle run, which is a bit annoying, but I
guess that's the price of doing things _properly_ (but I'll definitely
look into ways of further optimizing this).
2023-12-25 13:52:42 +01:00
Mike Yuan
2413a0fab4
format-table: introduce table_isempty and use it where appropriate 2023-12-25 17:47:18 +08:00
Yu Watanabe
c01a5c0527 siphash24: introduce siphash24_compress_typesafe() macro
To prevent copy-and-paste mistake.

This also introduce in_addr_hash_func().

No functional change, just refactoring.
2023-12-25 15:38:59 +09:00
Yu Watanabe
6ae5d4b52f coccinelle: convert hashmap_size() == 0 or friends
Addresses https://github.com/systemd/systemd/pull/30567#issuecomment-1866631816.
2023-12-25 00:47:42 +09:00
Frantisek Sumsal
b3bfb95178 coccinelle: don't run iovec-make on iovec_done{,_erase}
As the result is a bit funky (but still valid), i.e.:

 static inline void iovec_done_erase(struct iovec *iovec) {
         assert(iovec);

-        iovec->iov_base = erase_and_free(iovec->iov_base);
-        iovec->iov_len = 0;
+        *iovec = IOVEC_MAKE(erase_and_free(iovec->iov_base), 0);
 }
2023-10-25 11:16:37 +02:00
Zbigniew Jędrzejewski-Szmek
13d84288bc tree-wide: use cocinnelle to apply _NEG_ macros 2023-08-16 12:52:56 +02:00
Mike Yuan
906682a1de coccinelle/take-fd: match for -EBADF instead of -1
Follow-up for 254d1313ae
2023-07-09 14:07:48 +09:00
Frantisek Sumsal
64254629f7 coccinelle: add a transformation for GNU conditionals
i.e. x ? x : y => x ?: y
2023-03-18 14:23:11 +01:00
Frantisek Sumsal
d3a2a25fb4 coccinelle: respect spacing from the semantic patch 2023-03-18 14:23:11 +01:00
Frantisek Sumsal
19615a650b coccinelle: skip the empty-to-null transformation on the macro itself
Since the empty_to_null() function was "macrofied", we need to use a bit
of black magic to make Coccinelle avoid running the transformation on
the macro itself.

Follow-up to ef2409cbde.
2023-01-25 11:35:06 +01:00
Zbigniew Jędrzejewski-Szmek
254d1313ae tree-wide: use -EBADF for fd initialization
-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
2022-12-19 15:00:57 +01:00
Zbigniew Jędrzejewski-Szmek
2053593fcc treewide: drop "RUN_" from "RUN_WITH_UMASK"
RUN_WITH_UMASK was initially conceived for spawning externals progs with the
umask set. But nowadays we use it various syscalls and stuff that doesn't "run"
anything, so the "RUN_" prefix has outlived its usefulness.
2022-12-13 14:39:30 +01:00
Zbigniew Jędrzejewski-Szmek
64903d18df basic/list: drop LIST_IS_EMPTY
This was a trivial wrapper that didn't provide any added value. With more
complicated structures like strvs, hashmaps, sets, and arrays, it is possible
to have an empty container. But in case of a list, the list is empty only when
the head is missing.

Also, we generally want the positive condition, so we replace many
if (!LIST_IS_EMPTY(x)) with just if (x).
2022-07-02 12:46:16 +02:00
Frantisek Sumsal
a0aca8821c coccinelle: don't try to use IN_SET() in assert_cc()
Since assert_cc() requires an integral constant expression.
2022-05-30 18:17:11 +02:00
Lennart Poettering
ec93eb48f2 coccinelle: automatically look for timestamp_is_set candidates
Based on @mrc0mmand's Coccinelle skills, not mine. All credit is his.

Co-authored-by: Frantisek Sumsal <frantisek@sumsal.cz>
2022-02-22 22:47:12 +01:00
Lennart Poettering
96ca229517 coccinelle: automatically switch some uses of memcpy() → mempcpy()
Inspired by #22520, let's add a coccinelle script that converts this
automatically.
2022-02-16 17:26:26 +01:00
Yu Watanabe
cbf8fc90d5 list: drop unnecessary line continuation
Fixes #21886.
2021-12-25 00:47:37 +09:00
наб
f1e6f93372
Change all fixed-path bash shebangs to /u/b/env bash outside test/ 2021-12-12 21:13:50 +01:00
Lennart Poettering
2f82562bad alloc-util: add strdupa_safe() + strndupa_safe() and use it everywhere
Let's define two helpers strdupa_safe() + strndupa_safe() which do the
same as their non-safe counterparts, except that they abort if called
with allocations larger than ALLOCA_MAX.

This should ensure that all our alloca() based allocations are subject
to this limit.

afaics glibc offers three alloca() based APIs: alloca() itself,
strndupa() + strdupa(). With this we have now replacements for all of
them, that take the limit into account.
2021-10-14 15:57:52 +02:00
Zbigniew Jędrzejewski-Szmek
64b92d637c licensing: add spdx to our .cocci files
Since those are chunks of code based on our codebase, it's easiest to use the
same license.
2021-10-01 14:45:00 +02:00
Zbigniew Jędrzejewski-Szmek
186b9041ae ci: use LGPLv2+ for all our ci configuration 2021-10-01 14:45:00 +02:00
Frantisek Sumsal
8370da9ea6 ci: shellcheck-ify CI scripts 2021-09-29 22:24:12 +02:00
Frantisek Sumsal
ca21d59a3f coccinelle: filter out a couple of 'false-positive' transformations
* flag-set.cocci: perform the transformation only if the second
    argument is a constant
  * sd-journal/lookup3.c: skip the cocci completely for this file, since
    it's not "ours"
  * strjoina.cocci: skip the transformation on the "test_strjoina" test,
    since it intentionally tests the "incorrect" expression we're trying to
    transform (the same thing was already done in strjoin.cocci)
2021-03-18 11:59:53 +01:00
Yu Watanabe
61c26ca87f coccinelle: ignore specific cases to use SYNTHETIC_ERRNO() macro 2020-11-27 14:35:20 +09:00
Yu Watanabe
1c1729c9be coccinelle: add rules for log_unit_error_errno() or friends 2020-11-27 14:35:20 +09:00
Yu Watanabe
0aa8730edc coccinelle: always use SYNTHETIC_ERRNO() macro 2020-11-20 02:59:00 +09:00
Yu Watanabe
a61c0bdf39 coccinelle: add one more rule to use return value of log_xxx_errno() 2020-11-20 02:57:26 +09:00
Frantisek Sumsal
44e66de0f2 coccinelle: introduce drop-braces transformation
to drop braces around single-line if statements. Also, prefix it with
zz- so it runs as the last one, so it's able to fix stuff tweaked by
previous transformations.
2020-10-09 15:02:20 +02:00
Frantisek Sumsal
7e97526421 coccinelle: check for invalid errno comparisons
Prompted by #15868
2020-10-09 14:48:44 +02:00
Frantisek Sumsal
447643130c coccinelle: correctly resolve our own macros
Coccinelle can't do this automagically and requires we supply it
respective header files. Unfortunately, the option for this
(--macro-file=) can be used only once, so let's create our own
macro file by collecting macros needed for the semantic parser
to be happy.
2020-10-09 14:48:40 +02:00
Frantisek Sumsal
135a9868a7 coccinelle: skip strjoin transformation in test_strjoin() 2020-10-04 12:32:21 +02:00
Frantisek Sumsal
1813613fed coccinelle: limit the # of expressions in in_set/not_in_set
transformations. Otherwise the time and resources to generate all
possible permutations is simply unreasonable for running on local
machines.
2020-10-04 12:32:21 +02:00
Frantisek Sumsal
cb60571b31 coccinelle: skip the xsprintf transformation on man pages
since we don't expose xsprintf to users.
2020-10-04 12:32:21 +02:00
Frantisek Sumsal
aad54dbc74 coccinelle: disable flags-set where it doesn't make sense 2020-10-04 12:32:21 +02:00
Frantisek Sumsal
473de9b708 coccinelle: fix the equals-null transformation
The original issue with this transformation was that we were replacing
the whole if statement instead of just the expression inside. That
caused the code to be weirdly formatted, as Coccinelle put a new block
around each replaced if statement.

This version replaces just the inner expression if it's in its incorrect
form, otherwise it just accepts it (to avoid recursion).
2020-10-04 12:32:21 +02:00