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.
This commit is contained in:
Benedikt Heine 2018-10-03 15:18:08 +02:00
parent 2ef74c0d64
commit da846d8442

View File

@ -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;
}