[pve-devel] [PATCH guest-common 1/5] guesthelpers: move/add safe comparison functions from lxc and qemu

Oguz Bektas o.bektas at proxmox.com
Thu Feb 13 15:35:24 CET 2020


hi,

On Thu, Feb 13, 2020 at 11:16:31AM +0100, Stefan Reiter wrote:
> On 1/28/20 4:03 PM, Oguz Bektas wrote:
> > move the safe_string_ne and safe_num_ne functions to guesthelpers to
> > remove duplicate code.
> > 
> > add the new safe_boolean_ne and typesafe_ne helper functions
> > 
> > Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
> > ---
> > 
> > these will be used in the partial fast plug function in this series
> > 
> >   PVE/GuestHelpers.pm | 49 +++++++++++++++++++++++++++++++++++++++++++++
> >   1 file changed, 49 insertions(+)
> > 
> > diff --git a/PVE/GuestHelpers.pm b/PVE/GuestHelpers.pm
> > index 07a62ce..b7133d0 100644
> > --- a/PVE/GuestHelpers.pm
> > +++ b/PVE/GuestHelpers.pm
> > @@ -14,6 +14,55 @@ use Scalar::Util qw(weaken);
> >   our $lockdir = '/var/lock/pve-manager';
> > +# safe variable comparison functions
> > +
> > +sub safe_num_ne {
> > +    my ($a, $b) = @_;
> > +
> > +    return 0 if !defined($a) && !defined($b);
> > +    return 1 if !defined($a);
> > +    return 1 if !defined($b);
> > +
> > +    return $a != $b;
> > +}
> > +
> > +sub safe_string_ne  {
> > +    my ($a, $b) = @_;
> > +
> > +    return 0 if !defined($a) && !defined($b);
> > +    return 1 if !defined($a);
> > +    return 1 if !defined($b);
> > +
> > +    return $a ne $b;
> > +}
> > +
> > +sub safe_boolean_ne {
> > +    my ($a, $b) = @_;
> > +
> > +    # we don't check if value is defined, since undefined
> > +    # is false (so it's a valid boolean)
> > +
> > +    # negate both values to normalize and compare
> > +    return !$a != !$b;
> > +}
> > +
> > +sub typesafe_ne {
> > +    my ($a, $b, $type) = @_;
> > +
> > +    return 0 if !defined($a) && !defined($b);
> > +    return 1 if !defined($a);
> > +    return 1 if !defined($b);
> > +
> > +    if ($type eq 'string') {
> > +	return safe_string_ne($a, $b);
> > +    } elsif ($type eq 'number') {
> 
> This should include 'integer' too, since it's pretty much just a limited
> 'number'.

right, i'll add that
> 
> > +	return safe_num_ne($a, $b);
> > +    } elsif ($type eq 'boolean') {
> > +	return safe_boolean_ne($a, $b);
> > +    }
> 
> I'd add a 'die "internal error bla bla ..."' part just to be safe. Otherwise
> you just end the function without a return if $type is not matched, and I
> don't trust Perl to do the right thing™ in that case at all.

yeah, makes sense.

> 
> > +}
> > +
> > +
> >   sub guest_migration_lock {
> >       my ($vmid, $timeout, $func, @param) = @_;
> > 




More information about the pve-devel mailing list