From af5e4ca3e0e137518e0e56fafd1a96aee66a0c5f Mon Sep 17 00:00:00 2001 From: cn Date: Fri, 28 Feb 2020 16:44:27 +0100 Subject: [PATCH] dyndnsd: handle potential nil cases detected by sorbet - including review suggestions from @jgraichen --- CHANGELOG.md | 6 ++++++ lib/dyndnsd.rb | 10 ++++++++-- lib/dyndnsd/database.rb | 2 +- lib/dyndnsd/helper.rb | 2 +- lib/dyndnsd/updater/command_with_bind_zone.rb | 4 ++-- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f76bd1..5104587 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 2.1.1 + +IMPROVEMENTS: + +- Fix potential `nil` cases detected by [Sorbet](https://sorbet.org) including refactorings + ## 2.1.0 (March 1, 2020) IMPROVEMENTS: diff --git a/lib/dyndnsd.rb b/lib/dyndnsd.rb index 20ecea6..86898c8 100755 --- a/lib/dyndnsd.rb +++ b/lib/dyndnsd.rb @@ -112,8 +112,14 @@ module Dyndnsd # drop priviliges as soon as possible # NOTE: first change group than user - Process::Sys.setgid(Etc.getgrnam(config['group']).gid) if config['group'] - Process::Sys.setuid(Etc.getpwnam(config['user']).uid) if config['user'] + if config['group'] + group = Etc.getgrnam(config['group']) + Process::Sys.setgid(group.gid) if group + end + if config['user'] + user = Etc.getpwnam(config['user']) + Process::Sys.setuid(user.uid) if user + end setup_traps diff --git a/lib/dyndnsd/database.rb b/lib/dyndnsd/database.rb index daedef3..591e348 100644 --- a/lib/dyndnsd/database.rb +++ b/lib/dyndnsd/database.rb @@ -15,7 +15,7 @@ module Dyndnsd # @return [void] def load if File.file?(@db_file) - @db = JSON.parse(File.open(@db_file, 'r', &:read)) + @db = JSON.parse(File.read(@db_file, mode: 'r')) else @db = {} end diff --git a/lib/dyndnsd/helper.rb b/lib/dyndnsd/helper.rb index 9ac124f..6827663 100644 --- a/lib/dyndnsd/helper.rb +++ b/lib/dyndnsd/helper.rb @@ -57,7 +57,7 @@ module Dyndnsd 'error.kind': e.class.to_s, 'error.object': e, message: e.message, - stack: e.backtrace.join("\n") + stack: e.backtrace&.join("\n") || '' ) raise ensure diff --git a/lib/dyndnsd/updater/command_with_bind_zone.rb b/lib/dyndnsd/updater/command_with_bind_zone.rb index 8ac236b..14d5812 100644 --- a/lib/dyndnsd/updater/command_with_bind_zone.rb +++ b/lib/dyndnsd/updater/command_with_bind_zone.rb @@ -14,7 +14,7 @@ module Dyndnsd # @return [void] def update(db) Helper.span('updater_update') do |span| - span.set_tag('dyndnsd.updater.name', self.class.name.split('::').last) + span.set_tag('dyndnsd.updater.name', self.class.name&.split('::')&.last || 'None') # write zone file in bind syntax File.open(@zone_file, 'w') { |f| f.write(@generator.generate(db)) } @@ -24,7 +24,7 @@ module Dyndnsd end # detach so children don't become zombies - Process.detach(pid) + Process.detach(pid) if pid end end end