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

6 views
Skip to first unread message

Nick Walker (JIRA)

unread,
May 14, 2018, 6:33:03 PM5/14/18
to puppe...@googlegroups.com

Nick Walker (JIRA)

unread,
May 14, 2018, 6:33:04 PM5/14/18
to 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)

unread,
May 15, 2018, 8:27:02 AM5/15/18
to 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)

unread,
May 15, 2018, 9:11:02 AM5/15/18
to 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)

unread,
May 15, 2018, 10:35:03 AM5/15/18
to 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)

unread,
May 15, 2018, 10:36:03 AM5/15/18
to puppe...@googlegroups.com

Henrik Lindberg (JIRA)

unread,
May 15, 2018, 10:37:04 AM5/15/18
to 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)

unread,
May 15, 2018, 10:37:04 AM5/15/18
to puppe...@googlegroups.com

Kenn Hussey (JIRA)

unread,
Jun 21, 2018, 12:48:04 PM6/21/18
to puppe...@googlegroups.com

Rob Braden (JIRA)

unread,
Aug 8, 2018, 4:54:02 PM8/8/18
to puppe...@googlegroups.com

Rob Braden (JIRA)

unread,
Aug 8, 2018, 4:55:03 PM8/8/18
to puppe...@googlegroups.com

Kenn Hussey (JIRA)

unread,
Sep 10, 2018, 9:30:08 AM9/10/18
to puppe...@googlegroups.com

Henrik Lindberg (JIRA)

unread,
Sep 13, 2018, 1:55:03 AM9/13/18
to puppe...@googlegroups.com

Kenn Hussey (JIRA)

unread,
Sep 13, 2018, 9:47:04 AM9/13/18
to puppe...@googlegroups.com

Henrik Lindberg (JIRA)

unread,
Sep 13, 2018, 11:28:03 AM9/13/18
to puppe...@googlegroups.com

Kris Bosland (JIRA)

unread,
Sep 21, 2018, 2:06:04 PM9/21/18
to 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)

unread,
Jan 12, 2022, 7:57:01 PM1/12/22
to 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)

unread,
Feb 22, 2022, 1:50:02 PM2/22/22
to puppe...@googlegroups.com

Molly Waggett (Jira)

unread,
Feb 22, 2022, 1:50:02 PM2/22/22
to puppe...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages