diff --git a/lib/openpam_straddch.c b/lib/openpam_straddch.c index 8f168fc..7150725 100644 --- a/lib/openpam_straddch.c +++ b/lib/openpam_straddch.c @@ -65,7 +65,7 @@ openpam_straddch(char **str, size_t *size, size_t *len, int ch) *str = tmpstr; *size = tmpsize; *len = 0; - } else if (*len + 1 >= *size) { + } else if (ch != 0 && *len + 1 >= *size) { /* additional space required */ tmpsize = *size * 2; if ((tmpstr = realloc(*str, tmpsize)) == NULL) { @@ -76,8 +76,10 @@ openpam_straddch(char **str, size_t *size, size_t *len, int ch) *size = tmpsize; *str = tmpstr; } - (*str)[*len] = ch; - ++*len; + if (ch != 0) { + (*str)[*len] = ch; + ++*len; + } (*str)[*len] = '\0'; return (0); } @@ -94,6 +96,11 @@ openpam_straddch(char **str, size_t *size, size_t *len, int ch) * The =size and =len argument point to variables used to hold the size * of the buffer and the length of the string it contains, respectively. * + * The final argument, =ch, is the character that should be appended to + * the string. If =ch is 0, nothing is appended, but a new buffer is + * still allocated if =str is NULL. This can be used to "bootstrap" the + * string. + * * If a new buffer is allocated or an existing buffer is reallocated to * make room for the additional character, =str and =size are updated * accordingly. @@ -102,7 +109,7 @@ openpam_straddch(char **str, size_t *size, size_t *len, int ch) * NUL-terminated. * * If the =openpam_straddch function is successful, it increments the - * integer variable pointed to by =len and returns 0. + * integer variable pointed to by =len (unless =ch was 0) and returns 0. * Otherwise, it leaves the variables pointed to by =str, =size and =len * unmodified, sets :errno to =ENOMEM and returns -1. *