From 81a52ddc4d96194a2ff1c7997f07794cbd66e9ab Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Wed, 3 Oct 2018 01:10:30 +0200 Subject: [PATCH] gamemode: Add a tiny safe snprintf helper Give it a buffer, it returns an allocated string respecting the buffer size. Signed-off-by: Kai Krakow --- daemon/gamemode.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/daemon/gamemode.c b/daemon/gamemode.c index ca1c143..6ced936 100644 --- a/daemon/gamemode.c +++ b/daemon/gamemode.c @@ -65,6 +65,11 @@ POSSIBILITY OF SUCH DAMAGE. */ #define CLAMP(lbound, ubound, value) MIN(MIN(lbound, ubound), MAX(MAX(lbound, ubound), value)) +/* Little helper to safely print into a buffer, returns a newly allocated string + */ +#define safe_snprintf(b, s, ...) \ + (snprintf(b, sizeof(b), s, __VA_ARGS__) < (ssize_t)sizeof(b) ? strndup(b, sizeof(b)) : NULL) + /** * The GameModeClient encapsulates the remote connection, providing a list * form to contain the pid and credentials.