Changeset 634 in openpam


Ignore:
Timestamp:
Mar 3, 2013, 11:23:10 PM (8 years ago)
Author:
Dag-Erling Smørgrav
Message:

If ch == '\0', do not grow the string or advance the length counter,
but do allocate a string if there is none to begin with. This makes
it possible to use openpam_straddch(3) to preallocate the string (if
necessary) instead of manually calling malloc(3) or calloc(3) and
initializing size and len.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/openpam_straddch.c

    r619 r634  
    6666                *size = tmpsize;
    6767                *len = 0;
    68         } else if (*len + 1 >= *size) {
     68        } else if (ch != 0 && *len + 1 >= *size) {
    6969                /* additional space required */
    7070                tmpsize = *size * 2;
     
    7777                *str = tmpstr;
    7878        }
    79         (*str)[*len] = ch;
    80         ++*len;
     79        if (ch != 0) {
     80                (*str)[*len] = ch;
     81                ++*len;
     82        }
    8183        (*str)[*len] = '\0';
    8284        return (0);
     
    9597 * of the buffer and the length of the string it contains, respectively.
    9698 *
     99 * The final argument, =ch, is the character that should be appended to
     100 * the string.  If =ch is 0, nothing is appended, but a new buffer is
     101 * still allocated if =str is NULL.  This can be used to "bootstrap" the
     102 * string.
     103 *
    97104 * If a new buffer is allocated or an existing buffer is reallocated to
    98105 * make room for the additional character, =str and =size are updated
     
    103110 *
    104111 * If the =openpam_straddch function is successful, it increments the
    105  * integer variable pointed to by =len and returns 0.
     112 * integer variable pointed to by =len (unless =ch was 0) and returns 0.
    106113 * Otherwise, it leaves the variables pointed to by =str, =size and =len
    107114 * unmodified, sets :errno to =ENOMEM and returns -1.
Note: See TracChangeset for help on using the changeset viewer.