summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-02-16 20:25:37 +0100
committerGitHub <noreply@github.com>2021-02-16 20:25:37 +0100
commit0addd9ba9c2a5e32617d98bef6ec753323d3da4c (patch)
tree902cf3d5a1f05d4f347143c8789ffa130bd48159 /src/basic
parent7820a56ccb6b164699f25fc00cbe3c38fbc182a4 (diff)
parent15567b3a73285ac9d450fb2a967784e28afb6f5e (diff)
downloadsystemd-0addd9ba9c2a5e32617d98bef6ec753323d3da4c.tar.gz
Merge pull request #18616 from keszybz/argv-fuzzer-quick-fix
fuzz-systemctl-parse-argv: avoid "leak" of bus object
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/capability-util.c2
-rw-r--r--src/basic/capability-util.h2
-rw-r--r--src/basic/dlfcn-util.h2
-rw-r--r--src/basic/fd-util.h4
-rw-r--r--src/basic/fileio.c2
-rw-r--r--src/basic/gcrypt-util.h2
-rw-r--r--src/basic/macro.h10
-rw-r--r--src/basic/selinux-util.c2
-rw-r--r--src/basic/selinux-util.h2
-rw-r--r--src/basic/strbuf.c8
-rw-r--r--src/basic/strbuf.h6
11 files changed, 26 insertions, 16 deletions
diff --git a/src/basic/capability-util.c b/src/basic/capability-util.c
index c1520d9279..43eb23cb05 100644
--- a/src/basic/capability-util.c
+++ b/src/basic/capability-util.c
@@ -18,7 +18,7 @@
#include "util.h"
int have_effective_cap(int value) {
- _cleanup_cap_free_ cap_t cap;
+ _cleanup_cap_free_ cap_t cap = NULL;
cap_flag_value_t fv;
cap = cap_get_proc();
diff --git a/src/basic/capability-util.h b/src/basic/capability-util.h
index f5ce290524..d9489d7883 100644
--- a/src/basic/capability-util.h
+++ b/src/basic/capability-util.h
@@ -25,7 +25,7 @@ int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities);
int drop_capability(cap_value_t cv);
-DEFINE_TRIVIAL_CLEANUP_FUNC(cap_t, cap_free);
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(cap_t, cap_free, NULL);
#define _cleanup_cap_free_ _cleanup_(cap_freep)
static inline void cap_free_charpp(char **p) {
diff --git a/src/basic/dlfcn-util.h b/src/basic/dlfcn-util.h
index e0944f3aaf..aa713d328b 100644
--- a/src/basic/dlfcn-util.h
+++ b/src/basic/dlfcn-util.h
@@ -5,7 +5,7 @@
#include "macro.h"
-DEFINE_TRIVIAL_CLEANUP_FUNC(void*, dlclose);
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(void*, dlclose, NULL);
int dlsym_many_and_warn(void *dl, int level, ...);
diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h
index 2162537b80..f05c2b5a15 100644
--- a/src/basic/fd-util.h
+++ b/src/basic/fd-util.h
@@ -41,8 +41,8 @@ static inline void fclosep(FILE **f) {
safe_fclose(*f);
}
-DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
-DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL);
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
#define _cleanup_close_ _cleanup_(closep)
#define _cleanup_fclose_ _cleanup_(fclosep)
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index f4708bc05f..21532515fb 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -1146,7 +1146,7 @@ static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
return EOL_NONE;
}
-DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, funlockfile, NULL);
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
size_t n = 0, allocated = 0, count = 0;
diff --git a/src/basic/gcrypt-util.h b/src/basic/gcrypt-util.h
index c07b36cdb9..27dcc72028 100644
--- a/src/basic/gcrypt-util.h
+++ b/src/basic/gcrypt-util.h
@@ -14,7 +14,7 @@
void initialize_libgcrypt(bool secmem);
int string_hashsum(const char *s, size_t len, int md_algorithm, char **out);
-DEFINE_TRIVIAL_CLEANUP_FUNC(gcry_md_hd_t, gcry_md_close);
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(gcry_md_hd_t, gcry_md_close, NULL);
#endif
static inline int string_hashsum_sha224(const char *s, size_t len, char **out) {
diff --git a/src/basic/macro.h b/src/basic/macro.h
index d4e3d3b8fb..eebd0769dd 100644
--- a/src/basic/macro.h
+++ b/src/basic/macro.h
@@ -405,10 +405,20 @@ static inline int __coverity_check_and_return__(int condition) {
func(p); \
}
+/* When func() returns the void value (NULL, -1, …) of the appropriate type */
#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
static inline void func##p(type *p) { \
if (*p) \
+ *p = func(*p); \
+ }
+
+/* When func() doesn't return the appropriate type, set variable to empty afterwards */
+#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(type, func, empty) \
+ static inline void func##p(type *p) { \
+ if (*p != (empty)) { \
func(*p); \
+ *p = (empty); \
+ } \
}
#define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \
diff --git a/src/basic/selinux-util.c b/src/basic/selinux-util.c
index bd22500b7e..cfc8464c66 100644
--- a/src/basic/selinux-util.c
+++ b/src/basic/selinux-util.c
@@ -29,7 +29,7 @@
#include "time-util.h"
#if HAVE_SELINUX
-DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free);
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(context_t, context_free, NULL);
#define _cleanup_context_free_ _cleanup_(context_freep)
static int mac_selinux_reload(int seqno);
diff --git a/src/basic/selinux-util.h b/src/basic/selinux-util.h
index 1236d6efdf..1095bdef0e 100644
--- a/src/basic/selinux-util.h
+++ b/src/basic/selinux-util.h
@@ -11,7 +11,7 @@
#if HAVE_SELINUX
#include <selinux/selinux.h>
-DEFINE_TRIVIAL_CLEANUP_FUNC(char*, freecon);
+DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(char*, freecon, NULL);
#define _cleanup_freecon_ _cleanup_(freeconp)
#endif
diff --git a/src/basic/strbuf.c b/src/basic/strbuf.c
index aee6647e35..212b561fa6 100644
--- a/src/basic/strbuf.c
+++ b/src/basic/strbuf.c
@@ -26,7 +26,7 @@
* ...
*/
-struct strbuf *strbuf_new(void) {
+struct strbuf* strbuf_new(void) {
struct strbuf *str;
str = new(struct strbuf, 1);
@@ -65,13 +65,13 @@ void strbuf_complete(struct strbuf *str) {
}
/* clean up everything */
-void strbuf_cleanup(struct strbuf *str) {
+struct strbuf* strbuf_free(struct strbuf *str) {
if (!str)
- return;
+ return NULL;
strbuf_complete(str);
free(str->buf);
- free(str);
+ return mfree(str);
}
static int strbuf_children_cmp(const struct strbuf_child_entry *n1,
diff --git a/src/basic/strbuf.h b/src/basic/strbuf.h
index 82758d721b..6187c08683 100644
--- a/src/basic/strbuf.h
+++ b/src/basic/strbuf.h
@@ -32,8 +32,8 @@ struct strbuf_child_entry {
struct strbuf_node *child;
};
-struct strbuf *strbuf_new(void);
+struct strbuf* strbuf_new(void);
ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len);
void strbuf_complete(struct strbuf *str);
-void strbuf_cleanup(struct strbuf *str);
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct strbuf*, strbuf_cleanup);
+struct strbuf* strbuf_free(struct strbuf *str);
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct strbuf*, strbuf_free);