diff options
| author | Craig Barnes <craigbarnes@protonmail.com> | 2022-03-02 01:55:25 +0000 |
|---|---|---|
| committer | Craig Barnes <craigbarnes@protonmail.com> | 2022-03-02 01:55:25 +0000 |
| commit | a3c5e8927de8ba8c763230c7e1015bf7fad83ebc (patch) | |
| tree | 279774c8b18245e51a5c9c5fcfd8ec89bc77c8fc /osc.c | |
| parent | Wrap setlocale in asserts since it can fail (diff) | |
osc: grow OSC buffer exponentially instead of by fixed increments of 128
With fixed increments of 128 bytes, an OSC 52 copy operation could end
up doing thousands or tens of thousands of realloc(3) calls just to
copy a few MB.
Diffstat (limited to 'osc.c')
| -rw-r--r-- | osc.c | 16 |
1 files changed, 13 insertions, 3 deletions
@@ -902,11 +902,20 @@ osc_dispatch(struct terminal *term) bool osc_ensure_size(struct terminal *term, size_t required_size) { - if (required_size <= term->vt.osc.size) + if (likely(required_size <= term->vt.osc.size)) return true; - size_t new_size = (required_size + 127) / 128 * 128; - xassert(new_size > 0); + const size_t pow2_max = ~(SIZE_MAX >> 1); + if (unlikely(required_size > pow2_max)) { + LOG_ERR("required OSC buffer size (%zu) exceeds limit (%zu)", + required_size, pow2_max); + return false; + } + + size_t new_size = max(term->vt.osc.size, 4096); + while (new_size < required_size) { + new_size <<= 1; + } uint8_t *new_data = realloc(term->vt.osc.data, new_size); if (new_data == NULL) { @@ -914,6 +923,7 @@ osc_ensure_size(struct terminal *term, size_t required_size) return false; } + LOG_DBG("resized OSC buffer: %zu", new_size); term->vt.osc.data = new_data; term->vt.osc.size = new_size; return true; |
