nss: Make aligning look nicer

Every aligning requires at least one cast and it's hard to read.  Let's
make a function that makes sure the pointer is moved according to the
alignment and use that to move throughout the data buffer.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Martin Kletzander 2016-03-18 16:46:41 +01:00
parent a8dc3ac28a
commit dc56475f72

View File

@ -252,17 +252,31 @@ _nss_libvirt_gethostbyname2_r(const char *name, int af, struct hostent *result,
errnop, herrnop, NULL, NULL); errnop, herrnop, NULL, NULL);
} }
static inline void *
move_and_align(void *buf, size_t len, size_t *idx)
{
char *buffer = buf;
size_t move = ALIGN(len);
if (!idx)
return buffer + move;
*idx += move;
return buffer + *idx;
}
enum nss_status enum nss_status
_nss_libvirt_gethostbyname3_r(const char *name, int af, struct hostent *result, _nss_libvirt_gethostbyname3_r(const char *name, int af, struct hostent *result,
char *buffer, size_t buflen, int *errnop, char *buffer, size_t buflen, int *errnop,
int *herrnop, int32_t *ttlp, char **canonp) int *herrnop, int32_t *ttlp, char **canonp)
{ {
enum nss_status ret = NSS_STATUS_UNAVAIL; enum nss_status ret = NSS_STATUS_UNAVAIL;
char *r_name, **r_aliases, *r_addr, **r_addr_list; char *r_name, **r_aliases, *r_addr, *r_addr_next, **r_addr_list;
leaseAddress *addr = NULL; leaseAddress *addr = NULL;
size_t naddr, i; size_t naddr, i;
bool found = false; bool found = false;
size_t nameLen, need, idx; size_t nameLen, need, idx = 0;
int alen; int alen;
int r; int r;
@ -319,23 +333,27 @@ _nss_libvirt_gethostbyname3_r(const char *name, int af, struct hostent *result,
/* First, append name */ /* First, append name */
r_name = buffer; r_name = buffer;
memcpy(r_name, name, nameLen + 1); memcpy(r_name, name, nameLen + 1);
idx = ALIGN(nameLen + 1);
r_aliases = move_and_align(buffer, nameLen + 1, &idx);
/* Second, create aliases array */ /* Second, create aliases array */
r_aliases = (char **) buffer + idx;
r_aliases[0] = NULL; r_aliases[0] = NULL;
idx += sizeof(char*);
/* Third, append address */ /* Third, append address */
r_addr = buffer + idx; r_addr = move_and_align(buffer, sizeof(char *), &idx);
for (i = 0; i < naddr; i++) r_addr_next = r_addr;
memcpy(r_addr + i * ALIGN(alen), addr[i].addr, alen); for (i = 0; i < naddr; i++) {
idx += naddr * ALIGN(alen); memcpy(r_addr_next, addr[i].addr, alen);
r_addr_next = move_and_align(buffer, alen, &idx);
}
r_addr_list = move_and_align(buffer, 0, &idx);
r_addr_next = r_addr;
/* Third, append address pointer array */ /* Third, append address pointer array */
r_addr_list = (char **) buffer + idx; for (i = 0; i < naddr; i++) {
for (i = 0; i < naddr; i++) r_addr_list[i] = r_addr_next;
r_addr_list[i] = r_addr + i * ALIGN(alen); r_addr_next = move_and_align(r_addr_next, alen, NULL);
}
r_addr_list[i] = NULL; r_addr_list[i] = NULL;
idx += (naddr + 1) * sizeof(char*); idx += (naddr + 1) * sizeof(char*);
@ -375,7 +393,7 @@ _nss_libvirt_gethostbyname4_r(const char *name, struct gaih_addrtuple **pat,
size_t naddr, i; size_t naddr, i;
bool found = false; bool found = false;
int r; int r;
size_t nameLen, need, idx; size_t nameLen, need, idx = 0;
struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL; struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
char *r_name; char *r_name;
@ -420,25 +438,22 @@ _nss_libvirt_gethostbyname4_r(const char *name, struct gaih_addrtuple **pat,
/* First, append name */ /* First, append name */
r_name = buffer; r_name = buffer;
memcpy(r_name, name, nameLen + 1); memcpy(r_name, name, nameLen + 1);
idx = ALIGN(nameLen + 1);
/* Second, append addresses */ /* Second, append addresses */
r_tuple_first = (struct gaih_addrtuple*) (buffer + idx); r_tuple_first = move_and_align(buffer, nameLen + 1, &idx);
for (i = 0; i < naddr; i++) { for (i = 0; i < naddr; i++) {
int family = addr[i].af; int family = addr[i].af;
r_tuple = (struct gaih_addrtuple*) (buffer + idx); r_tuple = move_and_align(buffer, 0, &idx);
if (i == naddr - 1) if (i == naddr - 1)
r_tuple->next = NULL; r_tuple->next = NULL;
else else
r_tuple->next = (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple))); r_tuple->next = move_and_align(buffer, sizeof(struct gaih_addrtuple), &idx);
r_tuple->name = r_name; r_tuple->name = r_name;
r_tuple->family = family; r_tuple->family = family;
r_tuple->scopeid = 0; r_tuple->scopeid = 0;
memcpy(r_tuple->addr, addr[i].addr, FAMILY_ADDRESS_SIZE(family)); memcpy(r_tuple->addr, addr[i].addr, FAMILY_ADDRESS_SIZE(family));
idx += ALIGN(sizeof(struct gaih_addrtuple));
} }
/* At this point, idx == need */ /* At this point, idx == need */