New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PATCH] Coverity: pp_sys.c: uninitialized tmbuf #13758
Comments
From @jhiAttached. |
From @jhi0006-Fix-for-Coverity-perl5-CID-29088.patchFrom 978fb9a9aad85383b60c7a28506bab33941165f7 Mon Sep 17 00:00:00 2001
From: Jarkko Hietaniemi <jhi@iki.fi>
Date: Mon, 21 Apr 2014 17:53:13 -0400
Subject: [PATCH 6/9] Fix for Coverity perl5 CID 29088: Uninitialized scalar
variable (UNINIT) uninit_use: Using uninitialized value tmbuf.tm_year.
There is a code path that can lead to accessing uninitialized tmbuf:
when the too-small or too-large time inputs to gmtime/localtime
happen.
- make it so that the tm_year is used only on successful code path:
pp_sys.c
- add the gmtime failed / localtime failed errors to perldiag:
pod/perldiag.pod
- test those errors: t/op/time.t
---
pod/perldiag.pod | 10 ++++++++++
pp_sys.c | 27 +++++++++++++--------------
t/op/time.t | 9 ++++++---
3 files changed, 29 insertions(+), 17 deletions(-)
diff --git a/pod/perldiag.pod b/pod/perldiag.pod
index 00700c5..64d87a2 100644
--- a/pod/perldiag.pod
+++ b/pod/perldiag.pod
@@ -2128,6 +2128,11 @@ a term, so it's looking for the corresponding right angle bracket, and
not finding it. Chances are you left some needed parentheses out
earlier in the line, and you really meant a "less than".
+=item gmtime(%f) failed
+
+(W overflow) You called C<gmtime> with a number that it could not handle:
+too large, too small, or NaN. The returned value is C<undef>.
+
=item gmtime(%f) too large
(W overflow) You called C<gmtime> with a number that was larger than
@@ -2787,6 +2792,11 @@ L<perlfunc/listen>.
form of C<open> does not support pipes, such as C<open($pipe, '|-', @args)>.
Use the two-argument C<open($pipe, '|prog arg1 arg2...')> form instead.
+=item localtime(%f) failed
+
+(W overflow) You called C<localtime> with a number that it could not handle:
+too large, too small, or NaN. The returned value is C<undef>.
+
=item localtime(%f) too large
(W overflow) You called C<localtime> with a number that was larger
diff --git a/pp_sys.c b/pp_sys.c
index 9f97177..43d6473 100644
--- a/pp_sys.c
+++ b/pp_sys.c
@@ -4483,30 +4483,29 @@ PP(pp_gmtime)
}
if (err == NULL) {
+ /* diag_listed_as: gmtime(%f) failed */
/* XXX %lld broken for quads */
Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
"%s(%.0" NVff ") failed", opname, when);
}
if (GIMME != G_ARRAY) { /* scalar context */
- SV *tsv;
- /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
- double year = (double)tmbuf.tm_year + 1900;
-
EXTEND(SP, 1);
EXTEND_MORTAL(1);
if (err == NULL)
RETPUSHUNDEF;
-
- tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
- dayname[tmbuf.tm_wday],
- monname[tmbuf.tm_mon],
- tmbuf.tm_mday,
- tmbuf.tm_hour,
- tmbuf.tm_min,
- tmbuf.tm_sec,
- year);
- mPUSHs(tsv);
+ else {
+ mPUSHs(Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
+ dayname[tmbuf.tm_wday],
+ monname[tmbuf.tm_mon],
+ tmbuf.tm_mday,
+ tmbuf.tm_hour,
+ tmbuf.tm_min,
+ tmbuf.tm_sec,
+ /* XXX newSVpvf()'s %lld type is broken,
+ * so cheat with a double */
+ (double)tmbuf.tm_year + 1900));
+ }
}
else { /* list context */
if ( err == NULL )
diff --git a/t/op/time.t b/t/op/time.t
index 4ac7f5b..734b838 100644
--- a/t/op/time.t
+++ b/t/op/time.t
@@ -6,7 +6,7 @@ BEGIN {
require './test.pl';
}
-plan tests => 66;
+plan tests => 70;
# These tests make sure, among other things, that we don't end up
# burning tons of CPU for dates far in the future.
@@ -210,7 +210,7 @@ SKIP: { #rt #73040
|| $small_time == $smallest
|| $big_time - 200 != $biggest
|| $big_time == $biggest) {
- skip "Can't represent test values", 4;
+ skip "Can't represent test values", 8;
}
my $small_time_f = sprintf("%.0f", $small_time);
my $big_time_f = sprintf("%.0f", $big_time);
@@ -221,17 +221,20 @@ SKIP: { #rt #73040
$warning = '';
my $date = gmtime($big_time);
like $warning, qr/^gmtime\($big_time_f\) too large/;
+ like $warning, qr/^gmtime\($big_time_f\) failed/m;
$warning = '';
$date = localtime($big_time);
like $warning, qr/^localtime\($big_time_f\) too large/;
+ like $warning, qr/^localtime\($big_time_f\) failed/m;
$warning = '';
$date = gmtime($small_time);
like $warning, qr/^gmtime\($small_time_f\) too small/;
+ like $warning, qr/^gmtime\($small_time_f\) failed/m;
$warning = '';
$date = localtime($small_time);
like $warning, qr/^localtime\($small_time_f\) too small/;
-
+ like $warning, qr/^localtime\($small_time_f\) failed/m;
}
--
1.8.5.2 (Apple Git-48)
|
From @tonycozOn Tue Apr 22 17:29:56 2014, jhi wrote:
Added as a 5.21.1 blocker. The extra $warning checks confused me until I saw we were generating two warnings for those calls to (local|gm)time Tony |
The RT System itself - Status changed from 'new' to 'open' |
From @jhiOn Tuesday-201404-22, 20:29, perlbug-followup@perl.org wrote:
Better summary line, patch attached. |
From @jhi0001-Uninitialized-tmbuf.patchFrom 5500c3cf6201ccb9b4fe380e24f811f5cc018708 Mon Sep 17 00:00:00 2001
From: Jarkko Hietaniemi <jhi@iki.fi>
Date: Wed, 7 May 2014 09:26:52 -0400
Subject: [PATCH] Uninitialized tmbuf.
Fix for Coverity perl5 CID 29088: Uninitialized scalar variable (UNINIT)
uninit_use: Using uninitialized value tmbuf.tm_year.
There is a code path that can lead to accessing uninitialized tmbuf:
when the too-small or too-large time inputs to gmtime/localtime
happen.
- make it so that the tm_year is used only on successful code path:
pp_sys.c
- add the gmtime failed / localtime failed errors to perldiag:
pod/perldiag.pod
- test those errors: t/op/time.t
---
pod/perldiag.pod | 10 ++++++++++
pp_sys.c | 27 +++++++++++++--------------
t/op/time.t | 9 ++++++---
3 files changed, 29 insertions(+), 17 deletions(-)
diff --git a/pod/perldiag.pod b/pod/perldiag.pod
index f87ca9c..530bc47 100644
--- a/pod/perldiag.pod
+++ b/pod/perldiag.pod
@@ -2121,6 +2121,11 @@ a term, so it's looking for the corresponding right angle bracket, and
not finding it. Chances are you left some needed parentheses out
earlier in the line, and you really meant a "less than".
+=item gmtime(%f) failed
+
+(W overflow) You called C<gmtime> with a number that it could not handle:
+too large, too small, or NaN. The returned value is C<undef>.
+
=item gmtime(%f) too large
(W overflow) You called C<gmtime> with a number that was larger than
@@ -2780,6 +2785,11 @@ L<perlfunc/listen>.
form of C<open> does not support pipes, such as C<open($pipe, '|-', @args)>.
Use the two-argument C<open($pipe, '|prog arg1 arg2...')> form instead.
+=item localtime(%f) failed
+
+(W overflow) You called C<localtime> with a number that it could not handle:
+too large, too small, or NaN. The returned value is C<undef>.
+
=item localtime(%f) too large
(W overflow) You called C<localtime> with a number that was larger
diff --git a/pp_sys.c b/pp_sys.c
index 9f97177..43d6473 100644
--- a/pp_sys.c
+++ b/pp_sys.c
@@ -4483,30 +4483,29 @@ PP(pp_gmtime)
}
if (err == NULL) {
+ /* diag_listed_as: gmtime(%f) failed */
/* XXX %lld broken for quads */
Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
"%s(%.0" NVff ") failed", opname, when);
}
if (GIMME != G_ARRAY) { /* scalar context */
- SV *tsv;
- /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
- double year = (double)tmbuf.tm_year + 1900;
-
EXTEND(SP, 1);
EXTEND_MORTAL(1);
if (err == NULL)
RETPUSHUNDEF;
-
- tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
- dayname[tmbuf.tm_wday],
- monname[tmbuf.tm_mon],
- tmbuf.tm_mday,
- tmbuf.tm_hour,
- tmbuf.tm_min,
- tmbuf.tm_sec,
- year);
- mPUSHs(tsv);
+ else {
+ mPUSHs(Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
+ dayname[tmbuf.tm_wday],
+ monname[tmbuf.tm_mon],
+ tmbuf.tm_mday,
+ tmbuf.tm_hour,
+ tmbuf.tm_min,
+ tmbuf.tm_sec,
+ /* XXX newSVpvf()'s %lld type is broken,
+ * so cheat with a double */
+ (double)tmbuf.tm_year + 1900));
+ }
}
else { /* list context */
if ( err == NULL )
diff --git a/t/op/time.t b/t/op/time.t
index 4ac7f5b..734b838 100644
--- a/t/op/time.t
+++ b/t/op/time.t
@@ -6,7 +6,7 @@ BEGIN {
require './test.pl';
}
-plan tests => 66;
+plan tests => 70;
# These tests make sure, among other things, that we don't end up
# burning tons of CPU for dates far in the future.
@@ -210,7 +210,7 @@ SKIP: { #rt #73040
|| $small_time == $smallest
|| $big_time - 200 != $biggest
|| $big_time == $biggest) {
- skip "Can't represent test values", 4;
+ skip "Can't represent test values", 8;
}
my $small_time_f = sprintf("%.0f", $small_time);
my $big_time_f = sprintf("%.0f", $big_time);
@@ -221,17 +221,20 @@ SKIP: { #rt #73040
$warning = '';
my $date = gmtime($big_time);
like $warning, qr/^gmtime\($big_time_f\) too large/;
+ like $warning, qr/^gmtime\($big_time_f\) failed/m;
$warning = '';
$date = localtime($big_time);
like $warning, qr/^localtime\($big_time_f\) too large/;
+ like $warning, qr/^localtime\($big_time_f\) failed/m;
$warning = '';
$date = gmtime($small_time);
like $warning, qr/^gmtime\($small_time_f\) too small/;
+ like $warning, qr/^gmtime\($small_time_f\) failed/m;
$warning = '';
$date = localtime($small_time);
like $warning, qr/^localtime\($small_time_f\) too small/;
-
+ like $warning, qr/^localtime\($small_time_f\) failed/m;
}
--
1.9.2
|
From @bulk88On Wed May 07 06:55:52 2014, jhi wrote:
Why is this refactoring a bunch of lines unrelated to the uninit var access? the else block is redundant. By removing the tsv assignment it makes debugging harder. -- |
From @tonycozOn Sat May 10 18:49:54 2014, bulk88 wrote:
If you need to debug the return value you can either use a debugger that gives access to the function return value, or temporarily modify the code to save the value, as has been discussed in other threads. As to the re-org, while I can't read Jarkko's mind, it may be to move the declaration closer to the initialization, if we weren't restricted by certain older compilers that could be done in a simpler fashion. Tony |
From @jhiOn Monday-201405-12, 21:50, Tony Cook via RT wrote:
If I could read my own mind... I think it was something along the lines: (to recap what Coverity noticed: in the failure path the (*) I simply cannot type the word straight: unitial... unitializ... Darn |
From @tonycozOn Wed May 07 06:55:52 2014, jhi wrote:
Thanks, applied as b35b96b. Tony |
From @jkeenanNo failure reports to date; marking ticket Resolved. |
@jkeenan - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#121709 (status was 'resolved')
Searchable as RT121709$
The text was updated successfully, but these errors were encountered: