From fd3714d0cf068f3c782c1fff32105fc51cc97a0e Mon Sep 17 00:00:00 2001 From: Stephen Gallagher Date: Fri, 6 Jan 2012 16:47:50 -0500 Subject: 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. --- src/sss_client/common.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/sss_client/common.c') diff --git a/src/sss_client/common.c b/src/sss_client/common.c index 94d15a529..998f7c8ce 100644 --- a/src/sss_client/common.c +++ b/src/sss_client/common.c @@ -918,3 +918,33 @@ void sss_pam_lock(void) { return; } void sss_pam_unlock(void) { return; } #endif + +errno_t sss_readrep_copy_string(const char *in, + size_t *offset, + size_t *slen, + size_t *dlen, + char **out, + size_t *size) +{ + size_t i = 0; + while (*slen > *offset && *dlen > 0) { + (*out)[i] = in[*offset]; + if ((*out)[i] == '\0') break; + i++; + (*offset)++; + (*dlen)--; + } + if (*slen <= *offset) { /* premature end of buf */ + return EBADMSG; + } + if (*dlen == 0) { /* not enough memory */ + return ERANGE; /* not ENOMEM, ERANGE is what glibc looks for */ + } + (*offset)++; + (*dlen)--; + if (size) { + *size = i; + } + + return EOK; +} -- cgit