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