gisle / tcl.pm

Tcl extension module for Perl
http://search.cpan.org/dist/Tcl
9 stars 8 forks source link

Tcl.xs: handle undef SVs in TclObjFromSv() #35

Closed chrstphrchvz closed 3 years ago

chrstphrchvz commented 3 years ago

See chrstphrchvz/perl-tcl-ptk#17: assigning undef to a widget textvariable should behave the same as assigning an empty string, but unlike Perl/Tk a Use of uninitialized value in subroutine entry warning is emitted for programs with use warnings 'uninitialized'.

The issue is due to something affecting Tcl::Var, which is what widget textvariables are tied to. A more general example:

use warnings 'uninitialized';
use strict;

use Tcl;

my $i = new Tcl;
$i->Init;

my $t = ';l;;gmlxzssaw';
tie $t, 'Tcl::Var', $i, 'tvar';

$t = undef;

The STORE implementation for Tcl::Var in Tcl.xs uses TclObjFromSv() without first checking SvOK() (i.e. to not pass it an undef SV). TclObjFromSv() has several cases for possible types of SVs, but not one specifically for undef SVs, and the existing fallback case uses SvPV(sv, length) without first checking SvOK(sv), which causes the warning message to be emitted.

I would argue that this should be fixed by adding proper handling of undef SVs to TclObjFromSv(). Making it the first case checked could be ideal.