From b6ecedbe2f92df7a019dce00ceacfc320b5422a7 Mon Sep 17 00:00:00 2001 From: Rodrigo Osorio Date: Fri, 27 Nov 2015 22:54:29 +0100 Subject: [PATCH] Fix json buffer reallocation issue Instead of try to guess if we will need more space for future inserions, wait for exhaustion and allocate twice the required space. This is far from perfect but works better. Now Json output works all the time. --- dump1090.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/dump1090.c b/dump1090.c index 643ae89..5088040 100644 --- a/dump1090.c +++ b/dump1090.c @@ -2175,13 +2175,18 @@ char *aircraftsToJson(int *len) { a->reg?a->reg->code:"", a->flight, a->lat, a->lon, a->altitude, a->track, a->speed); - p += l; buflen -= l; + /* Resize if needed. */ - if (buflen < 256) { - int used = p-buf; - buflen += 1024; /* Our increment. */ - buf = realloc(buf,used+buflen); - p = buf+used; + if (l >= buflen) { + int used = p-buf; + buflen += (2*l); /* Our increment. */ + buf = realloc(buf,used+buflen); + if (buf == NULL) exit(1); + p = buf+used; + /* try to add the current airplane again */ + continue; + } else { + p += l; buflen -= l; } } a = a->next;