1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-03 01:17:45 +03:00

Merge pull request #21479 from keszybz/cosmetic-initialization-adjustments

Cosmetic initialization adjustments
This commit is contained in:
Lennart Poettering 2021-11-23 18:42:45 +01:00 committed by GitHub
commit 383c6f41e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 82 deletions

View File

@ -29,8 +29,11 @@ static void rewind_dns_packet(DnsPacketRewinder *rewinder) {
dns_packet_rewind(rewinder->packet, rewinder->saved_rindex);
}
#define INIT_REWINDER(rewinder, p) do { rewinder.packet = p; rewinder.saved_rindex = p->rindex; } while (0)
#define CANCEL_REWINDER(rewinder) do { rewinder.packet = NULL; } while (0)
#define REWINDER_INIT(p) { \
.packet = (p), \
.saved_rindex = (p)->rindex, \
}
#define CANCEL_REWINDER(rewinder) do { (rewinder).packet = NULL; } while (0)
int dns_packet_new(
DnsPacket **ret,
@ -515,15 +518,12 @@ int dns_packet_append_label(DnsPacket *p, const char *d, size_t l, bool canonica
*(w++) = (uint8_t) l;
if (p->canonical_form && canonical_candidate) {
size_t i;
if (p->canonical_form && canonical_candidate)
/* Generate in canonical form, as defined by DNSSEC
* RFC 4034, Section 6.2, i.e. all lower-case. */
for (i = 0; i < l; i++)
for (size_t i = 0; i < l; i++)
w[i] = (uint8_t) ascii_tolower(d[i]);
} else
else
/* Otherwise, just copy the string unaltered. This is
* essential for DNS-SD, where the casing of labels
* matters and needs to be retained. */
@ -1373,15 +1373,14 @@ int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start) {
}
int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start) {
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
assert(p);
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
const void *d;
char *t;
uint8_t c;
int r;
assert(p);
INIT_REWINDER(rewinder, p);
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
return r;
@ -1412,13 +1411,12 @@ int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start) {
}
int dns_packet_read_raw_string(DnsPacket *p, const void **ret, size_t *size, size_t *start) {
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
assert(p);
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
uint8_t c;
int r;
assert(p);
INIT_REWINDER(rewinder, p);
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
return r;
@ -1442,18 +1440,15 @@ int dns_packet_read_name(
bool allow_compression,
size_t *ret_start) {
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
size_t after_rindex = 0, jump_barrier;
assert(p);
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
size_t after_rindex = 0, jump_barrier = p->rindex;
_cleanup_free_ char *name = NULL;
bool first = true;
size_t n = 0;
int r;
assert(p);
INIT_REWINDER(rewinder, p);
jump_barrier = p->rindex;
if (p->refuse_compression)
allow_compression = false;
@ -1530,18 +1525,15 @@ int dns_packet_read_name(
}
static int dns_packet_read_type_window(DnsPacket *p, Bitmap **types, size_t *start) {
uint8_t window;
uint8_t length;
const uint8_t *bitmap;
uint8_t bit = 0;
unsigned i;
bool found = false;
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
int r;
assert(p);
assert(types);
INIT_REWINDER(rewinder, p);
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
uint8_t window, length;
const uint8_t *bitmap;
uint8_t bit = 0;
bool found = false;
int r;
r = bitmap_ensure_allocated(types);
if (r < 0)
@ -1562,7 +1554,7 @@ static int dns_packet_read_type_window(DnsPacket *p, Bitmap **types, size_t *sta
if (r < 0)
return r;
for (i = 0; i < length; i++) {
for (uint8_t i = 0; i < length; i++) {
uint8_t bitmask = 1 << 7;
if (!bitmap[i]) {
@ -1600,11 +1592,9 @@ static int dns_packet_read_type_window(DnsPacket *p, Bitmap **types, size_t *sta
}
static int dns_packet_read_type_windows(DnsPacket *p, Bitmap **types, size_t size, size_t *start) {
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
int r;
INIT_REWINDER(rewinder, p);
while (p->rindex < rewinder.saved_rindex + size) {
r = dns_packet_read_type_window(p, types, NULL);
if (r < 0)
@ -1631,15 +1621,14 @@ int dns_packet_read_key(
bool *ret_cache_flush_or_qu,
size_t *ret_start) {
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
assert(p);
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
_cleanup_free_ char *name = NULL;
bool cache_flush_or_qu = false;
uint16_t class, type;
int r;
assert(p);
INIT_REWINDER(rewinder, p);
r = dns_packet_read_name(p, &name, true, NULL);
if (r < 0)
return r;
@ -1693,18 +1682,16 @@ int dns_packet_read_rr(
bool *ret_cache_flush,
size_t *ret_start) {
assert(p);
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
size_t offset;
uint16_t rdlength;
bool cache_flush;
int r;
assert(p);
INIT_REWINDER(rewinder, p);
r = dns_packet_read_key(p, &key, &cache_flush, NULL);
if (r < 0)
return r;
@ -2199,7 +2186,7 @@ static bool opt_is_good(DnsResourceRecord *rr, bool *rfc6975) {
static int dns_packet_extract_question(DnsPacket *p, DnsQuestion **ret_question) {
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
unsigned n, i;
unsigned n;
int r;
n = DNS_PACKET_QDCOUNT(p);
@ -2219,7 +2206,7 @@ static int dns_packet_extract_question(DnsPacket *p, DnsQuestion **ret_question)
if (r < 0)
return r;
for (i = 0; i < n; i++) {
for (unsigned i = 0; i < n; i++) {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
bool qu;
@ -2250,7 +2237,7 @@ static int dns_packet_extract_question(DnsPacket *p, DnsQuestion **ret_question)
static int dns_packet_extract_answer(DnsPacket *p, DnsAnswer **ret_answer) {
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
unsigned n, i;
unsigned n;
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *previous = NULL;
bool bad_opt = false;
int r;
@ -2263,7 +2250,7 @@ static int dns_packet_extract_answer(DnsPacket *p, DnsAnswer **ret_answer) {
if (!answer)
return -ENOMEM;
for (i = 0; i < n; i++) {
for (unsigned i = 0; i < n; i++) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
bool cache_flush = false;
size_t start;
@ -2390,15 +2377,16 @@ static int dns_packet_extract_answer(DnsPacket *p, DnsAnswer **ret_answer) {
}
int dns_packet_extract(DnsPacket *p) {
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = {};
int r;
assert(p);
if (p->extracted)
return 0;
INIT_REWINDER(rewinder, p);
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
_unused_ _cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
int r;
dns_packet_rewind(p, DNS_PACKET_HEADER_SIZE);
r = dns_packet_extract_question(p, &question);
@ -2417,7 +2405,6 @@ int dns_packet_extract(DnsPacket *p) {
p->question = TAKE_PTR(question);
p->answer = TAKE_PTR(answer);
p->extracted = true;
/* no CANCEL, always rewind */
@ -2466,14 +2453,12 @@ int dns_packet_patch_max_udp_size(DnsPacket *p, uint16_t max_udp_size) {
}
static int patch_rr(DnsPacket *p, usec_t age) {
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
size_t ttl_index;
uint32_t ttl;
uint16_t type, rdlength;
int r;
INIT_REWINDER(rewinder, p);
/* Patches the RR at the current rindex, subtracts the specified time from the TTL */
r = dns_packet_read_name(p, NULL, true, NULL);
@ -2510,33 +2495,31 @@ static int patch_rr(DnsPacket *p, usec_t age) {
}
int dns_packet_patch_ttls(DnsPacket *p, usec_t timestamp) {
_cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = {};
unsigned i, n;
usec_t k;
int r;
assert(p);
assert(timestamp_is_set(timestamp));
/* Adjusts all TTLs in the packet by subtracting the time difference between now and the specified timestamp */
_unused_ _cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder = REWINDER_INIT(p);
unsigned n;
usec_t k;
int r;
k = now(clock_boottime_or_monotonic());
assert(k >= timestamp);
k -= timestamp;
INIT_REWINDER(rewinder, p);
dns_packet_rewind(p, DNS_PACKET_HEADER_SIZE);
n = DNS_PACKET_QDCOUNT(p);
for (i = 0; i < n; i++) {
for (unsigned i = 0; i < n; i++) {
r = dns_packet_read_key(p, NULL, NULL, NULL);
if (r < 0)
return r;
}
n = DNS_PACKET_RRCOUNT(p);
for (i = 0; i < n; i++) {
for (unsigned i = 0; i < n; i++) {
/* DNS servers suck, hence the RR count is in many servers off. If we reached the end
* prematurely, accept that, exit early */

View File

@ -1640,7 +1640,7 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
}
case TABLE_UINT64_HEX: {
_cleanup_free_ char *p;
_cleanup_free_ char *p = NULL;
p = new(char, 16 + 1);
if (!p)

View File

@ -102,6 +102,12 @@ typedef struct Context {
uint64_t media_session_last_offset;
} Context;
#define CONTEXT_EMPTY { \
.fd = -1, \
.media_feature = _FEATURE_INVALID, \
.media_state = _MEDIA_STATE_INVALID, \
}
static void context_clear(Context *c) {
if (!c)
return;
@ -110,16 +116,6 @@ static void context_clear(Context *c) {
free(c->drive_features);
}
static void context_init(Context *c) {
assert(c);
*c = (Context) {
.fd = -1,
.media_feature = _FEATURE_INVALID,
.media_state = _MEDIA_STATE_INVALID,
};
}
static bool drive_has_feature(const Context *c, Feature f) {
assert(c);
@ -954,7 +950,7 @@ static int parse_argv(int argc, char *argv[]) {
}
static int run(int argc, char *argv[]) {
_cleanup_(context_clear) Context c;
_cleanup_(context_clear) Context c = CONTEXT_EMPTY;
int r;
log_set_target(LOG_TARGET_AUTO);
@ -962,8 +958,6 @@ static int run(int argc, char *argv[]) {
log_parse_environment();
log_open();
context_init(&c);
r = parse_argv(argc, argv);
if (r <= 0)
return r;