fix integer overflow check - json2tsv - JSON to TSV converter
 (HTM) git clone git://git.codemadness.org/json2tsv
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 30c28c0ba7a3bec23f3c964f59aef85a9c2ba615
 (DIR) parent 79e73bfa51005f9b760c01117f4decce4435fe29
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Sun, 13 Oct 2019 14:51:50 +0200
       
       fix integer overflow check
       
       Diffstat:
         M json2tsv.c                          |      16 ++++++++++------
       
       1 file changed, 10 insertions(+), 6 deletions(-)
       ---
 (DIR) diff --git a/json2tsv.c b/json2tsv.c
       @@ -81,17 +81,21 @@ capacity(char **value, size_t *sz, size_t cur, size_t inc)
                char *newp;
        
                /* check addition overflow */
       -        need = cur + inc;
       -        if (cur > SIZE_MAX - need || *sz > SIZE_MAX - 16384) {
       +        if (cur > SIZE_MAX - inc) {
                        errno = EOVERFLOW;
                        return -1;
                }
       +        need = cur + inc;
        
       -        if (*sz == 0 || need > *sz) {
       -                for (newsiz = *sz; newsiz < need; newsiz += 16384)
       -                        ;
       +        if (need > *sz) {
       +                if (need > SIZE_MAX - 16384) {
       +                        newsiz = SIZE_MAX;
       +                } else {
       +                        for (newsiz = *sz; newsiz < need; newsiz += 16384)
       +                                ;
       +                }
                        if (!(newp = realloc(*value, newsiz)))
       -                        return -1; /* up to caller to free memory */
       +                        return -1; /* up to caller to free *value */
                        *value = newp;
                        *sz = newsiz;
                }