util: Fix leak in virStringTrimOptionalNewline

Do not access any data if strlen() == 0.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Martin Kletzander 2017-11-22 21:52:17 +01:00
parent 9baf50c414
commit ea7b2a6d04
1 changed files with 7 additions and 3 deletions

View File

@ -1394,9 +1394,13 @@ virStringEncodeBase64(const uint8_t *buf, size_t buflen)
*/
void virStringTrimOptionalNewline(char *str)
{
char *tmp = str + strlen(str) - 1;
if (*tmp == '\n')
*tmp = '\0';
size_t len = strlen(str);
if (!len)
return;
if (str[len - 1] == '\n')
str[len - 1] = '\0';
}