File: | build-scan/../src/timesync/timesyncd-server.c |
Warning: | line 129, column 17 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | ||||
2 | |||||
3 | #include "alloc-util.h" | ||||
4 | #include "timesyncd-server.h" | ||||
5 | |||||
6 | int server_address_new( | ||||
7 | ServerName *n, | ||||
8 | ServerAddress **ret, | ||||
9 | const union sockaddr_union *sockaddr, | ||||
10 | socklen_t socklen) { | ||||
11 | |||||
12 | ServerAddress *a, *tail; | ||||
13 | |||||
14 | assert(n)do { if ((__builtin_expect(!!(!(n)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("n"), "../src/timesync/timesyncd-server.c" , 14, __PRETTY_FUNCTION__); } while (0); | ||||
15 | assert(sockaddr)do { if ((__builtin_expect(!!(!(sockaddr)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("sockaddr"), "../src/timesync/timesyncd-server.c" , 15, __PRETTY_FUNCTION__); } while (0); | ||||
16 | assert(socklen >= offsetof(struct sockaddr, sa_data))do { if ((__builtin_expect(!!(!(socklen >= __builtin_offsetof (struct sockaddr, sa_data))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("socklen >= offsetof(struct sockaddr, sa_data)"), "../src/timesync/timesyncd-server.c" , 16, __PRETTY_FUNCTION__); } while (0); | ||||
17 | assert(socklen <= sizeof(union sockaddr_union))do { if ((__builtin_expect(!!(!(socklen <= sizeof(union sockaddr_union ))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("socklen <= sizeof(union sockaddr_union)" ), "../src/timesync/timesyncd-server.c", 17, __PRETTY_FUNCTION__ ); } while (0); | ||||
18 | |||||
19 | a = new0(ServerAddress, 1)((ServerAddress*) calloc((1), sizeof(ServerAddress))); | ||||
20 | if (!a) | ||||
21 | return -ENOMEM12; | ||||
22 | |||||
23 | memcpy(&a->sockaddr, sockaddr, socklen); | ||||
24 | a->socklen = socklen; | ||||
25 | |||||
26 | LIST_FIND_TAIL(addresses, n->addresses, tail)do { typeof(*(n->addresses)) *_item = (n->addresses); if (!_item) (tail) = ((void*)0); else { while (_item->addresses_next ) _item = _item->addresses_next; (tail) = _item; } } while (0); | ||||
27 | LIST_INSERT_AFTER(addresses, n->addresses, tail, a)do { typeof(*(n->addresses)) **_head = &(n->addresses ), *_a = (tail), *_b = (a); do { if ((__builtin_expect(!!(!(_b )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_b"), "../src/timesync/timesyncd-server.c" , 27, __PRETTY_FUNCTION__); } while (0); if (!_a) { if ((_b-> addresses_next = *_head)) _b->addresses_next->addresses_prev = _b; _b->addresses_prev = ((void*)0); *_head = _b; } else { if ((_b->addresses_next = _a->addresses_next)) _b-> addresses_next->addresses_prev = _b; _b->addresses_prev = _a; _a->addresses_next = _b; } } while (0); | ||||
28 | a->name = n; | ||||
29 | |||||
30 | if (ret) | ||||
31 | *ret = a; | ||||
32 | |||||
33 | return 0; | ||||
34 | } | ||||
35 | |||||
36 | ServerAddress* server_address_free(ServerAddress *a) { | ||||
37 | if (!a
| ||||
38 | return NULL((void*)0); | ||||
39 | |||||
40 | if (a->name) { | ||||
41 | LIST_REMOVE(addresses, a->name->addresses, a)do { typeof(*(a->name->addresses)) **_head = &(a-> name->addresses), *_item = (a); do { if ((__builtin_expect (!!(!(_item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_item"), "../src/timesync/timesyncd-server.c", 41, __PRETTY_FUNCTION__ ); } while (0); if (_item->addresses_next) _item->addresses_next ->addresses_prev = _item->addresses_prev; if (_item-> addresses_prev) _item->addresses_prev->addresses_next = _item->addresses_next; else { do { if ((__builtin_expect( !!(!(*_head == _item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("*_head == _item"), "../src/timesync/timesyncd-server.c", 41 , __PRETTY_FUNCTION__); } while (0); *_head = _item->addresses_next ; } _item->addresses_next = _item->addresses_prev = ((void *)0); } while (0); | ||||
42 | |||||
43 | if (a->name->manager && a->name->manager->current_server_address == a) | ||||
44 | manager_set_server_address(a->name->manager, NULL((void*)0)); | ||||
45 | } | ||||
46 | |||||
47 | return mfree(a); | ||||
48 | } | ||||
49 | |||||
50 | int server_name_new( | ||||
51 | Manager *m, | ||||
52 | ServerName **ret, | ||||
53 | ServerType type, | ||||
54 | const char *string) { | ||||
55 | |||||
56 | ServerName *n, *tail; | ||||
57 | |||||
58 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/timesync/timesyncd-server.c" , 58, __PRETTY_FUNCTION__); } while (0); | ||||
59 | assert(string)do { if ((__builtin_expect(!!(!(string)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("string"), "../src/timesync/timesyncd-server.c" , 59, __PRETTY_FUNCTION__); } while (0); | ||||
60 | |||||
61 | n = new0(ServerName, 1)((ServerName*) calloc((1), sizeof(ServerName))); | ||||
62 | if (!n) | ||||
63 | return -ENOMEM12; | ||||
64 | |||||
65 | n->type = type; | ||||
66 | n->string = strdup(string); | ||||
67 | if (!n->string) { | ||||
68 | free(n); | ||||
69 | return -ENOMEM12; | ||||
70 | } | ||||
71 | |||||
72 | if (type == SERVER_SYSTEM) { | ||||
73 | LIST_FIND_TAIL(names, m->system_servers, tail)do { typeof(*(m->system_servers)) *_item = (m->system_servers ); if (!_item) (tail) = ((void*)0); else { while (_item->names_next ) _item = _item->names_next; (tail) = _item; } } while (0); | ||||
74 | LIST_INSERT_AFTER(names, m->system_servers, tail, n)do { typeof(*(m->system_servers)) **_head = &(m->system_servers ), *_a = (tail), *_b = (n); do { if ((__builtin_expect(!!(!(_b )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_b"), "../src/timesync/timesyncd-server.c" , 74, __PRETTY_FUNCTION__); } while (0); if (!_a) { if ((_b-> names_next = *_head)) _b->names_next->names_prev = _b; _b ->names_prev = ((void*)0); *_head = _b; } else { if ((_b-> names_next = _a->names_next)) _b->names_next->names_prev = _b; _b->names_prev = _a; _a->names_next = _b; } } while (0); | ||||
75 | } else if (type == SERVER_LINK) { | ||||
76 | LIST_FIND_TAIL(names, m->link_servers, tail)do { typeof(*(m->link_servers)) *_item = (m->link_servers ); if (!_item) (tail) = ((void*)0); else { while (_item->names_next ) _item = _item->names_next; (tail) = _item; } } while (0); | ||||
77 | LIST_INSERT_AFTER(names, m->link_servers, tail, n)do { typeof(*(m->link_servers)) **_head = &(m->link_servers ), *_a = (tail), *_b = (n); do { if ((__builtin_expect(!!(!(_b )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_b"), "../src/timesync/timesyncd-server.c" , 77, __PRETTY_FUNCTION__); } while (0); if (!_a) { if ((_b-> names_next = *_head)) _b->names_next->names_prev = _b; _b ->names_prev = ((void*)0); *_head = _b; } else { if ((_b-> names_next = _a->names_next)) _b->names_next->names_prev = _b; _b->names_prev = _a; _a->names_next = _b; } } while (0); | ||||
78 | } else if (type == SERVER_FALLBACK) { | ||||
79 | LIST_FIND_TAIL(names, m->fallback_servers, tail)do { typeof(*(m->fallback_servers)) *_item = (m->fallback_servers ); if (!_item) (tail) = ((void*)0); else { while (_item->names_next ) _item = _item->names_next; (tail) = _item; } } while (0); | ||||
80 | LIST_INSERT_AFTER(names, m->fallback_servers, tail, n)do { typeof(*(m->fallback_servers)) **_head = &(m-> fallback_servers), *_a = (tail), *_b = (n); do { if ((__builtin_expect (!!(!(_b)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_b" ), "../src/timesync/timesyncd-server.c", 80, __PRETTY_FUNCTION__ ); } while (0); if (!_a) { if ((_b->names_next = *_head)) _b ->names_next->names_prev = _b; _b->names_prev = ((void *)0); *_head = _b; } else { if ((_b->names_next = _a->names_next )) _b->names_next->names_prev = _b; _b->names_prev = _a; _a->names_next = _b; } } while (0); | ||||
81 | } else | ||||
82 | assert_not_reached("Unknown server type")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, ( "Unknown server type"), "../src/timesync/timesyncd-server.c", 82, __PRETTY_FUNCTION__); } while (0); | ||||
83 | |||||
84 | n->manager = m; | ||||
85 | |||||
86 | if (type != SERVER_FALLBACK && | ||||
87 | m->current_server_name && | ||||
88 | m->current_server_name->type == SERVER_FALLBACK) | ||||
89 | manager_set_server_name(m, NULL((void*)0)); | ||||
90 | |||||
91 | log_debug("Added new server %s.", string)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/timesync/timesyncd-server.c", 91, __func__, "Added new server %s." , string) : -abs(_e); }); | ||||
92 | |||||
93 | if (ret) | ||||
94 | *ret = n; | ||||
95 | |||||
96 | return 0; | ||||
97 | } | ||||
98 | |||||
99 | ServerName *server_name_free(ServerName *n) { | ||||
100 | if (!n) | ||||
| |||||
101 | return NULL((void*)0); | ||||
102 | |||||
103 | server_name_flush_addresses(n); | ||||
104 | |||||
105 | if (n->manager) { | ||||
106 | if (n->type == SERVER_SYSTEM) | ||||
107 | LIST_REMOVE(names, n->manager->system_servers, n)do { typeof(*(n->manager->system_servers)) **_head = & (n->manager->system_servers), *_item = (n); do { if ((__builtin_expect (!!(!(_item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_item"), "../src/timesync/timesyncd-server.c", 107, __PRETTY_FUNCTION__ ); } while (0); if (_item->names_next) _item->names_next ->names_prev = _item->names_prev; if (_item->names_prev ) _item->names_prev->names_next = _item->names_next; else { do { if ((__builtin_expect(!!(!(*_head == _item)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("*_head == _item" ), "../src/timesync/timesyncd-server.c", 107, __PRETTY_FUNCTION__ ); } while (0); *_head = _item->names_next; } _item->names_next = _item->names_prev = ((void*)0); } while (0); | ||||
108 | else if (n->type == SERVER_LINK) | ||||
109 | LIST_REMOVE(names, n->manager->link_servers, n)do { typeof(*(n->manager->link_servers)) **_head = & (n->manager->link_servers), *_item = (n); do { if ((__builtin_expect (!!(!(_item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_item"), "../src/timesync/timesyncd-server.c", 109, __PRETTY_FUNCTION__ ); } while (0); if (_item->names_next) _item->names_next ->names_prev = _item->names_prev; if (_item->names_prev ) _item->names_prev->names_next = _item->names_next; else { do { if ((__builtin_expect(!!(!(*_head == _item)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("*_head == _item" ), "../src/timesync/timesyncd-server.c", 109, __PRETTY_FUNCTION__ ); } while (0); *_head = _item->names_next; } _item->names_next = _item->names_prev = ((void*)0); } while (0); | ||||
110 | else if (n->type == SERVER_FALLBACK) | ||||
111 | LIST_REMOVE(names, n->manager->fallback_servers, n)do { typeof(*(n->manager->fallback_servers)) **_head = & (n->manager->fallback_servers), *_item = (n); do { if ( (__builtin_expect(!!(!(_item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("_item"), "../src/timesync/timesyncd-server.c", 111, __PRETTY_FUNCTION__ ); } while (0); if (_item->names_next) _item->names_next ->names_prev = _item->names_prev; if (_item->names_prev ) _item->names_prev->names_next = _item->names_next; else { do { if ((__builtin_expect(!!(!(*_head == _item)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("*_head == _item" ), "../src/timesync/timesyncd-server.c", 111, __PRETTY_FUNCTION__ ); } while (0); *_head = _item->names_next; } _item->names_next = _item->names_prev = ((void*)0); } while (0); | ||||
112 | else | ||||
113 | assert_not_reached("Unknown server type")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, ( "Unknown server type"), "../src/timesync/timesyncd-server.c", 113, __PRETTY_FUNCTION__); } while (0); | ||||
114 | |||||
115 | if (n->manager->current_server_name == n) | ||||
116 | manager_set_server_name(n->manager, NULL((void*)0)); | ||||
117 | } | ||||
118 | |||||
119 | log_debug("Removed server %s.", n->string)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/timesync/timesyncd-server.c", 119, __func__, "Removed server %s." , n->string) : -abs(_e); }); | ||||
120 | |||||
121 | free(n->string); | ||||
122 | return mfree(n); | ||||
123 | } | ||||
124 | |||||
125 | void server_name_flush_addresses(ServerName *n) { | ||||
126 | assert(n)do { if ((__builtin_expect(!!(!(n)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("n"), "../src/timesync/timesyncd-server.c" , 126, __PRETTY_FUNCTION__); } while (0); | ||||
127 | |||||
128 | while (n->addresses) | ||||
129 | server_address_free(n->addresses); | ||||
| |||||
130 | } |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | #pragma once |
3 | |
4 | #include <alloca.h> |
5 | #include <stddef.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | |
9 | #include "macro.h" |
10 | |
11 | #define new(t, n)((t*) malloc_multiply(sizeof(t), (n))) ((t*) malloc_multiply(sizeof(t), (n))) |
12 | |
13 | #define new0(t, n)((t*) calloc((n), sizeof(t))) ((t*) calloc((n), sizeof(t))) |
14 | |
15 | #define newa(t, n)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)" ), "../src/basic/alloc-util.h", 15, __PRETTY_FUNCTION__); } while (0); (t*) __builtin_alloca (sizeof(t)*(n)); }) \ |
16 | ({ \ |
17 | assert(!size_multiply_overflow(sizeof(t), n))do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)" ), "../src/basic/alloc-util.h", 17, __PRETTY_FUNCTION__); } while (0); \ |
18 | (t*) alloca(sizeof(t)*(n))__builtin_alloca (sizeof(t)*(n)); \ |
19 | }) |
20 | |
21 | #define newa0(t, n)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)" ), "../src/basic/alloc-util.h", 21, __PRETTY_FUNCTION__); } while (0); (t*) ({ char *_new_; size_t _len_ = sizeof(t)*(n); _new_ = __builtin_alloca (_len_); (void *) memset(_new_, 0, _len_) ; }); }) \ |
22 | ({ \ |
23 | assert(!size_multiply_overflow(sizeof(t), n))do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)" ), "../src/basic/alloc-util.h", 23, __PRETTY_FUNCTION__); } while (0); \ |
24 | (t*) alloca0(sizeof(t)*(n))({ char *_new_; size_t _len_ = sizeof(t)*(n); _new_ = __builtin_alloca (_len_); (void *) memset(_new_, 0, _len_); }); \ |
25 | }) |
26 | |
27 | #define newdup(t, p, n)((t*) memdup_multiply(p, sizeof(t), (n))) ((t*) memdup_multiply(p, sizeof(t), (n))) |
28 | |
29 | #define newdup_suffix0(t, p, n)((t*) memdup_suffix0_multiply(p, sizeof(t), (n))) ((t*) memdup_suffix0_multiply(p, sizeof(t), (n))) |
30 | |
31 | #define malloc0(n)(calloc(1, (n))) (calloc(1, (n))) |
32 | |
33 | static inline void *mfree(void *memory) { |
34 | free(memory); |
35 | return NULL((void*)0); |
36 | } |
37 | |
38 | #define free_and_replace(a, b)({ free(a); (a) = (b); (b) = ((void*)0); 0; }) \ |
39 | ({ \ |
40 | free(a); \ |
41 | (a) = (b); \ |
42 | (b) = NULL((void*)0); \ |
43 | 0; \ |
44 | }) |
45 | |
46 | void* memdup(const void *p, size_t l) _alloc_(2); |
47 | void* memdup_suffix0(const void *p, size_t l) _alloc_(2); |
48 | |
49 | static inline void freep(void *p) { |
50 | free(*(void**) p); |
51 | } |
52 | |
53 | #define _cleanup_free___attribute__((cleanup(freep))) _cleanup_(freep)__attribute__((cleanup(freep))) |
54 | |
55 | static inline bool_Bool size_multiply_overflow(size_t size, size_t need) { |
56 | return _unlikely_(need != 0 && size > (SIZE_MAX / need))(__builtin_expect(!!(need != 0 && size > ((18446744073709551615UL ) / need)),0)); |
57 | } |
58 | |
59 | _malloc___attribute__ ((malloc)) _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) { |
60 | if (size_multiply_overflow(size, need)) |
61 | return NULL((void*)0); |
62 | |
63 | return malloc(size * need); |
64 | } |
65 | |
66 | #if !HAVE_REALLOCARRAY1 |
67 | _alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size) { |
68 | if (size_multiply_overflow(size, need)) |
69 | return NULL((void*)0); |
70 | |
71 | return realloc(p, size * need); |
72 | } |
73 | #endif |
74 | |
75 | _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) { |
76 | if (size_multiply_overflow(size, need)) |
77 | return NULL((void*)0); |
78 | |
79 | return memdup(p, size * need); |
80 | } |
81 | |
82 | _alloc_(2, 3) static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) { |
83 | if (size_multiply_overflow(size, need)) |
84 | return NULL((void*)0); |
85 | |
86 | return memdup_suffix0(p, size * need); |
87 | } |
88 | |
89 | void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size); |
90 | void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size); |
91 | |
92 | #define GREEDY_REALLOC(array, allocated, need)greedy_realloc((void**) &(array), &(allocated), (need ), sizeof((array)[0])) \ |
93 | greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0])) |
94 | |
95 | #define GREEDY_REALLOC0(array, allocated, need)greedy_realloc0((void**) &(array), &(allocated), (need ), sizeof((array)[0])) \ |
96 | greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0])) |
97 | |
98 | #define alloca0(n)({ char *_new_; size_t _len_ = n; _new_ = __builtin_alloca (_len_ ); (void *) memset(_new_, 0, _len_); }) \ |
99 | ({ \ |
100 | char *_new_; \ |
101 | size_t _len_ = n; \ |
102 | _new_ = alloca(_len_)__builtin_alloca (_len_); \ |
103 | (void *) memset(_new_, 0, _len_); \ |
104 | }) |
105 | |
106 | /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */ |
107 | #define alloca_align(size, align)({ void *_ptr_; size_t _mask_ = (align) - 1; _ptr_ = __builtin_alloca ((size) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); }) \ |
108 | ({ \ |
109 | void *_ptr_; \ |
110 | size_t _mask_ = (align) - 1; \ |
111 | _ptr_ = alloca((size) + _mask_)__builtin_alloca ((size) + _mask_); \ |
112 | (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \ |
113 | }) |
114 | |
115 | #define alloca0_align(size, align)({ void *_new_; size_t _size_ = (size); _new_ = ({ void *_ptr_ ; size_t _mask_ = ((align)) - 1; _ptr_ = __builtin_alloca ((_size_ ) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_ ); }); (void*)memset(_new_, 0, _size_); }) \ |
116 | ({ \ |
117 | void *_new_; \ |
118 | size_t _size_ = (size); \ |
119 | _new_ = alloca_align(_size_, (align))({ void *_ptr_; size_t _mask_ = ((align)) - 1; _ptr_ = __builtin_alloca ((_size_) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); }); \ |
120 | (void*)memset(_new_, 0, _size_); \ |
121 | }) |
122 | |
123 | /* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to |
124 | * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */ |
125 | #define TAKE_PTR(ptr)({ typeof(ptr) _ptr_ = (ptr); (ptr) = ((void*)0); _ptr_; }) \ |
126 | ({ \ |
127 | typeof(ptr) _ptr_ = (ptr); \ |
128 | (ptr) = NULL((void*)0); \ |
129 | _ptr_; \ |
130 | }) |