From: Alessandro Ranellucci <aar@cpan.org>
Date: Wed, 25 Jun 2014 14:57:06 +0200
Subject: Fix compilation under 5.20 (untested). #2109

Bug-Debian: #757798
Bug: https://github.com/alexrj/Slic3r/issues/2109
Origin: commit:67bf99633e48f9c8a5863b88c2a03fddc1cc247f
---
 xs/Build.PL             |  2 +-
 xs/src/ClipperUtils.cpp |  2 +-
 xs/src/MultiPoint.cpp   |  4 ++--
 xs/xsp/TriangleMesh.xsp |  6 ++++--
 xs/xsp/my.map           | 17 +++++++++++------
 5 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/xs/Build.PL b/xs/Build.PL
index 6e17ffc..216a3ea 100644
--- a/xs/Build.PL
+++ b/xs/Build.PL
@@ -30,7 +30,7 @@ my $build = Module::Build::WithXSpp->new(
     build_requires => {qw(
         ExtUtils::ParseXS           3.18
         ExtUtils::Typemaps          1.00
-        ExtUtils::Typemaps::Default 1.03
+        ExtUtils::Typemaps::Default 1.05
         ExtUtils::XSpp              0.17
         Module::Build               0.3601
         Test::More                  0
diff --git a/xs/src/ClipperUtils.cpp b/xs/src/ClipperUtils.cpp
index 2989783..240cf3b 100644
--- a/xs/src/ClipperUtils.cpp
+++ b/xs/src/ClipperUtils.cpp
@@ -542,7 +542,7 @@ polynode_children_2_perl(const ClipperLib::PolyNode& node)
 {
     AV* av = newAV();
     const unsigned int len = node.ChildCount();
-    av_extend(av, len-1);
+    if (len > 0) av_extend(av, len-1);
     for (int i = 0; i < len; ++i) {
         av_store(av, i, polynode2perl(*node.Childs[i]));
     }
diff --git a/xs/src/MultiPoint.cpp b/xs/src/MultiPoint.cpp
index 47830ce..5da3cb4 100644
--- a/xs/src/MultiPoint.cpp
+++ b/xs/src/MultiPoint.cpp
@@ -139,7 +139,7 @@ SV*
 MultiPoint::to_AV() {
     const unsigned int num_points = this->points.size();
     AV* av = newAV();
-    av_extend(av, num_points-1);
+    if (num_points > 0) av_extend(av, num_points-1);
     for (unsigned int i = 0; i < num_points; i++) {
         av_store(av, i, perl_to_SV_ref(this->points[i]));
     }
@@ -150,7 +150,7 @@ SV*
 MultiPoint::to_SV_pureperl() const {
     const unsigned int num_points = this->points.size();
     AV* av = newAV();
-    av_extend(av, num_points-1);
+    if (num_points > 0) av_extend(av, num_points-1);
     for (unsigned int i = 0; i < num_points; i++) {
         av_store(av, i, this->points[i].to_SV_pureperl());
     }
diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp
index 3338d97..be40543 100644
--- a/xs/xsp/TriangleMesh.xsp
+++ b/xs/xsp/TriangleMesh.xsp
@@ -151,10 +151,12 @@ TriangleMesh::slice(z)
         mslicer.slice(z_f, &layers);
         
         AV* layers_av = newAV();
-        av_extend(layers_av, layers.size()-1);
+        size_t len = layers.size();
+        if (len > 0) av_extend(layers_av, len-1);
         for (unsigned int i = 0; i < layers.size(); i++) {
             AV* expolygons_av = newAV();
-            av_extend(expolygons_av, layers[i].size()-1);
+            len = layers[i].size();
+            if (len > 0) av_extend(expolygons_av, len-1);
             unsigned int j = 0;
             for (ExPolygons::iterator it = layers[i].begin(); it != layers[i].end(); ++it) {
                 av_store(expolygons_av, j++, perl_to_SV_clone_ref(*it));
diff --git a/xs/xsp/my.map b/xs/xsp/my.map
index e69ba04..994874d 100644
--- a/xs/xsp/my.map
+++ b/xs/xsp/my.map
@@ -235,7 +235,8 @@ T_ARRAYREF
 	AV* av = newAV();
 	$arg = newRV_noinc((SV*)av);
 	sv_2mortal($arg);
-	av_extend(av, $var.size()-1);
+	const unsigned int len = $var.size();
+	if (len > 0) av_extend(av, len-1);
 	int i = 0;
     for (${type}::const_iterator it = $var.begin(); it != $var.end(); ++it) {
         av_store(av, i++, perl_to_SV_clone_ref(*it));
@@ -246,7 +247,8 @@ T_ARRAYREF_PTR
     AV* av = newAV();
 	$arg = newRV_noinc((SV*)av);
 	sv_2mortal($arg);
-	av_extend(av, $var->size()-1);
+	const unsigned int len = $var->size();
+	if (len > 0) av_extend(av, len-1);
 	int i = 0;
     for (${ my $t = $type; $t =~ s/\*$//; \$t }::iterator it = $var->begin(); it != $var->end(); ++it) {
         av_store(av, i++, perl_to_SV_ref(*it));
@@ -256,7 +258,8 @@ T_PTR_ARRAYREF_PTR
     AV* av = newAV();
     $arg = newRV_noinc((SV*)av);
     sv_2mortal($arg);
-    av_extend(av, $var->size()-1);
+	const unsigned int len = $var->size();
+	if (len > 0) av_extend(av, len-1);
     int i = 0;
     for (${ my $t = $type; $t =~ s/\*$//; \$t }::iterator it = $var->begin(); it != $var->end(); ++it) {
         av_store(av, i++, perl_to_SV_ref(**it));
@@ -266,7 +269,8 @@ T_PTR_ARRAYREF
     AV* av = newAV();
 	$arg = newRV_noinc((SV*)av);
 	sv_2mortal($arg);
-	av_extend(av, $var.size()-1);
+	const unsigned int len = $var.size();
+	if (len > 0) av_extend(av, len-1);
 	int i = 0;
     for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) {
         av_store(av, i++, (*it)->to_SV());
@@ -275,8 +279,9 @@ T_PTR_ARRAYREF
 T_LAYER_HEIGHT_RANGES
     AV* av = newAV();
     $arg = newRV_noinc((SV*)av);
-	sv_2mortal($arg);
-    av_extend(av, $var.size() - 1);
+    sv_2mortal($arg);
+	const unsigned int len = $var.size();
+	if (len > 0) av_extend(av, len-1);
     // map is sorted, so we can just copy it in order
     int i = 0;
     for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) {
