test-stringlist: avoid buffer underrun when sorting nothing

Check if the strbuf containing data to sort is empty before attempting
to trim a trailing newline character.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2017-10-03 16:36:40 +02:00 committed by Junio C Hamano
parent 83fc4d64fe
commit 97487ea11a

View File

@ -108,7 +108,7 @@ int cmd_main(int argc, const char **argv)
* Split by newline, but don't create a string_list item * Split by newline, but don't create a string_list item
* for the empty string after the last separator. * for the empty string after the last separator.
*/ */
if (sb.buf[sb.len - 1] == '\n') if (sb.len && sb.buf[sb.len - 1] == '\n')
strbuf_setlen(&sb, sb.len - 1); strbuf_setlen(&sb, sb.len - 1);
string_list_split_in_place(&list, sb.buf, '\n', -1); string_list_split_in_place(&list, sb.buf, '\n', -1);