Commit graph

111 commits

Author SHA1 Message Date
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
Frantisek Sumsal 3bc3c734c6 coccinelle: drop the custom isomorphisms
My former dumb me didn't read the documentation properly, so with the
introduction of custom isomorphisms I caused two issues:

1) Masked all standard isomorphisms defined by Coccinelle
2) Replace the original issue with a completely new one
2020-10-04 12:32:21 +02:00
Lennart Poettering 14eb3285ab execute: use empty_to_root() a bit more 2020-10-01 11:02:11 +02:00
Zbigniew Jędrzejewski-Szmek de7fef4b6e tree-wide: use set_ensure_put()
Patch contains a coccinelle script, but it only works in some cases. Many
parts were converted by hand.

Note: I did not fix errors in return value handing. This will be done separate
to keep the patch comprehensible. No functional change is intended in this
patch.
2020-06-22 16:32:37 +02:00
Frantisek Sumsal e4ff03935c tree-wide: formatting tweaks reported by Coccinelle 2020-04-21 23:21:04 +02:00
Lennart Poettering 9228fef0d6 tree-wide: use empty-to-root a bit more 2019-07-16 12:40:22 +02:00
Zbigniew Jędrzejewski-Szmek 1d3fe304fd Use sd_event_source_disable_unref() 2019-05-10 16:55:37 +02:00
Frantisek Sumsal ccd52940d0 coccinelle: further restrict certain transformations
Some transformations generate results we don't want to keep, so
let's disable such transformations for specific files.

Also, disable const-strlen.cocci everywhere, as the STRLEN macro has a
pretty limited scope, so the transformation generates false positives in
most cases.
2019-04-30 09:39:13 +02:00
Frantisek Sumsal 1f72479037 coccinelle: exclude JsonVariant* from the IN_SET transformation
JsonVariant* doesn't work with the current IN_SET implementation, so
let's exclude it from the transformation altogether
2019-04-30 09:39:13 +02:00
Frantisek Sumsal 4a4eaade60 coccinelle: exclude certain paths from the transformations
There's no point in running these transformation for certain files,
mainly anything from src/boot/efi and src/shared/linux, as this code
doesn't have access to our internal utility functions
2019-04-29 15:38:53 +02:00
Frantisek Sumsal 60d9959dd8 coccinelle: ignore function transformations causing recursion
For example, following transformation:

- isempty(s) ? NULL : s
+ empty_to_null(s)

would get applied to the empty_to_null function itself as well,
causing an infinite recursion, like:

--- src/basic/string-util.h
+++ /tmp/cocci-output-307-9f76e6-string-util.h
@@ -50,11 +50,11 @@ static inline bool isempty(const char *p
 }

 static inline const char *empty_to_null(const char *p) {
-        return isempty(p) ? NULL : p;
+        return empty_to_null(p);
 }

Let's avoid that by checking the current match position
2019-04-29 15:38:53 +02:00
Frantisek Sumsal 33af88cf70 coccinelle: ignore macro transformations in the macros themselves
For example, the following transformation:

- sizeof(s)-1
+ STRLEN(s)

would replace sizeof by STRLEN even in the STRLEN macro definition
itself, which generates following nonsensical patch:

--- src/basic/macro.h
+++ /tmp/cocci-output-8753-b50773-macro.h
@@ -182,7 +182,7 @@ static inline unsigned long ALIGN_POWER2
  *          Contrary to strlen(), this is a constant expression.
  * @x: a string literal.
  */
