>From 9fe982f0c31e52c6bd14360a0c1765f1a8545111 Mon Sep 17 00:00:00 2001
From: Nick Guay <nguay@redhat.com>
Date: Mon, 20 Aug 2012 15:32:37 -0400
Subject: [PATCH] Fix alignment issues reported by clang

https://fedorahosted.org/sssd/ticket/1359
---
 src/monitor/monitor.c                   |  2 +-
 src/providers/ipa/ipa_dyndns.c          |  6 ++++--
 src/providers/krb5/krb5_child_handler.c |  4 ++--
 src/responder/common/responder_cmd.c    | 10 +++++-----
 src/responder/common/responder_packet.c | 33 ++++++++++++++++++++++-----------
 src/responder/nss/nsssrv_cmd.c          | 22 +++++++++++-----------
 src/responder/nss/nsssrv_mmap_cache.c   |  2 +-
 src/responder/nss/nsssrv_netgroup.c     | 10 +++++-----
 src/responder/nss/nsssrv_services.c     |  6 +++---
 src/responder/pam/pamsrv_cmd.c          |  4 +++-
 src/sss_client/common.c                 |  2 +-
 src/sss_client/nss_group.c              | 25 +++++++++++++++++--------
 src/sss_client/nss_mc_group.c           |  2 +-
 src/sss_client/nss_netgroup.c           | 10 ++++++++--
 src/sss_client/nss_passwd.c             | 19 ++++++++++++++-----
 src/sss_client/nss_services.c           | 21 +++++++++++++++------
 src/sss_client/pam_sss.c                |  4 ++--
 src/util/refcount.c                     |  4 ++--
 18 files changed, 117 insertions(+), 69 deletions(-)

diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index 5751cd018a05876631dce838cfe9d5d58a6c2a6f..e1dbcc963d1d7a176830fab306ebf9208ceef894 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -1528,7 +1528,7 @@ static void process_config_file(struct tevent_context *ev,
         goto done;
     }
 
-    in_event = (struct inotify_event *)buf;
+    in_event = (void *)buf;
 
     if (in_event->len > 0) {
         /* Read in the name, even though we don't use it,
diff --git a/src/providers/ipa/ipa_dyndns.c b/src/providers/ipa/ipa_dyndns.c
index 66515e840ebcf20e78deb405c3c456e1905032a5..92e960a7a54a72b44dbc0e00baf73762e8590cf3 100644
--- a/src/providers/ipa/ipa_dyndns.c
+++ b/src/providers/ipa/ipa_dyndns.c
@@ -185,7 +185,8 @@ static bool ok_for_dns(struct sockaddr *sa)
     char straddr[INET6_ADDRSTRLEN];
 
     if (sa->sa_family == AF_INET6) {
-        struct in6_addr *addr = &((struct sockaddr_in6 *) sa)->sin6_addr;
+        struct sockaddr_in6 *sa_in6 = (void *)sa;
+        struct in6_addr *addr = &sa_in6->sin6_addr;
 
         if (inet_ntop(AF_INET6, addr, straddr, INET6_ADDRSTRLEN) == NULL) {
             DEBUG(SSSDBG_MINOR_FAILURE,
@@ -204,7 +205,8 @@ static bool ok_for_dns(struct sockaddr *sa)
             return false;
         }
     } else if (sa->sa_family == AF_INET) {
-        struct in_addr *addr = &((struct sockaddr_in *) sa)->sin_addr;
+        struct sockaddr_in *sa_in = (void *)sa;
+        struct in_addr *addr = &sa_in->sin_addr;
 
         if (inet_ntop(AF_INET, addr, straddr, INET6_ADDRSTRLEN) == NULL) {
             DEBUG(SSSDBG_MINOR_FAILURE,
diff --git a/src/providers/krb5/krb5_child_handler.c b/src/providers/krb5/krb5_child_handler.c
index 768d8c7dce1a8dd20b471f6abb51b16d2da4563b..0138df8c769ee0a049624010d4ea74f049155129 100644
--- a/src/providers/krb5/krb5_child_handler.c
+++ b/src/providers/krb5/krb5_child_handler.c
@@ -503,10 +503,10 @@ parse_krb5_child_response(TALLOC_CTX *mem_ctx, uint8_t *buf, ssize_t len,
         }
 
         if (msg_type == SSS_PAM_USER_INFO) {
-            msg_subtype = (uint32_t *)&buf[p];
+            msg_subtype = (void *)&buf[p];
             if (*msg_subtype == SSS_PAM_USER_INFO_EXPIRE_WARN)
             {
-                expiration = (uint32_t *)&buf[p+sizeof(uint32_t)];
+                expiration = (void *)&buf[p+sizeof(uint32_t)];
                 if (pwd_exp_warning > 0 &&
                     difftime(pwd_exp_warning, *expiration) < 0.0) {
                     skip = true;
diff --git a/src/responder/common/responder_cmd.c b/src/responder/common/responder_cmd.c
index 16f38eafd6a3362b8338ced459caa88bf2a4edb7..bbbe4f625f612b9078d416fb2f8f464264ff59c7 100644
--- a/src/responder/common/responder_cmd.c
+++ b/src/responder/common/responder_cmd.c
@@ -51,8 +51,8 @@ int sss_cmd_empty_packet(struct sss_packet *packet)
     if (ret != EOK) return ret;
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = 0; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_SET_UINT32(body, 0, NULL); /* num results */
+    SAFEALIGN_SET_UINT32(body+sizeof(uint32_t), 0, NULL); /* reserved */
 
     return EOK;
 }
