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