]> git.itanic.dy.fi Git - rrdd/commitdiff
network_parser: Sanitize buffer state between calls and between parsed lines
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 1 Mar 2023 16:09:35 +0000 (18:09 +0200)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 1 Mar 2023 16:09:35 +0000 (18:09 +0200)
Whenever we have finished parsing a line, the destination buffer must
be zeroed out to prevent stale data from corrupting the data.

Once data is copied from the receive buffer to the last_line buffer
and the receive buffer is moved back to the beginning, we must zero
out the old data at the end of the buffer to prevent corrupting any
new data. This can happen if we receive multiple lines in one recv()
call.

When we have finished handling one set of data, we should clear the
buffer state and data to prevent any stale data from corrupting the
data on next call. This could happen for example if the TCP transfer
is interrupted for any reason and we don't receive comlete line for
any reason.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
network_parser.c

index 89941b46dfa3a4cd08b63cf8afafb6b520a6b860..92c0d07f3d43bed43142a0b46e191ae17d63a0d1 100644 (file)
@@ -172,6 +172,7 @@ static int parse_line(struct network_parser_data *data)
        len++;
        memmove(data->buf, s, sizeof(data->buf) - len);
        data->idx -= len;
+       bzero(data->buf + data->idx, sizeof(data->buf) - data->idx);
 
        return 1;
 }
@@ -261,12 +262,16 @@ static int network_multi_parser(char ***rrd_data, const char **parser_data,
 
                strncpy(rdata[d], data->last_line, RRD_DATA_MAX_LEN);
                data->last_time = 0;
+               bzero(data->last_line, sizeof(data->last_line));
                mutex_unlock(&data->lock);
        }
 
        pr_info("Finished at line %d, idx: %d\n", d, data->idx);
 
        ret = d;
+       bzero(data->last_line, sizeof(data->last_line));
+       bzero(data->buf, sizeof(data->buf));
+       data->idx = 0;
 
 out_close:
        /*