683572
From 027471cf1095f75f273df40310e4647fe1e8a9df Mon Sep 17 00:00:00 2001
683572
From: Tony Cook <tony@develop-help.com>
683572
Date: Wed, 20 Mar 2019 16:47:49 +1100
683572
Subject: [PATCH] (perl #133913) limit numeric format results to INT_MAX
683572
MIME-Version: 1.0
683572
Content-Type: text/plain; charset=UTF-8
683572
Content-Transfer-Encoding: 8bit
683572
683572
The return value of v?snprintf() is int, and we pay attention to that
683572
return value, so limit the expected size of numeric formats to
683572
INT_MAX.
683572
683572
Signed-off-by: Petr Písař <ppisar@redhat.com>
683572
---
683572
 pod/perldiag.pod | 6 ++++++
683572
 sv.c             | 7 +++++++
683572
 t/op/sprintf2.t  | 7 +++++++
683572
 3 files changed, 20 insertions(+)
683572
683572
diff --git a/pod/perldiag.pod b/pod/perldiag.pod
683572
index 1037215d44..166d29b4bb 100644
683572
--- a/pod/perldiag.pod
683572
+++ b/pod/perldiag.pod
683572
@@ -4354,6 +4354,12 @@ the meantime, try using scientific notation (e.g. "1e6" instead of
683572
 a number.  This happens, for example with C<\o{}>, with no number between
683572
 the braces.
683572
 
683572
+=item Numeric format result too large
683572
+
683572
+(F) The length of the result of a numeric format supplied to sprintf()
683572
+or printf() would have been too large for the underlying C function to
683572
+report.  This limit is typically 2GB.
683572
+
683572
 =item Octal number > 037777777777 non-portable
683572
 
683572
 (W portable) The octal number you specified is larger than 2**32-1
683572
diff --git a/sv.c b/sv.c
683572
index 8fbca52eb2..8bc0af0c16 100644
683572
--- a/sv.c
683572
+++ b/sv.c
683572
@@ -13085,6 +13085,13 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
683572
 	    if (float_need < width)
683572
 		float_need = width;
683572
 
683572
+            if (float_need > INT_MAX) {
683572
+                /* snprintf() returns an int, and we use that return value,
683572
+                   so die horribly if the expected size is too large for int
683572
+                */
683572
+                Perl_croak(aTHX_ "Numeric format result too large");
683572
+            }
683572
+
683572
 	    if (PL_efloatsize <= float_need) {
683572
                 /* PL_efloatbuf should be at least 1 greater than
683572
                  * float_need to allow a trailing \0 to be returned by
683572
diff --git a/t/op/sprintf2.t b/t/op/sprintf2.t
683572
index 84259a4afd..5fee8efede 100644
683572
--- a/t/op/sprintf2.t
683572
+++ b/t/op/sprintf2.t
683572
@@ -1153,4 +1153,11 @@ foreach(
683572
     is sprintf("%.0f", $_), sprintf("%-.0f", $_), "special-case %.0f on $_";
683572
 }
683572
 
683572
+# large uvsize needed so the large width is parsed properly
683572
+# large sizesize needed so the STRLEN check doesn't
683572
+if ($Config{intsize} == 4 && $Config{uvsize} > 4 && $Config{sizesize} > 4) {
683572
+    eval { my $x = sprintf("%7000000000E", 0) };
683572
+    like($@, qr/^Numeric format result too large at /,
683572
+         "croak for very large numeric format results");
683572
+}
683572
 done_testing();
683572
-- 
683572
2.20.1
683572