Changeset 533 in openpam
- Timestamp:
- 03/31/12 14:25:43 (14 months ago)
- File:
-
- 1 edited
-
trunk/lib/openpam_straddch.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/openpam_straddch.c
r530 r533 35 35 36 36 #include <security/pam_appl.h> 37 37 38 #include "openpam_impl.h" 39 40 #define MIN_STR_SIZE 32 38 41 39 42 /* … … 46 49 openpam_straddch(char **str, size_t *size, size_t *len, char ch) 47 50 { 48 char *tmp; 51 size_t tmpsize; 52 char *tmpstr; 49 53 50 54 if (*str == NULL) { 51 55 /* initial allocation */ 52 if ((*str = malloc(*size = 32)) == NULL) { 56 tmpsize = MIN_STR_SIZE 57 if ((tmpstr = malloc(tmpsize)) == NULL) { 53 58 openpam_log(PAM_LOG_ERROR, "malloc(): %m"); 54 59 return (-1); 55 60 } 61 *str = tmpstr; 62 *size = tmpsize; 56 63 *len = 0; 57 64 } else if (*len >= *size - 1) { 58 65 /* additional space required */ 59 if ((tmp = realloc(*str, *size *= 2)) == NULL) { 66 tmpsize = *size * 2; 67 if ((tmpstr = realloc(*str, tmpsize)) == NULL) { 60 68 openpam_log(PAM_LOG_ERROR, "realloc(): %m"); 61 free(*str);62 *str = NULL;63 69 return (-1); 64 70 } 65 *str = tmp; 71 size = tmpsize; 72 *str = tmpstr; 66 73 } 67 74 (*str)[*len] = ch; … … 71 78 } 72 79 73 /* 74 * NODOC 80 /** 81 * The =openpam_straddch function appends a character to a dynamically 82 * allocated NUL-terminated buffer, reallocating the buffer as needed. 83 * 84 * The =str argument points to a variable containing either a pointer to 85 * an existing buffer or =NULL. 86 * If the value of the variable pointed to by =str is =NULL, a new buffer 87 * is allocated. 88 * 89 * The =size and =len argument point to variables used to hold the size 90 * of the buffer and the length of the string it contains, respectively. 91 * 92 * If a new buffer is allocated or an existing buffer is reallocated to 93 * make room for the additional character, =str and =size are updated 94 * accordingly. 95 * 96 * The =openpam_straddch function ensures that the buffer is always 97 * NUL-terminated. 98 * 99 * If the =openpam_straddch function is successful, it increments the 100 * integer variable pointed to by =len and returns 0. 101 * Otherwise, it leaves the variables pointed to by =str, =size and =len 102 * unmodified and returns -1. 75 103 */
Note: See TracChangeset
for help on using the changeset viewer.