diff options
| author | Mattias Andrée <maandree@kth.se> | 2016-05-02 01:02:41 +0200 |
|---|---|---|
| committer | Mattias Andrée <maandree@kth.se> | 2016-05-02 01:02:41 +0200 |
| commit | e82b5e111223c34deb1e200c9f7cc2ecff9efb01 (patch) | |
| tree | 2cc18210ab3ac5ee3dda40973092dfa53d935302 | |
| parent | Buffer was too small (diff) | |
| download | libzahl-e82b5e111223c34deb1e200c9f7cc2ecff9efb01.tar.gz libzahl-e82b5e111223c34deb1e200c9f7cc2ecff9efb01.tar.bz2 libzahl-e82b5e111223c34deb1e200c9f7cc2ecff9efb01.tar.xz | |
zstr: do not calculate the exact output size, calculate something easier
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
| -rw-r--r-- | src/zstr.c | 14 |
1 files changed, 5 insertions, 9 deletions
@@ -70,15 +70,11 @@ zstr(z_t a, char *b, size_t n) } if (!n) { - /* This is not the most efficient way to handle this. It should - * be faster to allocate buffers that sprintint_fix and - * sprintint_min print to, and then allocate `b` and copy the - * buffers in reverse order into `b`. However, that is an overly - * complicated solution. You probably already know the maximum - * length or do not care about performance. Another disadvantage - * with calculating the length before-hand, means that it is not - * possible to reallocate `b` if it is too small. */ - n = zstr_length(a, 10); + /* Calculate a value that is at least the number of + * digits required to store the string. The overshoot + * is not too signicant. */ + n = (20 * BITS_PER_CHAR / 64 + (BITS_PER_CHAR == 8)) * a->used; + /* Note, depends on a ≠ as ensure above. */ } if (unlikely(!b) && unlikely(!(b = libzahl_temp_allocation = malloc(n + 1)))) |
