]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
confile: improve strprint()
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 30 May 2018 13:34:03 +0000 (15:34 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 10 Dec 2018 07:47:59 +0000 (08:47 +0100)
POSIX specifies [1]:
"If the value of n is zero on a call to snprintf(), nothing shall be written,
the number of bytes that would have been written had n been sufficiently large
excluding the terminating null shall be returned, and s may be a null pointer."

But in case there are any non-sane libcs out there that do actually dereference
the buffer when when 0 is passed as length to snprintf() let's give them a
dummy buffer.

[1]: The Open Group Base Specifications Issue 7, 2018 edition
     IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008)
     Copyright © 2001-2018 IEEE and The Open Group

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Reported-by: Donghwa Jeong <dh48.jeong@samsung.com>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/confile.c
src/lxc/confile_utils.h

index 00ea5a0697a5ee7c423ff440af43eb659a95e999..034a6bc317f5f16c6bd17b5aaac3cc35578732b1 100644 (file)
@@ -474,23 +474,6 @@ extern struct lxc_config_t *lxc_getconfig(const char *key)
        return NULL;
 }
 
-#define strprint(str, inlen, ...)                                              \
-       do {                                                                   \
-               len = snprintf(str, inlen, ##__VA_ARGS__);                     \
-               if (len < 0) {                                                 \
-                       SYSERROR("failed to create string");                   \
-                       return -1;                                             \
-               };                                                             \
-               fulllen += len;                                                \
-               if (inlen > 0) {                                               \
-                       if (str)                                               \
-                               str += len;                                    \
-                       inlen -= len;                                          \
-                       if (inlen < 0)                                         \
-                               inlen = 0;                                     \
-               }                                                              \
-       } while (0);
-
 static int set_config_string_item(char **conf_item, const char *value)
 {
        char *new_value;
index 53665b914724c9420d7e2ff49da8ff478b028931..d23d52e7dbd7e905d1c3112e1648c4bae3bccaea 100644 (file)
 #include <stdbool.h>
 
 #include "conf.h"
+#include "confile_utils.h"
+
+#define strprint(str, inlen, ...)                                       \
+       do {                                                            \
+               if (str)                                                \
+                       len = snprintf(str, inlen, ##__VA_ARGS__);      \
+               else                                                    \
+                       len = snprintf((char *){""}, 0, ##__VA_ARGS__); \
+               if (len < 0) {                                          \
+                       SYSERROR("failed to create string");            \
+                       return -1;                                      \
+               };                                                      \
+               fulllen += len;                                         \
+               if (inlen > 0) {                                        \
+                       if (str)                                        \
+                               str += len;                             \
+                       inlen -= len;                                   \
+                       if (inlen < 0)                                  \
+                               inlen = 0;                              \
+               }                                                       \
+       } while (0);
 
 extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
                        unsigned long *hostid, unsigned long *range);