@@ -133,9 +133,9 @@ int sss_cmd_get_version(struct cli_ctx *cctx)
         return ret;
     }
     sss_packet_get_body(cctx->creq->out, &body, &blen);
-    ((uint32_t *)body)[0] = cctx->cli_protocol_version!=NULL ?
-                                cctx->cli_protocol_version->version : 0;
-    DEBUG(5, ("Offered version [%d].\n", ((uint32_t *)body)[0]));
+    SAFEALIGN_SET_UINT32(body, cctx->cli_protocol_version!=NULL ?
+                               cctx->cli_protocol_version->version : 0, NULL);
+    DEBUG(5, ("Offered version [%d].\n", *body));
 
     sss_cmd_done(cctx, NULL);
     return EOK;
diff --git a/src/responder/common/responder_packet.c b/src/responder/common/responder_packet.c
index 5132d955b71eb025cf54a41edd41da779d352f2e..f8b2b293ae9f822a8c87c6b411c50660bcddd65f 100644
--- a/src/responder/common/responder_packet.c
+++ b/src/responder/common/responder_packet.c
@@ -74,11 +74,15 @@ int sss_packet_new(TALLOC_CTX *mem_ctx, size_t size,
     }
     memset(packet->buffer, 0, SSS_NSS_HEADER_SIZE);
 
-    packet->len = &((uint32_t *)packet->buffer)[0];
-    packet->cmd = &((uint32_t *)packet->buffer)[1];
-    packet->status = &((uint32_t *)packet->buffer)[2];
-    packet->reserved = &((uint32_t *)packet->buffer)[3];
-    packet->body = (uint8_t *)&((uint32_t *)packet->buffer)[4];
+    SAFEALIGN_SET_VALUE(&packet->len, packet->buffer, void *, NULL);
+    SAFEALIGN_SET_VALUE(&packet->cmd,
+                         packet->buffer+sizeof(uint32_t), void *, NULL);
+    SAFEALIGN_SET_VALUE(&packet->status,
+                         packet->buffer+(2*sizeof(uint32_t)), void *, NULL);
+    SAFEALIGN_SET_VALUE(&packet->reserved,
+                         packet->buffer+(3*sizeof(uint32_t)), void *, NULL);
+    SAFEALIGN_SET_VALUE(&packet->body,
+                         packet->buffer+(4*sizeof(uint32_t)), void *, NULL);
 
     *(packet->len) = size + SSS_NSS_HEADER_SIZE;
     *(packet->cmd) = cmd;
