Skip to content

Commit

Permalink
[perl #127877] Emit undef warning on sassign+concat
Browse files Browse the repository at this point in the history
Code like this:

    my $x;
    $x .= 'a';

is specifically exempted from "use of uninitialized value" warnings,
according to the "Declarations" section of perlsyn, to allow the idiom of
building up a value piecemeal. The same is true of the += and -= operators.
However, breaking the combined assignment up into the underlying operator
and a simple assignment, as in this code:

    my $x;
    $x = $x . 'a';

*should* produce a warning.

That warning was correctly being emitted for addition and subtraction, but
concatenation was behaving as the ".=" case, because "$x = $x . EXPR" is
optimized to the equivalent of "$x .= EXPR".

So we now explicitly detect this case, and emit the desired warning.
  • Loading branch information
arc committed May 9, 2016
1 parent 0f51bd1 commit 51f69a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pp_hot.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,11 @@ PP(pp_concat)
}
else { /* $l .= $r and left == TARG */
if (!SvOK(left)) {
if (left == right && ckWARN(WARN_UNINITIALIZED)) /* $l .= $l */
report_uninit(right);
if ((left == right /* $l .= $l */
|| (PL_op->op_private & OPpTARGET_MY)) /* $l = $l . $r */
&& ckWARN(WARN_UNINITIALIZED)
)
report_uninit(left);
sv_setpvs(left, "");
}
else {
Expand Down
21 changes: 21 additions & 0 deletions t/lib/warnings/9uninit
Original file line number Diff line number Diff line change
Expand Up @@ -2138,3 +2138,24 @@ Use of uninitialized value $i in array element at - line 12.
Use of uninitialized value $k in hash element at - line 12.
Use of uninitialized value $i in array element at - line 13.
Use of uninitialized value $k in hash element at - line 13.
########
# perl #127877
use warnings 'uninitialized';
my ($p, $q, $r, $s, $t, $u, $v, $w, $x, $y);
$p = $p . "a";
$q .= "a";
$r = $r + 17;
$s += 17;
$t = $t - 17;
$u -= 17;
use integer;
$v = $v + 17;
$w += 17;
$x = $x - 17;
$y -= 17;
EXPECT
Use of uninitialized value $p in concatenation (.) or string at - line 4.
Use of uninitialized value $r in addition (+) at - line 6.
Use of uninitialized value $t in subtraction (-) at - line 8.
Use of uninitialized value $v in integer addition (+) at - line 11.
Use of uninitialized value $x in integer subtraction (-) at - line 13.

0 comments on commit 51f69a2

Please sign in to comment.