-#define STRLEN(x) (sizeof(""x"") - 1)
+#define STRLEN(x) (STRLEN("" x ""))

 /*
  * container_of - cast a member of a structure out to the containing structure

Let's exclude the macro itself from the transformation to avoid this
2019-04-28 22:11:15 +02:00
Frantisek Sumsal 17e3e37c05 coccinelle: avoid matching 'errno' as a file descriptor
The `coccinelle/take-fd.cocci` transformation file attempts to rewrite

r = fd;
fd = -1;

to

r = TAKE_FD(fd);

Unfortunately, using `identifier` or `idexpression` as a metavariable
type in this case wouldn't match more complex location descriptions,
like:

x->fd = fd
fd = -1;

Using 'expression' metavariable type generates false positives,
as you can't specify scope of such expression. The only real example
from the current codebase is the global 'errno' variable, which results
in following patch generated by `spatch`:

--- src/basic/errno-util.h
+++ /tmp/cocci-output-28263-971baa-errno-util.h
@@ -15,8 +15,7 @@ static inline void _reset_errno_(int *sa

 #define UNPROTECT_ERRNO                         \
         do {                                    \
-                errno = _saved_errno_;          \
-                _saved_errno_ = -1;             \
+                errno = TAKE_FD(_saved_errno_);             \
         } while (false)

 static inline int negative_errno(void) {

Let's explicitly state that the matched expression should not equal
'errno' to avoid this. It's not particularly nice, but it should be
enough, at least for now.
2019-04-27 15:46:48 +02:00
Frantisek Sumsal b3fd7b53ff coccinelle: add explicit statement isomorphisms
Coccinelle needs a custom isomorphism file with rules (isomorphisms) how
to correctly rewrite conditions with explicit NULL checks (i.e.
if (ptr == NULL)) to their shorter form (i.e. if (!ptr)). Coccinelle
already contains such isomorphisms in its default .iso file, however,
they're in the opposite direction, which results in useless output from
coccinelle/equals-null.cocci.

With this fix, `spatch` should no longer report patches like:

@@ -628,8 +628,9 @@ static int path_deserialize_item(Unit *u
                 f = path_result_from_string(value);
                 if (f < 0)
                         log_unit_debug(u, "Failed to parse result value: %s", value);
-                else if (f != PATH_SUCCESS)
-                        p->result = f;
+                else {if (f != PATH_SUCCESS)
+                                p->result = f;
+                }

         } else
                 log_unit_debug(u, "Unknown serialization key: %s", key);
2019-04-27 15:26:11 +02:00
Lennart Poettering 3661dc349e
Merge pull request #12217 from keszybz/unlocked-operations
Refactor how we do unlocked file operations
2019-04-12 13:51:53 +02:00
Zbigniew Jędrzejewski-Szmek 2fe21124a6 Add open_memstream_unlocked() wrapper 2019-04-12 11:44:57 +02:00
Zbigniew Jędrzejewski-Szmek 02e23d1a1a Add fdopen_unlocked() wrapper 2019-04-12 11:44:57 +02:00
Zbigniew Jędrzejewski-Szmek 41f6e627d7 Make fopen_temporary and fopen_temporary_label unlocked
This is partially a refactoring, but also makes many more places use
unlocked operations implicitly, i.e. all users of fopen_temporary().
AFAICT, the uses are always for short-lived files which are not shared
externally, and are just used within the same context. Locking is not
necessary.
2019-04-12 11:44:56 +02:00
Zbigniew Jędrzejewski-Szmek fdeea3f4f1 Add fopen_unlocked() wrapper 2019-04-12 11:44:52 +02:00
Zbigniew Jędrzejewski-Szmek cc5549ca12 scripts: use 4 space indentation
We had all kinds of indentation: 2 sp, 3 sp, 4 sp, 8 sp, and mixed.
4 sp was the most common, in particular the majority of scripts under test/
used that. Let's standarize on 4 sp, because many commandlines are long and
there's a lot of nesting, and with 8sp indentation less stuff fits. 4 sp
also seems to be the default indentation, so this will make it less likely
that people will mess up if they don't load the editor config. (I think people
often use vi, and vi has no support to load project-wide configuration
automatically. We distribute a .vimrc file, but it is not loaded by default,
and even the instructions in it seem to discourage its use for security
reasons.)

Also remove the few vim config lines that were left. We should either have them
on all files, or none.

Also remove some strange stuff like '#!/bin/env bash', yikes.
2019-04-12 08:30:31 +02:00
Lennart Poettering ca7410fe43 coccinelle: add coccinelle script for empty_or_dash() use 2019-04-08 14:31:15 +02:00
Zbigniew Jędrzejewski-Szmek 19130626a0 nspawn-oci: use SYNTHETIC_ERRNO 2019-03-21 10:51:43 +01:00
Lennart Poettering cb3108669d tree-wide: more IOVEC_MAKE() conversions 2018-11-28 13:08:19 +09:00
Lennart Poettering 5cfa2c3dc0 tree-wide: use IOVEC_MAKE() at many places 2018-11-27 10:12:27 +01:00
Lennart Poettering 020b39497a tree-wide: use SWAP_TWO a bit more 2018-11-26 22:17:34 +01:00