| 232 | } |
| 233 | |
| 234 | static int csv_increase_buffer(struct csv_parser *p) { |
| 235 | /* Increase the size of the entry buffer. Attempt to increase size by |
| 236 | * p->blk_size, if this is larger than SIZE_MAX try to increase current |
| 237 | * buffer size to SIZE_MAX. If allocation fails, try to allocate halve |
| 238 | * the size and try again until successful or increment size is zero. |
| 239 | */ |
| 240 | |
| 241 | size_t to_add = p->blk_size; |
| 242 | void *vp; |
| 243 | |
| 244 | if (p->entry_size >= SIZE_MAX - to_add) to_add = SIZE_MAX - p->entry_size; |
| 245 | |
| 246 | if (!to_add) { |
| 247 | p->status = CSV_ETOOBIG; |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | while ((vp = p->realloc_func(p->entry_buf, p->entry_size + to_add)) == NULL) { |
| 252 | to_add /= 2; |
| 253 | if (!to_add) { |
| 254 | p->status = CSV_ENOMEM; |
| 255 | return -1; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* Update entry buffer pointer and entry_size if successful */ |
| 260 | p->entry_buf = vp; |
| 261 | p->entry_size += to_add; |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | size_t csv_parse(struct csv_parser *p, const void *s, size_t len, |
| 266 | void (*cb1)(void *, size_t, void *), |