@@ -123,12 +127,19 @@ int sss_packet_grow(struct sss_packet *packet, size_t size)
         /* re-set pointers if realloc had to move memory */
         if (newmem != packet->buffer) {
             packet->buffer = newmem;
-            packet->len = &((uint32_t *)packet->buffer)[0];
-            packet->cmd = &((uint32_t *)packet->buffer)[1];
-            packet->status = &((uint32_t *)packet->buffer)[2];
-            packet->reserved = &((uint32_t *)packet->buffer)[3];
-            packet->body = (uint8_t *)&((uint32_t *)packet->buffer)[4];
-        }
+            SAFEALIGN_SET_VALUE(&packet->len, packet->buffer, void *, NULL);
+            SAFEALIGN_SET_VALUE(&packet->cmd, packet->buffer+sizeof(uint32_t),
+                                void *, NULL);
+            SAFEALIGN_SET_VALUE(&packet->status,
+                                packet->buffer+(2*sizeof(uint32_t)),
+                                void *, NULL);
+            SAFEALIGN_SET_VALUE(&packet->reserved,
+                                packet->buffer+(3*sizeof(uint32_t)),
+                                void *, NULL);
+            SAFEALIGN_SET_VALUE(&packet->body,
+                                packet->buffer+(4*sizeof(uint32_t)),
+                                void *, NULL);
+       }
     }
 
     *(packet->len) += size;
diff --git a/src/responder/nss/nsssrv_cmd.c b/src/responder/nss/nsssrv_cmd.c
index 64fd7a587b75d30d4517ee1500b4b3b6bf8bec14..cbcf60d64b4b90aebebea25567647c86dcd49bc5 100644
--- a/src/responder/nss/nsssrv_cmd.c
+++ b/src/responder/nss/nsssrv_cmd.c
@@ -398,8 +398,8 @@ done:
     if (!packet_initialized) return ENOENT;
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = num; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_COPY_UINT32(body, &num, NULL); /* num results */
+    SAFEALIGN_SET_UINT32(body+sizeof(uint32_t), 0, NULL); /* reserved */
 
     return EOK;
 }
@@ -1119,7 +1119,7 @@ static int nss_cmd_getpwuid(struct cli_ctx *cctx)
         ret = EINVAL;
         goto done;
     }
-    cmdctx->id = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&cmdctx->id, body, NULL);
 
     ret = sss_ncache_check_uid(nctx->ncache, nctx->neg_timeout, cmdctx->id);
     if (ret == EEXIST) {
@@ -1569,7 +1569,7 @@ static int nss_cmd_getpwent_immediate(struct nss_cmd_ctx *cmdctx)
     if (blen != sizeof(uint32_t)) {
         return EINVAL;
     }
-    num = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&num, body, NULL);
 
     /* create response packet */
     ret = sss_packet_new(cctx->creq, 0,
@@ -2043,8 +2043,8 @@ done:
         return ENOENT;
     }
 
-    ((uint32_t *)body)[0] = num; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_COPY_UINT32(body, &num, NULL); /* num results */
+    SAFEALIGN_SET_UINT32(body+sizeof(uint32_t), 0, NULL); /* reserved */
 
     return EOK;
 }
@@ -2583,7 +2583,7 @@ static int nss_cmd_getgrgid(struct cli_ctx *cctx)
         ret = EINVAL;
         goto done;
     }
-    cmdctx->id = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&cmdctx->id, body, NULL);
 
     ret = sss_ncache_check_gid(nctx->ncache, nctx->neg_timeout, cmdctx->id);
     if (ret == EEXIST) {
@@ -3034,7 +3034,7 @@ static int nss_cmd_getgrent_immediate(struct nss_cmd_ctx *cmdctx)
     if (blen != sizeof(uint32_t)) {
         return EINVAL;
     }
-    num = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&num, body, NULL);
 
     /* create response packet */
     ret = sss_packet_new(cctx->creq, 0,
@@ -3189,12 +3189,12 @@ static int fill_initgr(struct sss_packet *packet, struct ldb_result *res)
                 return EFAULT;
             }
         }
-        ((uint32_t *)body)[2 + bindex] = gid;
+        SAFEALIGN_COPY_UINT32(body+((2+bindex)*sizeof(uint32_t)), &gid, NULL);
         bindex++;
     }
 
-    ((uint32_t *)body)[0] = num-skipped; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_SET_UINT32(body, num-skipped, NULL);
+    SAFEALIGN_SET_UINT32(body+sizeof(uint32_t), 0, NULL);
 
     return EOK;
 }
