# [PATCH] test-stringlist: avoid buffer underrun when sorting nothing

2 messages from 2017-10-03 to 2017-10-03. Participants: René Scharfe, Jonathan Nieder.
Thread: https://gitlist.dev/t/46890

## René Scharfe, 2017-10-03 14:36

Subject: [PATCH] test-stringlist: avoid buffer underrun when sorting nothing
Message-ID: <16d3a84c-b695-a55d-0263-885d0d34ae95@web.de>
URL: https://gitlist.dev/e/16d3a84c-b695-a55d-0263-885d0d34ae95%40web.de

```
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>
---
 t/helper/test-string-list.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/t/helper/test-string-list.c b/t/helper/test-string-list.c
index c502fa16d3..829ec3d7d2 100644
--- a/t/helper/test-string-list.c
+++ b/t/helper/test-string-list.c
@@ -108,7 +108,7 @@ int cmd_main(int argc, const char **argv)
 		 * Split by newline, but don't create a string_list item
 		 * 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);
 		string_list_split_in_place(&list, sb.buf, '\n', -1);
 
-- 
2.14.2

```

## Jonathan Nieder, 2017-10-03 22:59

Subject: Re: [PATCH] test-stringlist: avoid buffer underrun when sorting nothing
Message-ID: <20171003225919.GF19555@aiede.mtv.corp.google.com>
URL: https://gitlist.dev/e/20171003225919.GF19555%40aiede.mtv.corp.google.com
In-Reply-To: <16d3a84c-b695-a55d-0263-885d0d34ae95@web.de>

```
René Scharfe wrote:

> 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>
> ---
>  t/helper/test-string-list.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Thanks for fixing it.
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

```
