diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c index e5b5079c168..e2a29475a22 100644 --- a/src/nss-resolve/nss-resolve.c +++ b/src/nss-resolve/nss-resolve.c @@ -207,14 +207,10 @@ enum nss_status _nss_resolve_gethostbyname4_r( int *errnop, int *h_errnop, int32_t *ttlp) { - _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; - struct gaih_addrtuple *r_tuple = NULL, *r_tuple_first = NULL; _cleanup_(varlink_unrefp) Varlink *link = NULL; - const char *canonical = NULL, *error_id = NULL; - JsonVariant *entry, *rparams; - size_t l, ms, idx, c = 0; - char *r_name; + _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; + JsonVariant *rparams, *entry; int r; PROTECT_ERRNO; @@ -241,6 +237,7 @@ enum nss_status _nss_resolve_gethostbyname4_r( * DNSSEC errors and suchlike. (We don't use UNAVAIL in this case so that the nsswitch.conf * configuration can distinguish such executed but negative replies from complete failure to * talk to resolved). */ + const char *error_id; r = varlink_call(link, "io.systemd.Resolve.ResolveHostname", cparams, &rparams, &error_id, NULL); if (r < 0) goto fail; @@ -256,6 +253,7 @@ enum nss_status _nss_resolve_gethostbyname4_r( if (json_variant_is_blank_object(p.addresses)) goto not_found; + size_t n_addresses = 0; JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; @@ -271,13 +269,13 @@ enum nss_status _nss_resolve_gethostbyname4_r( goto fail; } - c++; + n_addresses++; } - canonical = p.name ?: name; + const char *canonical = p.name ?: name; + size_t l = strlen(canonical); + size_t idx, ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * n_addresses; - l = strlen(canonical); - ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c; if (buflen < ms) { UNPROTECT_ERRNO; *errnop = ERANGE; @@ -286,12 +284,13 @@ enum nss_status _nss_resolve_gethostbyname4_r( } /* First, append name */ - r_name = buffer; - memcpy(r_name, canonical, l+1); - idx = ALIGN(l+1); + char *r_name = buffer; + memcpy(r_name, canonical, l + 1); + idx = ALIGN(l + 1); /* Second, append addresses */ - r_tuple_first = (struct gaih_addrtuple*) (buffer + idx); + struct gaih_addrtuple *r_tuple = NULL, + *r_tuple_first = (struct gaih_addrtuple*) (buffer + idx); JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; @@ -313,7 +312,7 @@ enum nss_status _nss_resolve_gethostbyname4_r( idx += ALIGN(sizeof(struct gaih_addrtuple)); } - assert(r_tuple); + assert(r_tuple); /* We had at least one address, so r_tuple must be set */ r_tuple->next = NULL; /* Override last next pointer */ assert(idx == ms); @@ -353,13 +352,10 @@ enum nss_status _nss_resolve_gethostbyname3_r( int32_t *ttlp, char **canonp) { - _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; - char *r_name, *r_aliases, *r_addr, *r_addr_list; _cleanup_(varlink_unrefp) Varlink *link = NULL; - const char *canonical, *error_id = NULL; - size_t l, idx, ms, alen, i = 0, c = 0; - JsonVariant *entry, *rparams; + _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; + JsonVariant *rparams, *entry; int r; PROTECT_ERRNO; @@ -389,6 +385,7 @@ enum nss_status _nss_resolve_gethostbyname3_r( if (r < 0) goto fail; + const char *error_id; r = varlink_call(link, "io.systemd.Resolve.ResolveHostname", cparams, &rparams, &error_id, NULL); if (r < 0) goto fail; @@ -404,6 +401,7 @@ enum nss_status _nss_resolve_gethostbyname3_r( if (json_variant_is_blank_object(p.addresses)) goto not_found; + size_t n_addresses = 0; JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; @@ -419,15 +417,15 @@ enum nss_status _nss_resolve_gethostbyname3_r( goto fail; } - c++; + n_addresses++; } - canonical = p.name ?: name; + const char *canonical = p.name ?: name; - alen = FAMILY_ADDRESS_SIZE(af); - l = strlen(canonical); + size_t alen = FAMILY_ADDRESS_SIZE(af); + size_t l = strlen(canonical); - ms = ALIGN(l+1) + c*ALIGN(alen) + (c+2) * sizeof(char*); + size_t idx, ms = ALIGN(l + 1) + n_addresses * ALIGN(alen) + (n_addresses + 2) * sizeof(char*); if (buflen < ms) { UNPROTECT_ERRNO; @@ -437,18 +435,19 @@ enum nss_status _nss_resolve_gethostbyname3_r( } /* First, append name */ - r_name = buffer; + char *r_name = buffer; memcpy(r_name, canonical, l+1); idx = ALIGN(l+1); /* Second, create empty aliases array */ - r_aliases = buffer + idx; + char *r_aliases = buffer + idx; ((char**) r_aliases)[0] = NULL; idx += sizeof(char*); /* Third, append addresses */ - r_addr = buffer + idx; + char *r_addr = buffer + idx; + size_t i = 0; JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; @@ -468,16 +467,16 @@ enum nss_status _nss_resolve_gethostbyname3_r( i++; } - assert(i == c); - idx += c * ALIGN(alen); + assert(i == n_addresses); + idx += n_addresses * ALIGN(alen); /* Fourth, append address pointer array */ - r_addr_list = buffer + idx; - for (i = 0; i < c; i++) + char *r_addr_list = buffer + idx; + for (i = 0; i < n_addresses; i++) ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen); ((char**) r_addr_list)[i] = NULL; - idx += (c+1) * sizeof(char*); + idx += (n_addresses + 1) * sizeof(char*); assert(idx == ms); @@ -553,9 +552,9 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( int *errnop, int *h_errnop, int32_t *ttlp) { - _cleanup_(resolve_address_reply_destroy) ResolveAddressReply p = {}; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; _cleanup_(varlink_unrefp) Varlink *link = NULL; + _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(resolve_address_reply_destroy) ResolveAddressReply p = {}; JsonVariant *rparams, *entry; int r;