diff --git a/src/responder/nss/nsssrv_mmap_cache.c b/src/responder/nss/nsssrv_mmap_cache.c
index 07498a9b71981f17a657a919dcc458eb13e2cec2..3cc8fc830c389cb3ced02e82028c76afbd55f7df 100644
--- a/src/responder/nss/nsssrv_mmap_cache.c
+++ b/src/responder/nss/nsssrv_mmap_cache.c
@@ -278,7 +278,7 @@ static struct sss_mc_rec *sss_mc_find_record(struct sss_mc_ctx *mcc,
 
     while (slot != MC_INVALID_VAL) {
         rec = MC_SLOT_TO_PTR(mcc->data_table, slot, struct sss_mc_rec);
-        name_ptr = *((rel_ptr_t *)rec->data);
+        SAFEALIGN_SET_VALUE(&name_ptr, *rec->data, rel_ptr_t, NULL);
 
         t_key = (char *)rec->data + name_ptr;
         if (strcmp(key->str, t_key) == 0) {
diff --git a/src/responder/nss/nsssrv_netgroup.c b/src/responder/nss/nsssrv_netgroup.c
index 774d182bd95ed5d80710e54c2dd2287f6a83d803..c302fb99043275ae34f8ca5084915bc563f0225e 100644
--- a/src/responder/nss/nsssrv_netgroup.c
+++ b/src/responder/nss/nsssrv_netgroup.c
@@ -666,8 +666,8 @@ static void nss_cmd_setnetgrent_done(struct tevent_req *req)
             }
 
             sss_packet_get_body(packet, &body, &blen);
-            ((uint32_t *)body)[0] = 1; /* Got some results */
-            ((uint32_t *)body)[1] = 0; /* reserved */
+            SAFEALIGN_SET_UINT32(body, 1, NULL); /* Got some results */
+            SAFEALIGN_SET_UINT32(body+sizeof(uint32_t), 0, NULL); /* reserved */
         }
 
         sss_cmd_done(cmdctx->cctx, NULL);
@@ -828,7 +828,7 @@ static errno_t nss_cmd_getnetgrent_process(struct nss_cmd_ctx *cmdctx,
     if (blen != sizeof(uint32_t)) {
         return EINVAL;
     }
-    num = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&num, body, NULL);
 
     /* create response packet */
     ret = sss_packet_new(client->creq, 0,
@@ -967,8 +967,8 @@ static errno_t nss_cmd_retnetgrent(struct cli_ctx *client,
     }
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = num; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_COPY_UINT32(body, &num, NULL); /* num results */
+    SAFEALIGN_SET_UINT32(body+sizeof(uint32_t), 0, NULL); /* reserved */
 
     return EOK;
 }
diff --git a/src/responder/nss/nsssrv_services.c b/src/responder/nss/nsssrv_services.c
index 90698231757057e33b9045874b95bdf9a155a52d..a451cf78291a969be4f1d717107477a66aaaf0ef 100644
--- a/src/responder/nss/nsssrv_services.c
+++ b/src/responder/nss/nsssrv_services.c
@@ -768,8 +768,8 @@ done:
         return ENOENT;
     }
 
-    ((uint32_t *)body)[0] = num; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_COPY_UINT32(body, &num, NULL); /* num results */
+    SAFEALIGN_SET_UINT32(body+sizeof(uint32_t), 0, NULL); /* reserved */
 
     return ret;
 }
@@ -1735,7 +1735,7 @@ nss_cmd_getservent_immediate(struct nss_cmd_ctx *cmdctx)
     if (blen != sizeof(uint32_t)) {
         return EINVAL;
     }
