confile: improve strprint()

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: 's avatarChristian Brauner <christian.brauner@ubuntu.com> Reported-by: 's avatarDonghwa Jeong <dh48.jeong@samsung.com>
parent 9715e65c
...@@ -43,7 +43,10 @@ ...@@ -43,7 +43,10 @@
#define strprint(str, inlen, ...) \ #define strprint(str, inlen, ...) \
do { \ do { \
if (str) \
len = snprintf(str, inlen, ##__VA_ARGS__); \ len = snprintf(str, inlen, ##__VA_ARGS__); \
else \
len = snprintf((char *){""}, 0, ##__VA_ARGS__); \
if (len < 0) { \ if (len < 0) { \
SYSERROR("failed to create string"); \ SYSERROR("failed to create string"); \
return -1; \ return -1; \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment