Uploaded image for project: 'Modules'
  1. Modules
  2. MODULES-6990

stdlib : 'any2bool' documented functionality differs from implementation

    XMLWordPrintable

Details

    • Bug
    • Status: Accepted
    • Normal
    • Resolution: Unresolved
    • None
    • None
    • stdlib
    • None
    • Verified on:

      puppetlabs-stdlib 4.19.0 and latest

      puppet 3.7.2 and 4.7

       

    • Modules
    • Needs Assessment
    • Needs Assessment

    Description

      According to the documentation of any2bool:

      * Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true
      * Strings such as 0,F,f,N,n,FALSE,no,'false' will return false
      * Booleans will just return their original value
      * Number (or a string representation of a number) > 0 will return true, otherwise false
      * undef will return false
      * Anything else will return true

      The last part 'Anything else will return true' I interpret as:

      any2bool('foobar') => true

       

      When looking at the implementation, the decision is delegated to str2bool:

      ...
      if arg.is_a?(String)
            if valid_float
              return function_num2bool( [ arguments[0] ] )
            else
              return function_str2bool( [ arguments[0] ] )
            end
      end
      return true

      Having a closer look at str2bool the issue seems to be that it only accepts a limited input set:

      ...
      when /^$/, '' then false # Empty string will be false ...
      when /^(1|t|y|true|yes)$/i  then true
      when /^(0|f|n|false|no)$/i  then false
      when /^(undef|undefined)$/ then false # This is not likely to happen ...
      else
          raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given')
      ...

      One could fix this by:

      • Removing support for arbitrary strings (defeating the purpose of any2bool)
      • Catching the exception thrown in str2bool and defaulting to true
      • Allowing arbitrary strings in str2bool and handling it correctly there

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            fabio.germann Fabio Germann
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Zendesk Support