From da846d8442df6446b5ae29edd8d5b7d9707e7590 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Wed, 3 Oct 2018 15:18:08 +0200 Subject: [PATCH] Do not count longer than necessary And also use g_strndup. This makes allocation easier and also ensures a null-byte at the end of the string. --- src/notification.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/notification.c b/src/notification.c index b36a817..eb57507 100644 --- a/src/notification.c +++ b/src/notification.c @@ -421,11 +421,8 @@ static void notification_format_message(struct notification *n) n->msg = g_strchomp(n->msg); /* truncate overlong messages */ - if (strlen(n->msg) > DUNST_NOTIF_MAX_CHARS) { - char *buffer = g_malloc(DUNST_NOTIF_MAX_CHARS); - strncpy(buffer, n->msg, DUNST_NOTIF_MAX_CHARS); - buffer[DUNST_NOTIF_MAX_CHARS-1] = '\0'; - + if (strnlen(n->msg, DUNST_NOTIF_MAX_CHARS + 1) > DUNST_NOTIF_MAX_CHARS) { + char * buffer = g_strndup(n->msg, DUNST_NOTIF_MAX_CHARS); g_free(n->msg); n->msg = buffer; }