Merge pull request #20377 from Homebrew/fix-linkage-checker-file-descriptors

Fix file descriptor leak in Linux LD library path parsing
This commit is contained in:
Mike McQuaid 2025-08-05 15:02:04 +00:00 committed by GitHub
commit a729c533fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -50,29 +50,37 @@ module OS
conf_file = Pathname(conf_path) conf_file = Pathname(conf_path)
return [] unless conf_file.exist? return [] unless conf_file.exist?
@library_paths_cache ||= T.let({}, T.nilable(T::Hash[String, T::Array[String]]))
cache_key = conf_file.to_s
if (cached_library_path_contents = @library_paths_cache[cache_key])
return cached_library_path_contents
end
paths = Set.new paths = Set.new
directory = conf_file.realpath.dirname directory = conf_file.realpath.dirname
conf_file.readlines.each do |line| conf_file.open("r") do |file|
# Remove comments and leading/trailing whitespace file.each_line do |line|
line.strip! # Remove comments and leading/trailing whitespace
line.sub!(/\s*#.*$/, "") line.strip!
line.sub!(/\s*#.*$/, "")
if line.start_with?(/\s*include\s+/) if line.start_with?(/\s*include\s+/)
include_path = Pathname(line.sub(/^\s*include\s+/, "")).expand_path include_path = Pathname(line.sub(/^\s*include\s+/, "")).expand_path
wildcard = include_path.absolute? ? include_path : directory/include_path wildcard = include_path.absolute? ? include_path : directory/include_path
Dir.glob(wildcard.to_s).each do |include_file| Dir.glob(wildcard.to_s).each do |include_file|
paths += library_paths(include_file) paths += library_paths(include_file)
end
elsif line.empty?
next
else
paths << line
end end
elsif line.empty?
next
else
paths << line
end end
end end
paths.to_a @library_paths_cache[cache_key] = paths.to_a
end end
end end
end end