diff options
author | Stephen Gallagher <sgallagh@redhat.com> | 2012-01-06 16:47:50 -0500 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2012-01-18 09:27:56 -0500 |
commit | fd3714d0cf068f3c782c1fff32105fc51cc97a0e (patch) | |
tree | 4763af29c0a5e3fa2dd293744dccb1f268e7fca9 /src/sss_client/nss_group.c | |
parent | 4e19af30cbaf819bdd88f7d0390aeabeb2797a60 (diff) | |
download | sssd-fd3714d0cf068f3c782c1fff32105fc51cc97a0e.tar.gz sssd-fd3714d0cf068f3c782c1fff32105fc51cc97a0e.tar.bz2 sssd-fd3714d0cf068f3c782c1fff32105fc51cc97a0e.zip |
NSS: Add sss_readrep_copy_string
There were many places in the client code where we were
duplicating a loop to copy data in from the response buffer. This
patch turns those loops into a function for easier maintenance and
easier-to-read *readrep() routines.
Diffstat (limited to 'src/sss_client/nss_group.c')
-rw-r--r-- | src/sss_client/nss_group.c | 66 |
1 files changed, 20 insertions, 46 deletions
diff --git a/src/sss_client/nss_group.c b/src/sss_client/nss_group.c index f5e715c8..7e5f79ad 100644 --- a/src/sss_client/nss_group.c +++ b/src/sss_client/nss_group.c @@ -78,8 +78,8 @@ struct sss_nss_gr_rep { static int sss_nss_getgr_readrep(struct sss_nss_gr_rep *pr, uint8_t *buf, size_t *len) { - size_t i, l, slen, ptmem, pad; - ssize_t dlen; + errno_t ret; + size_t i, l, slen, ptmem, pad, dlen, glen; char *sbuf; uint32_t mem_num; uint32_t c; @@ -98,36 +98,19 @@ static int sss_nss_getgr_readrep(struct sss_nss_gr_rep *pr, pr->result->gr_name = &(pr->buffer[0]); i = 0; - while (slen > i && dlen > 0) { - pr->buffer[i] = sbuf[i]; - if (pr->buffer[i] == '\0') break; - i++; - dlen--; - } - if (slen <= i) { /* premature end of buf */ - return EBADMSG; - } - if (dlen <= 0) { /* not enough memory */ - return ERANGE; /* not ENOMEM, ERANGE is what glibc looks for */ - } - i++; - dlen--; + + ret = sss_readrep_copy_string(sbuf, &i, + &slen, &dlen, + &pr->result->gr_name, + NULL); + if (ret != EOK) return ret; pr->result->gr_passwd = &(pr->buffer[i]); - while (slen > i && dlen > 0) { - pr->buffer[i] = sbuf[i]; - if (pr->buffer[i] == '\0') break; - i++; - dlen--; - } - if (slen <= i) { /* premature end of buf */ - return EBADMSG; - } - if (dlen <= 0) { /* not enough memory */ - return ERANGE; /* not ENOMEM, ERANGE is what glibc looks for */ - } - i++; - dlen--; + ret = sss_readrep_copy_string(sbuf, &i, + &slen, &dlen, + &pr->result->gr_passwd, + NULL); + if (ret != EOK) return ret; /* Make sure pr->buffer[i+pad] is 32 bit aligned */ pad = 0; @@ -147,22 +130,13 @@ static int sss_nss_getgr_readrep(struct sss_nss_gr_rep *pr, for (l = 0; l < mem_num; l++) { pr->result->gr_mem[l] = &(pr->buffer[ptmem]); - while ((slen > i) && (dlen > 0)) { - pr->buffer[ptmem] = sbuf[i]; - if (pr->buffer[ptmem] == '\0') break; - i++; - dlen--; - ptmem++; - } - if (slen <= i) { /* premature end of buf */ - return EBADMSG; - } - if (dlen <= 0) { /* not enough memory */ - return ERANGE; /* not ENOMEM, ERANGE is what glibc looks for */ - } - i++; - dlen--; - ptmem++; + ret = sss_readrep_copy_string(sbuf, &i, + &slen, &dlen, + &pr->result->gr_mem[l], + &glen); + if (ret != EOK) return ret; + + ptmem += glen + 1; } *len = slen -i; |