Fix possible missing \n termination of TSV messages

If the \n termination of TSV messages is missing, faup1090 encounters
the following error:
Caught background error: list must have an even number of elements
    while executing
"array set row [split $line "\t"]"
    (object "::faup1090" method "::FaupConnection::data_available" body line 28)

This missing termination with \n can happen when p == end is true in the
overrun check. vsnprintf will always terminate the string with \0, so if
the vsnprintf is called to write the to the last position in the buffer
(end-1), it will write \0 instead to terminate the string as not to
overflow the buffer.
To catch that possibility the overrun check is changed to p < end as
then vsnprintf is assured to have had enough room to write the \0
termination to end-1.
This commit is contained in:
Matthias Wirth 2019-04-08 20:29:56 +02:00
parent b281ceee7b
commit 82c5ff6724

View file

@ -1550,7 +1550,7 @@ char *generateStatsJson(const char *url_path, int *len) {
p = appendStatsJson(p, end, &add, "total");
p = safe_snprintf(p, end, "\n}\n");
assert(p <= end);
assert(p < end);
*len = p-buf;
return buf;
@ -1908,7 +1908,7 @@ static void writeFATSVPositionUpdate(float lat, float lon, float alt)
--p; // remove last tab
p = safe_snprintf(p, end, "\n");
if (p <= end)
if (p < end)
completeWrite(&Modes.fatsv_out, p);
else
fprintf(stderr, "fatsv: output too large (max %d, overran by %d)\n", TSV_MAX_PACKET_SIZE, (int) (p - end));
@ -1935,7 +1935,7 @@ static void writeFATSVEventMessage(struct modesMessage *mm, const char *datafiel
}
p = safe_snprintf(p, end, "\n");
if (p <= end)
if (p < end)
completeWrite(&Modes.fatsv_out, p);
else
fprintf(stderr, "fatsv: output too large (max %d, overran by %d)\n", TSV_MAX_PACKET_SIZE, (int) (p - end));
@ -2256,7 +2256,7 @@ static void writeFATSV()
--p; // remove last tab
p = safe_snprintf(p, end, "\n");
if (p <= end)
if (p < end)
completeWrite(&Modes.fatsv_out, p);
else
fprintf(stderr, "fatsv: output too large (max %d, overran by %d)\n", TSV_MAX_PACKET_SIZE, (int) (p - end));