Append the given printf-style formatted string to the pString 'ps'. * This is much cleaner (and faster) than the technique this file * used to use! */
| 155 | * This is much cleaner (and faster) than the technique this file |
| 156 | * used to use! */ |
| 157 | static void im_iprintf(pString *ps, const char *fmt, ...) { |
| 158 | int n, remaining; |
| 159 | va_list ap; |
| 160 | do { |
| 161 | remaining = *(ps->alloc_size) - ps->string_len; |
| 162 | va_start(ap, fmt); |
| 163 | #if defined(HAVE_VSNPRINTF) |
| 164 | n = vsnprintf((*(ps->string)) + ps->string_len, |
| 165 | remaining, fmt, ap); |
| 166 | #else |
| 167 | /* If vsnprintf() is not available then require a minimum |
| 168 | * of 512 bytes of free space to prevent a buffer overflow |
| 169 | * This is not fully bulletproof but should help, see bug 1613 |
| 170 | */ |
| 171 | if (remaining < 512) |
| 172 | n = -1; |
| 173 | else |
| 174 | n = vsprintf((*(ps->string)) + ps->string_len, fmt, ap); |
| 175 | #endif |
| 176 | va_end(ap); |
| 177 | /* if that worked, we're done! */ |
| 178 | if (-1<n && n<remaining) { |
| 179 | ps->string_len += n; |
| 180 | return; |
| 181 | } else { /* double allocated string size */ |
| 182 | *(ps->alloc_size) *= 2;/* these keeps realloc linear.*/ |
| 183 | if (*(ps->alloc_size) < 1024) |
| 184 | /* careful: initial size can be 0 */ |
| 185 | *(ps->alloc_size)=1024; |
| 186 | if (n>-1 && *(ps->alloc_size) <= (n + ps->string_len)) |
| 187 | /* ensure at least as much as what is needed */ |
| 188 | *(ps->alloc_size) = n+ps->string_len+1; |
| 189 | *(ps->string) = (char *) msSmallRealloc |
| 190 | (*(ps->string), *(ps->alloc_size)); |
| 191 | /* if realloc doesn't work, we're screwed! */ |
| 192 | } |
| 193 | } while (1); /* go back and try again. */ |
| 194 | } |
| 195 | |
| 196 | |
| 197 |
no test coverage detected