-    num = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&num, body, NULL);
 
     /* create response packet */
     ret = sss_packet_new(cctx->creq, 0,
diff --git a/src/responder/pam/pamsrv_cmd.c b/src/responder/pam/pamsrv_cmd.c
index 238b4fa7feee666e3419eae55c4952e45fd7184c..62405acda0ccb9d674c3fd8345b4c5cfde00c3aa 100644
--- a/src/responder/pam/pamsrv_cmd.c
+++ b/src/responder/pam/pamsrv_cmd.c
@@ -129,8 +129,10 @@ static int pam_parse_in_data_v2(struct sss_domain_info *domains,
     int ret;
     uint32_t terminator = SSS_END_OF_PAM_REQUEST;
 
+    SAFEALIGN_COPY_UINT32(&type, &body[0], NULL);
+
     if (blen < 4*sizeof(uint32_t)+2 ||
-        ((uint32_t *)body)[0] != SSS_START_OF_PAM_REQUEST ||
+        type != SSS_START_OF_PAM_REQUEST ||
         memcmp(&body[blen - sizeof(uint32_t)], &terminator, sizeof(uint32_t)) != 0) {
         DEBUG(1, ("Received data is invalid.\n"));
         return EINVAL;
diff --git a/src/sss_client/common.c b/src/sss_client/common.c
index 59eae3b1fe4101383de96f680458467a962c2248..630e5b7afd602c323c2fd15a6190120f3c831ed9 100644
--- a/src/sss_client/common.c
+++ b/src/sss_client/common.c
@@ -417,7 +417,7 @@ static bool sss_cli_check_version(const char *socket_name)
         return false;
     }
 
-    obtained_version = ((uint32_t *)repbuf)[0];
+    SAFEALIGN_COPY_UINT32(&obtained_version, repbuf, NULL);
     free(repbuf);
 
     return (obtained_version == expected_version);
diff --git a/src/sss_client/nss_group.c b/src/sss_client/nss_group.c
index 99b7ad91531345f2b4e3b8bc2b344100d15148c4..3ba8f83f862df823dd3e0072399a12147c5cc72f 100644
--- a/src/sss_client/nss_group.c
+++ b/src/sss_client/nss_group.c
@@ -241,7 +241,7 @@ static int sss_nss_getgr_readrep(struct sss_nss_gr_rep *pr,
     }
 
     /* now members */
-    pr->result->gr_mem = (char **)&(pr->buffer[i+pad]);
+    pr->result->gr_mem = (void *)&(pr->buffer[i+pad]);
     ptmem = (sizeof(char *) * (mem_num + 1)) + pad;
     if (ptmem > dlen) {
         return ERANGE; /* not ENOMEM, ERANGE is what glibc looks for */
@@ -299,7 +299,7 @@ enum nss_status _nss_sss_initgroups_dyn(const char *user, gid_t group,
     }
 
     /* no results if not found */
-    num_ret = ((uint32_t *)repbuf)[0];
+    SAFEALIGN_COPY_UINT32(&num_ret, repbuf, NULL);
     if (num_ret == 0) {
         free(repbuf);
         nret = NSS_STATUS_NOTFOUND;
@@ -329,7 +329,7 @@ enum nss_status _nss_sss_initgroups_dyn(const char *user, gid_t group,
         *size = newsize;
     }
 
-    rbuf = &((uint32_t *)repbuf)[2];
+    SAFEALIGN_SET_VALUE(&rbuf, repbuf+(2*sizeof(uint32_t)), void *, NULL);
     for (l = 0; l < max_ret; l++) {
         (*groups)[*start] = rbuf[l];
         *start += 1;
@@ -350,6 +350,7 @@ enum nss_status _nss_sss_getgrnam_r(const char *name, struct group *result,
     struct sss_cli_req_data rd;
     struct sss_nss_gr_rep grrep;
     uint8_t *repbuf;
+    uint32_t repvalue;
     size_t replen, len, name_len;
     enum nss_status nret;
     int ret;
@@ -403,15 +404,17 @@ enum nss_status _nss_sss_getgrnam_r(const char *name, struct group *result,
     grrep.buffer = buffer;
     grrep.buflen = buflen;
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if (((uint32_t *)repbuf)[0] == 0) {
+    if (repvalue == 0) {
         free(repbuf);
         nret = NSS_STATUS_NOTFOUND;
         goto out;
     }
 
     /* only 1 result is accepted for this function */
-    if (((uint32_t *)repbuf)[0] != 1) {
+    if (repvalue != 1) {
         *errnop = EBADMSG;
         free(repbuf);
         nret = NSS_STATUS_TRYAGAIN;
@@ -444,6 +447,7 @@ enum nss_status _nss_sss_getgrgid_r(gid_t gid, struct group *result,
     struct sss_cli_req_data rd;
     struct sss_nss_gr_rep grrep;
     uint8_t *repbuf;
+    uint32_t repvalue;
     size_t replen, len;
     enum nss_status nret;
     uint32_t group_gid;
@@ -490,15 +494,17 @@ enum nss_status _nss_sss_getgrgid_r(gid_t gid, struct group *result,
     grrep.buffer = buffer;
     grrep.buflen = buflen;
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if (((uint32_t *)repbuf)[0] == 0) {
+    if (repvalue == 0) {
         free(repbuf);
         nret = NSS_STATUS_NOTFOUND;
         goto out;
     }
 
     /* only 1 result is accepted for this function */
-    if (((uint32_t *)repbuf)[0] != 1) {
+    if (repvalue != 1) {
         *errnop = EBADMSG;
         free(repbuf);
         nret = NSS_STATUS_TRYAGAIN;
@@ -552,6 +558,7 @@ static enum nss_status internal_getgrent_r(struct group *result,
     struct sss_cli_req_data rd;
     struct sss_nss_gr_rep grrep;
     uint8_t *repbuf;
+    uint32_t repvalue;
     size_t replen;
     enum nss_status nret;
     uint32_t num_entries;
@@ -599,8 +606,10 @@ static enum nss_status internal_getgrent_r(struct group *result,
         return nret;
     }
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if ((((uint32_t *)repbuf)[0] == 0) || (replen - 8 == 0)) {
+    if ((repvalue == 0) || (replen - 8 == 0)) {
         free(repbuf);
         return NSS_STATUS_NOTFOUND;
     }
diff --git a/src/sss_client/nss_mc_group.c b/src/sss_client/nss_mc_group.c
index 7beeb28232031f2c27f59dd3054f1f4ae1b9238a..a31b7024c11375dc9f2699eee58c927c53470bdd 100644
--- a/src/sss_client/nss_mc_group.c
+++ b/src/sss_client/nss_mc_group.c
@@ -64,7 +64,7 @@ static errno_t sss_nss_mc_parse_result(struct sss_mc_rec *rec,
 
     /* fill in group */
     result->gr_gid = data->gid;
-    result->gr_mem = (char **)buffer;
+    result->gr_mem = (void *)buffer;
     result->gr_mem[data->members] = NULL;
 
     cookie = NULL;
diff --git a/src/sss_client/nss_netgroup.c b/src/sss_client/nss_netgroup.c
index f72d547e81c21ca80b22002cdee8854a9182e125..b3994a5ae0662ef0308957de68c34fa972f0720f 100644
--- a/src/sss_client/nss_netgroup.c
+++ b/src/sss_client/nss_netgroup.c
@@ -159,6 +159,7 @@ enum nss_status _nss_sss_setnetgrent(const char *netgroup,
                      struct __netgrent *result)
 {
     uint8_t *repbuf = NULL;
+    uint32_t repvalue;
     size_t replen;
     enum nss_status nret;
     struct sss_cli_req_data rd;
@@ -198,8 +199,10 @@ enum nss_status _nss_sss_setnetgrent(const char *netgroup,
         goto out;
     }
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if ((((uint32_t *)repbuf)[0] == 0) || (replen < NETGR_METADATA_COUNT)) {
+    if ((repvalue == 0) || (replen < NETGR_METADATA_COUNT)) {
         free(repbuf);
         nret = NSS_STATUS_NOTFOUND;
         goto out;
@@ -220,6 +223,7 @@ static enum nss_status internal_getnetgrent_r(struct __netgrent *result,
     struct sss_cli_req_data rd;
     struct sss_nss_netgr_rep netgrrep;
     uint8_t *repbuf;
+    uint32_t repvalue;
     size_t replen;
     enum nss_status nret;
     uint32_t num_entries;
@@ -266,8 +270,10 @@ static enum nss_status internal_getnetgrent_r(struct __netgrent *result,
         return nret;
     }
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if ((((uint32_t *)repbuf)[0] == 0) || (replen <= NETGR_METADATA_COUNT)) {
+    if ((repvalue == 0) || (replen <= NETGR_METADATA_COUNT)) {
         free(repbuf);
         return NSS_STATUS_RETURN;
     }
diff --git a/src/sss_client/nss_passwd.c b/src/sss_client/nss_passwd.c
index e2f488b97f137808773ec65f5d7f398fbbefeceb..487ce7c566c8e5b0b851f4d5259a42740c618f61 100644
--- a/src/sss_client/nss_passwd.c
+++ b/src/sss_client/nss_passwd.c
@@ -139,6 +139,7 @@ enum nss_status _nss_sss_getpwnam_r(const char *name, struct passwd *result,
     struct sss_cli_req_data rd;
     struct sss_nss_pw_rep pwrep;
     uint8_t *repbuf;
+    uint32_t repvalue;
     size_t replen, len, name_len;
     enum nss_status nret;
     int ret;
@@ -188,15 +189,17 @@ enum nss_status _nss_sss_getpwnam_r(const char *name, struct passwd *result,
     pwrep.buffer = buffer;
     pwrep.buflen = buflen;
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if (((uint32_t *)repbuf)[0] == 0) {
+    if (repvalue == 0) {
         free(repbuf);
         nret = NSS_STATUS_NOTFOUND;
         goto out;
     }
 
     /* only 1 result is accepted for this function */
-    if (((uint32_t *)repbuf)[0] != 1) {
+    if (repvalue != 1) {
         *errnop = EBADMSG;
         free(repbuf);
         nret = NSS_STATUS_TRYAGAIN;
@@ -225,6 +228,7 @@ enum nss_status _nss_sss_getpwuid_r(uid_t uid, struct passwd *result,
     struct sss_cli_req_data rd;
     struct sss_nss_pw_rep pwrep;
     uint8_t *repbuf;
+    uint32_t repvalue;
     size_t replen, len;
     enum nss_status nret;
     uint32_t user_uid;
@@ -267,15 +271,17 @@ enum nss_status _nss_sss_getpwuid_r(uid_t uid, struct passwd *result,
     pwrep.buffer = buffer;
     pwrep.buflen = buflen;
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if (((uint32_t *)repbuf)[0] == 0) {
+    if (repvalue == 0) {
         free(repbuf);
         nret = NSS_STATUS_NOTFOUND;
         goto out;
     }
 
     /* only 1 result is accepted for this function */
-    if (((uint32_t *)repbuf)[0] != 1) {
+    if (repvalue != 1) {
         *errnop = EBADMSG;
         free(repbuf);
         nret = NSS_STATUS_TRYAGAIN;
@@ -325,6 +331,7 @@ static enum nss_status internal_getpwent_r(struct passwd *result,
     struct sss_cli_req_data rd;
     struct sss_nss_pw_rep pwrep;
     uint8_t *repbuf;
+    uint32_t repvalue;
     size_t replen;
     enum nss_status nret;
     uint32_t num_entries;
@@ -370,8 +377,10 @@ static enum nss_status internal_getpwent_r(struct passwd *result,
         return nret;
     }
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if ((((uint32_t *)repbuf)[0] == 0) || (replen - 8 == 0)) {
+    if ((repvalue == 0) || (replen - 8 == 0)) {
         free(repbuf);
         return NSS_STATUS_NOTFOUND;
     }
diff --git a/src/sss_client/nss_services.c b/src/sss_client/nss_services.c
index 5f98d8dc983a197939c501baad54aa62cdb5176b..c6a6f14346b21575d5fc475f9fea1ec4224d31b8 100644
--- a/src/sss_client/nss_services.c
+++ b/src/sss_client/nss_services.c
@@ -134,7 +134,7 @@ sss_nss_getsvc_readrep(struct sss_nss_svc_rep *sr,
     }
 
     /* Copy in the aliases */
-    sr->result->s_aliases = (char **) &(sr->buffer[i+pad]);
+    sr->result->s_aliases = (void *) &(sr->buffer[i+pad]);
 
     ptaliases = (sizeof(char *) * (num_aliases + 1)) + pad;
     if (ptaliases > dlen) {
@@ -174,6 +174,7 @@ _nss_sss_getservbyname_r(const char *name,
     size_t proto_len = 0;
     uint8_t *repbuf;
     uint8_t *data;
+    uint32_t repvalue;
     size_t replen, len;
     enum nss_status nret;
     int ret;
@@ -225,15 +226,17 @@ _nss_sss_getservbyname_r(const char *name,
     svcrep.buffer = buffer;
     svcrep.buflen = buflen;
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if (((uint32_t *)repbuf)[0] == 0) {
+    if (repvalue == 0) {
         free(repbuf);
         nret = NSS_STATUS_NOTFOUND;
         goto out;
     }
 
     /* only 1 result is accepted for this function */
-    if (((uint32_t *)repbuf)[0] != 1) {
+    if (repvalue != 1) {
         *errnop = EBADMSG;
         free(repbuf);
         nret = NSS_STATUS_TRYAGAIN;
@@ -272,6 +275,7 @@ _nss_sss_getservbyport_r(int port, const char *protocol,
     uint8_t *data;
     size_t p = 0;
     size_t replen, len;
+    uint32_t repvalue;
     enum nss_status nret;
     int ret;
 
@@ -320,15 +324,17 @@ _nss_sss_getservbyport_r(int port, const char *protocol,
     svcrep.buffer = buffer;
     svcrep.buflen = buflen;
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if (((uint32_t *)repbuf)[0] == 0) {
+    if (repvalue == 0) {
         free(repbuf);
         nret = NSS_STATUS_NOTFOUND;
         goto out;
     }
 
     /* only 1 result is accepted for this function */
-    if (((uint32_t *)repbuf)[0] != 1) {
+    if (repvalue != 1) {
         *errnop = EBADMSG;
         free(repbuf);
         nret = NSS_STATUS_TRYAGAIN;
@@ -399,6 +405,7 @@ static enum nss_status internal_getservent_r(struct servent *result,
     struct sss_cli_req_data rd;
     struct sss_nss_svc_rep pwrep;
     uint8_t *repbuf;
+    uint32_t repvalue;
     size_t replen;
     enum nss_status nret;
     uint32_t num_entries;
@@ -444,8 +451,10 @@ static enum nss_status internal_getservent_r(struct servent *result,
         return nret;
     }
 
+    SAFEALIGN_COPY_UINT32(&repvalue, repbuf, NULL);
+
     /* no results if not found */
-    if ((((uint32_t *)repbuf)[0] == 0)
+    if ((repvalue == 0)
             || (replen - SVC_METADATA_COUNT == 0)) {
         free(repbuf);
         return NSS_STATUS_NOTFOUND;
diff --git a/src/sss_client/pam_sss.c b/src/sss_client/pam_sss.c
index efbc48b6ef0458bb79dad53f81c584611f2be22d..f122e027fd3c2c37034dbacacfd0058d38b8de13 100644
--- a/src/sss_client/pam_sss.c
+++ b/src/sss_client/pam_sss.c
@@ -243,7 +243,7 @@ static int pack_message_v3(struct pam_items *pi, size_t *size,
     }
 
     rp = 0;
-    ((uint32_t *)(&buf[rp]))[0] = SSS_START_OF_PAM_REQUEST;
+    SAFEALIGN_SET_UINT32(&buf[rp], SSS_START_OF_PAM_REQUEST, NULL);
     rp += sizeof(uint32_t);
 
     rp += add_string_item(SSS_PAM_ITEM_USER, pi->pam_user, pi->pam_user_size,
@@ -1093,7 +1093,7 @@ static int send_and_receive(pam_handle_t *pamh, struct pam_items *pi,
         goto done;
     }
 
-    pam_status = ((int32_t *)repbuf)[0];
+    memcpy(&pam_status, repbuf, sizeof(int32_t));
     ret = eval_response(pamh, replen, repbuf, pi);
     if (ret != PAM_SUCCESS) {
         D(("eval_response failed."));
diff --git a/src/util/refcount.c b/src/util/refcount.c
index 735170d1b1f1468444f35059abb1229db65a9539..717c58539784c9278fa2419ca9e80c74e2f6d2ee 100644
--- a/src/util/refcount.c
+++ b/src/util/refcount.c
@@ -60,7 +60,7 @@ _rc_alloc(const void *context, size_t size, size_t refcount_offset,
         return NULL;
     };
 
-    wrapper->refcount = (int *)((char *)wrapper->ptr + refcount_offset);
+    wrapper->refcount = (void *)((char *)wrapper->ptr + refcount_offset);
     *wrapper->refcount = 1;
 
     talloc_set_destructor(wrapper, refcount_destructor);
@@ -79,7 +79,7 @@ _rc_reference(const void *context, size_t refcount_offset, void *source)
     }
 
     wrapper->ptr = source;
-    wrapper->refcount = (int *)((char *)wrapper->ptr + refcount_offset);
+    wrapper->refcount = (void *)((char *)wrapper->ptr + refcount_offset);
     (*wrapper->refcount)++;
 
     talloc_set_destructor(wrapper, refcount_destructor);
-- 
1.7.11.4

