From 2b301921ff7682e54ab74ad30dbf2ce1c9fc24b1 Mon Sep 17 00:00:00 2001 From: Yves Orton Date: Fri, 31 Jan 2020 15:34:48 +0100 Subject: pp_sort.c: fix fencepost error in call to av_extend() In [rt.cpan.org #39196] issue #17496 there is a report that Tie::File produced spurious blank lines in the file after @tied= sort @tied; it turns out that this is because Tie::File treats EXTEND similarly to STORESIZE (which is arguably not entirely correct, but also not that weird) coupled with an off by one error in the calls to av_extend() in pp_sort. This patch fixes the fencepost error, adds some comments to av_extend() to make it clear what it is doing, and adds a test that EXTEND is called by this code with correct argument. --- pp_sort.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'pp_sort.c') diff --git a/pp_sort.c b/pp_sort.c index 0c5efb0869..4f81aaab7e 100644 --- a/pp_sort.c +++ b/pp_sort.c @@ -1067,7 +1067,8 @@ PP(pp_sort) for (i = 0; i < max; i++) base[i] = newSVsv(base[i]); av_clear(av); - av_extend(av, max); + if (max) + av_extend(av, max-1); for (i=0; i < max; i++) { SV * const sv = base[i]; SV ** const didstore = av_store(av, i, sv); @@ -1094,7 +1095,7 @@ PP(pp_sort) } av_clear(av); if (max > 0) { - av_extend(av, max); + av_extend(av, max-1); Copy(base, AvARRAY(av), max, SV*); } AvFILLp(av) = max - 1; -- cgit v1.2.1