Project

General

Profile

Actions

Feature #13577

open

Digest.file accidentally receives File object but uses file path

Added by naruse (Yui NARUSE) over 7 years ago. Updated over 7 years ago.

Status:
Assigned
Target version:
-
[ruby-core:81271]

Description

Digest::SHA256.file()'s first argument is file path name but it accidentally accepts file object.
But for file objects created with O_TMPFILE to_path returns the directory of the temporary file and this File.open will fail.

  class ::Digest::Class
    # Creates a digest object and reads a given file, _name_.
    # Optional arguments are passed to the constructor of the digest
    # class.
    #
    #   p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
    #   # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
    def self.file(name, *args)
      new(*args).file(name)
    end
  end

  module Instance
    # Updates the digest with the contents of a given file _name_ and
    # returns self.
    def file(name)
      File.open(name, "rb") {|f|
        buf = ""
        while f.read(16384, buf)
          update buf
        end
      }
      self
    end

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #13568: File#path for O_TMPFILE fds has no meaningClosedsorah (Sorah Fukumori)Actions

Updated by Hanmac (Hans Mackowiak) over 7 years ago

as Sorah did there: https://github.com/aws/aws-sdk-ruby/pull/1516
i would suggest that Digest should try to read file objects directly giving to the #file method if able.

Actions #2

Updated by naruse (Yui NARUSE) over 7 years ago

  • Related to Feature #13568: File#path for O_TMPFILE fds has no meaning added

Updated by naruse (Yui NARUSE) over 7 years ago

Hanmac (Hans Mackowiak) wrote:

as Sorah did there: https://github.com/aws/aws-sdk-ruby/pull/1516
i would suggest that Digest should try to read file objects directly giving to the #file method if able.

Yeah.
It should use pread if it can or simply read then restore the pos.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0