chiark / gitweb /
basic: log: Increase static buffer for source file location (#3674)
authorDaniel Mack <github@zonque.org>
Thu, 7 Jul 2016 04:30:34 +0000 (06:30 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 16 Jun 2017 08:13:01 +0000 (10:13 +0200)
Commit d054f0a4 ("tree-wide: use xsprintf() where applicable") used a
semantic patch approach to change a number of locations from

  snprintf(buf, sizeof(buf), FMT, ...)

to

  xsprintf(buf, FMT, ...)

The problem is that xsprintf() wraps the snprintf() in an
assert_message_se(), so if snprintf() reports an overflow of the
destination buffer, the binary will now terminate.

This hit a user running a version of elogind that was built from a
deeply nested system path.

Fix this by

a) Switching back to snprintf() for this particular case. We should really
rather truncate the location string than crash in such situations.

b) Increasing the size of that static string buffer, to make the event more
unlikely.

src/basic/log.c

index 1b8374cfdc10eec9a745da45d1aca50a43eb4fc9..da6aa409b5b99b7ebef64f2420627db857fe9b1a 100644 (file)
@@ -346,7 +346,7 @@ static int write_to_console(
                 const char *object,
                 const char *buffer) {
 
-        char location[64], prefix[1 + DECIMAL_STR_MAX(int) + 2];
+        char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2];
         struct iovec iovec[6] = {};
         unsigned n = 0;
         bool highlight;
@@ -362,7 +362,7 @@ static int write_to_console(
         highlight = LOG_PRI(level) <= LOG_ERR && show_color;
 
         if (show_location) {
-                xsprintf(location, "(%s:%i) ", file, line);
+                snprintf(location, sizeof(location), "(%s:%i) ", file, line);
                 IOVEC_SET_STRING(iovec[n++], location);
         }