Jira (PUP-8728) A parameter that does not pass type validation can cause catalog compilation failure if used in the default expression of another parameter

0 vue
Accéder directement au premier message non lu

Nick Walker (JIRA)

non lue,
14 mai 2018, 18:33:0314/05/2018
à puppe...@googlegroups.com

Nick Walker (JIRA)

non lue,
14 mai 2018, 18:33:0414/05/2018
à puppe...@googlegroups.com
Nick Walker created an issue
 
Puppet / Bug PUP-8728
Issue Type: Bug Bug
Assignee: Unassigned
Created: 2018/05/14 3:32 PM
Priority: Normal Normal
Reporter: Nick Walker

The Problem

Given the below code I would expect to get an error message back indicating that the test parameter didn't pass type validation but it instead causes failure in the function used in the parameter below.

    function puppet_enterprise::calculate_log_temp_files(
      Variant[String, Integer] $work_mem,
    ) >> Integer {
      #work_mem is specified in kB if no memory unit is provided
      if $work_mem =~ /\A(\d+)(kB|MB|GB|TB)\Z/ {
    
        $wm_size = $work_mem.pe_size()
    
        $work_mem_integer = $work_mem[0, $wm_size - 2]
    
        $work_mem_suffix = $work_mem[$wm_size-2, $wm_size]
    
        $multiplier = $work_mem_suffix ? {
          'kb' => 1,
          'MB' => 1024,
          'GB' => 1024*1024,
          'TB' => 1024*1024*1024,
        }
    
        $log_temp_files = Integer($work_mem_integer * $multiplier)
      } else {
        $log_temp_files = Integer($work_mem)
      }
    }
    
    
    class my_class (
      Integer[64] $test,
      Integer     $blah = puppet_enterprise::calculate_log_temp_files($test),
    ) {
      #do things
    }
    
    class { 'my_class' :
      test => 63,
    }

Causes a failure like:

    [root@master201811-centos ~]# puppet apply /tmp/test.pp
    Error: Evaluation Error: Left match operand must result in a String value. Got an Integer. (file: /tmp/test.pp, line: 5, column: 6) on node master201811-centos.puppetdebug.vlan

Expected Behavior

If I fix the function so it doesn't fail then I get something like this which is the same thing I would expect since I don't expect the function to be evaluated if the first parameter doesn't pass type validation.

    Error: Evaluation Error: Error while evaluating a Resource Statement, Class[My_class]:
      parameter 'test' expects an Integer[64] value, got Integer[63, 63]

Henrik Lindberg (JIRA)

non lue,
15 mai 2018, 08:27:0215/05/2018
à puppe...@googlegroups.com
Henrik Lindberg commented on Bug PUP-8728
 
Re: A parameter that does not pass type validation can cause catalog compilation failure if used in the default expression of another parameter

It would be nice if arguments were validated before they get used in a default value expression. Ping Thomas Hallgren - do you think this is difficult to achieve? For puppet functions it should work since they are single dispatch, but I imagine this being very difficult for multiple dispatch 4.x functions (where on the other hand, you would need to do the default value evaluation inside the body of a function method.

Thomas Hallgren (JIRA)

non lue,
15 mai 2018, 09:11:0215/05/2018
à puppe...@googlegroups.com

I would have assumed that this already happened but apparently all parameter values are first calculated and then type checked. That's clearly a bug. $test should never be able to hold the value of 63. The fix is probably trivial. The code needs to check each individual parameter instead of checking the full Tuple at the end (probably means that APL assignments must be checked individually as well).

Henrik Lindberg (JIRA)

non lue,
15 mai 2018, 10:35:0315/05/2018
à puppe...@googlegroups.com

Thomas Hallgren Any thoughts on multiple dispatch? What if we introduced that in the puppet language? How would that work, say you have a first parameter Integer[10] $x, String $y = foo($x), and a second Integer[20] $x, Integer $y = foo($y) - here you cannot assert $x until you have established that it is the second signature that is going to be used (because the default expression returned an Integer). We would get around that problem by specifying that values returned by default value expressions are not involved in the selection of signature - which I think is quite reasonable.

Henrik Lindberg (JIRA)

non lue,
15 mai 2018, 10:36:0315/05/2018
à puppe...@googlegroups.com

Henrik Lindberg (JIRA)

non lue,
15 mai 2018, 10:37:0415/05/2018
à puppe...@googlegroups.com
 
Re: A parameter that does not pass type validation can cause catalog compilation failure if used in the default expression of another parameter

I targeted Puppet 6.0.0 for this since there is a small chance that it may break someone's code.

Henrik Lindberg (JIRA)

non lue,
15 mai 2018, 10:37:0415/05/2018
à puppe...@googlegroups.com

Kenn Hussey (JIRA)

non lue,
21 juin 2018, 12:48:0421/06/2018
à puppe...@googlegroups.com

Rob Braden (JIRA)

non lue,
8 août 2018, 16:54:0208/08/2018
à puppe...@googlegroups.com

Rob Braden (JIRA)

non lue,
8 août 2018, 16:55:0308/08/2018
à puppe...@googlegroups.com

Kenn Hussey (JIRA)

non lue,
10 sept. 2018, 09:30:0810/09/2018
à puppe...@googlegroups.com

Henrik Lindberg (JIRA)

non lue,
13 sept. 2018, 01:55:0313/09/2018
à puppe...@googlegroups.com

Kenn Hussey (JIRA)

non lue,
13 sept. 2018, 09:47:0413/09/2018
à puppe...@googlegroups.com

Henrik Lindberg (JIRA)

non lue,
13 sept. 2018, 11:28:0313/09/2018
à puppe...@googlegroups.com

Kris Bosland (JIRA)

non lue,
21 sept. 2018, 14:06:0421/09/2018
à puppe...@googlegroups.com
Kris Bosland commented on Bug PUP-8728
 
Re: A parameter that does not pass type validation can cause catalog compilation failure if used in the default expression of another parameter

This test case is a little simplified with some explanation comments:

function check_for_num_or_nan(
  Variant[String, Integer] $num,
) >> Integer {
  if $num =~ /NaN/ { #This fails if $num is an Integer
  #if $num =~ String { #This would pass if $num is an Integer
    1000000
  } else {
    $num
  }
}
 
class my_class (
  #Integer[64] means an integer in the range [64,64], so this must be 64.
  Integer[64] $test,
  #This parameter is assigned a default from the function.
  #This function is being called before the validity of the first argument is checked.
  Integer     $blah = check_for_num_or_nan($test),
) {
  #do things
}
 
class { 'my_class' :
  #This is not a valid value for the first my_class parameter
  test => 63,
}

David McTavish (Jira)

non lue,
12 janv. 2022, 19:57:0112/01/2022
à puppe...@googlegroups.com
David McTavish updated an issue
 
Change By: David McTavish
Labels: final_triage
This message was sent by Atlassian Jira (v8.20.2#820002-sha1:829506d)
Atlassian logo

Molly Waggett (Jira)

non lue,
22 févr. 2022, 13:50:0222/02/2022
à puppe...@googlegroups.com

Molly Waggett (Jira)

non lue,
22 févr. 2022, 13:50:0222/02/2022
à puppe...@googlegroups.com
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message