Skip to content
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

Use of uninitialized value $this in <HANDLE> #17435

Open
toddr opened this issue May 31, 2018 · 3 comments
Open

Use of uninitialized value $this in <HANDLE> #17435

toddr opened this issue May 31, 2018 · 3 comments
Assignees
Labels
dist-IO issues in the dual-life blead-first IO distribution

Comments

@toddr
Copy link
Member

toddr commented May 31, 2018

Transferred from RT - https://rt.cpan.org/Ticket/Display.html?id=125436

IO::File started throwing warnings on getline(), if there wasn't writing to file before.

$ perl5.18.2 -Mwarnings -MIO::File -e 'my $h; my $f=IO::File->new(\$h,"+>:"); $f->getline();'
$ 
$ perl5.24.3 -Mwarnings -MIO::File -e 'my $h; my $f=IO::File->new(\$h,"+>:"); $f->getline();'
Use of uninitialized value $this in <HANDLE> at -e line 1.
Use of uninitialized value $this in <HANDLE> at -e line 1.
Use of uninitialized value $this in <HANDLE> at -e line 1.
Use of uninitialized value $this in <HANDLE> at -e line 1.
Use of uninitialized value $this in <HANDLE> at -e line 1.
Use of uninitialized value $this in <HANDLE> at -e line 1.
Use of uninitialized value $this in <HANDLE> at -e line 1.
$ 
$ perl5.24.3 -Mwarnings -MIO::File -e 'my $h; my $f=IO::File->new(\$h,"+>:"); $f->write("a"); $f->getline();'
$

Commenting this line fixing the warning https://metacpan.org/source/TODDR/IO-1.39/lib/IO/Handle.pm#L435

@themage
Copy link

themage commented May 31, 2018

After a bit of time trying to reproduce it, I think in the original report there is a backslash () missing in IO::File->new:

$ perl5.24.3 -Mwarnings -MIO::File -e 'my $h; my $f=IO::File->new(\$h,"+>:"); $f->write("a"); $f->getline();'

@toddr
Copy link
Member Author

toddr commented Jun 1, 2018

markdown magic. I added quoting and now it reads right.

@toddr toddr transferred this issue from Dual-Life/IO Jan 20, 2020
@toddr toddr added Needs Triage dist-IO issues in the dual-life blead-first IO distribution labels Jan 20, 2020
@tonycoz
Copy link
Contributor

tonycoz commented May 7, 2020

I see warnings even in 5.18.2, just not as many:

$ ~/perl/5.18.2/bin/perl -Mwarnings -MIO::File -e 'my $h; open my $f, "+>", \$h; $f->getline();'
Use of uninitialized value $this in <HANDLE> at -e line 1.
Use of uninitialized value $this in <HANDLE> at -e line 1.
Use of uninitialized value $this in <HANDLE> at -e line 1.

It doesn't happen in 5.14:

$ ~/perl/5.14.2-debug/bin/perl -Mwarnings -MIO::File -e 'my $h; open my $f, "+>", \$h; $f->getline();'

I expect this was introduced in 986a805 which made getline() act as if it was in the caller's scope, and the re-work for that in 986a805 hasn't changed that.

The warnings are generated when the various hooks PerlIO::scalar are called by the implementation of sv_gets() (called by do_readline(), called by pp_readline), those hooks have no way to tell when that readline operation starts and ends so we can't really limit the number of warnings produced to one per readline().

The warning could be considered reasonable, but I think it's way over punishing to report it many times per operation and reporting it on a readline() (aka getline()) could be confusing, since the handle might be passed in from some other code.

So I think PerlIO::scalar should be modified to prevent those warnings.

It might be reasonable to have warning on open, but the PerlIOScalar_pushed() handler carefully avoids warning on an undef SV and it probably not worthwhile changing that.

One other issue from reviewing the code while checking out the warning sources - the various functions PerlIOScalar_get_base(), PerlIOScalar_get_ptr(), PerlIOScalar_get_cnt), PerlIOScalar_set_ptrcnt() all directly or indirectly call get magic, if the underlying magic ends up returning different SVs those values will end up being inconsistent at best.

PerlIOScalar_read() avoids this for the read operation, but that doesn't help for sv_gets() or anything else that peeks at the buffer.

Unfortunately I don't see a way to make that safe in the general case.

@tonycoz tonycoz self-assigned this Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dist-IO issues in the dual-life blead-first IO distribution
Projects
None yet
Development

No branches or pull requests

4 participants