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.
This commit is contained in:
Rodrigo Osorio 2015-11-27 22:54:29 +01:00
parent 5656777bda
commit b6ecedbe2f

View File

@ -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;