fixed name_trunc() on NULL or empty strings - patch from andrew_b

This commit is contained in:
Enrico Weigelt, metux IT service 2009-02-01 20:26:27 +01:00
parent 5ee6f43796
commit 6c016bc781
2 changed files with 13 additions and 3 deletions

View File

@ -1,3 +1,8 @@
2009-02-01 Enrico Weigelt, metux ITS <weigelt@metux.de>
* src/util.c: fixed name_trunc() on NULL or empty parameters
(patch from andrew_b)
2009-01-31 Enrico Weigelt, metux ITS <weigelt@metux.de>, Patrick Winnertz <winnie@debian.org>, Slava Zanko <slavazanko@gmail.com>, Sergei Trofimovich <slyfox@inbox.ru>
* edit/editcmd.c, mhl/escape.h, mhl/string.h, mhl/types.h, src/Makefile.am,

View File

@ -236,16 +236,21 @@ name_trunc (const char *txt, int trunc_len)
int txt_len;
char *p;
if (!txt)
return NULL;
if (!*txt)
return txt;
if ((size_t) trunc_len > sizeof (x) - 1) {
trunc_len = sizeof (x) - 1;
}
txt_len = strlen (txt);
txt_len = (int) strlen (txt);
if (txt_len <= trunc_len) {
strcpy (x, txt);
} else {
int y = (trunc_len / 2) + (trunc_len % 2);
strncpy (x, txt, y);
strncpy (x + y, txt + txt_len - (trunc_len / 2), trunc_len / 2);
strncpy (x, txt, (size_t) y);
strncpy (x + y, txt + (size_t) (txt_len - (trunc_len / 2)), (size_t) trunc_len / 2);
x[y] = '~';
}
x[trunc_len] = 0;