basic/log: make sure header is printed correctly, add test
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 22 Feb 2018 22:55:14 +0000 (23:55 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 23 Feb 2018 10:13:42 +0000 (11:13 +0100)
commitf8e6f4aaedf8c96bf909e027631f4910ac9c4539
tree4c1cb498568699315ddbf522320fd6c476fae94b
parent4dd09c6a1fc70f6bb0155661ab0a980b7a507986
basic/log: make sure header is printed correctly, add test

If log_do_header() was called with overly long parameters, it'd generate
improper output. Essentially, it'd be truncated at random point, in particular
missing a newline at the end, so it'd run with the next field, usually MESSAGE=.

log_do_header is called with parameters from compiled code (file name, lien
nubmer, etc), so in practice this was unlikely to ever be a problem, but it is
possible. In particular, if systemd was compiled from sources in some deeply
nested directory (which happens for example in mock and other build roots), the
filename could be very long.

As a safety measure, let's truncate all parameters to 256 bytes. So we have
5 fields which are 256 bytes (plus the field name prefix), and a few other
fields with fixed width. This must always fit in the 2048 byte buffer.
I don't think there's much gain in calculating the required length precisely,
since it's a lot of fields and a few bytes allocated on the stack don't matter.
src/basic/log.c
src/test/test-log.c