Fix critical exception handling and Ruby idiom violations #5531
+51
−50
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull request overview
Addresses critical security and code quality issues flagged by Ultimate Bug Scanner: improper exception handling, resource leaks, and non-idiomatic nil checks.
Changes
Exception Handling (13 instances)
rescue Exception→rescue StandardErrorto prevent catching system signalsrescue→rescue StandardErrorfor explicit error handlingmeasure_manager_server.rb,embedded_help.rb,measure_manager_test.rb,openstudio_cli.rb,measure_manager.rbResource Management (1 instance)
BlameFiles.rb: File handle leak fixed using block form ofFile.openNil Checks (30+ instances)
== nil/!= nil→.nil?/!.nil?SwigWrapToRDoc.rb,openstudio_cli.rb,Polygon3d_Join_Test.rb, othersNot Changed (intentional)
Marshal.loadin tests (testing Marshal functionality)evalusage (required for CLI dynamic loading)Pull Request Author
Labels:
Pull Request - Ready for CIso that CI builds your PRReview Checklist
Original prompt
This section details on the original issue you should resolve
<issue_title>Bugs from Ultimate Bug Scanner</issue_title>
<issue_description>Issue overview
Project: /Users/achapin/OpenStudio/openstudio-full/OpenStudio
Started: 2025-11-19T04:48:14Z
Files: 0 source files (rb,rake,ru,gemspec,erb,haml,slim,rbi,rbs,jbuilder)
✓ ast-grep available (ast-grep) - full AST analysis enabled
⚠ Bundler or Gemfile not detected - will run tools if globally installed
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
▓▓▓ Detects: nil equality, deep method chains without guards, dig? usage
Prefer x.nil?, safe navigation (&.), and Hash#dig to avoid NoMethodError.
• == nil or != nil (prefer .nil?)
⚠ Warning (30 found)
Equality to nil
Use x.nil? / !x.nil?
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/SwigWrapToRDoc.rb:61 (
OpenStudio/developer/ruby/SwigWrapToRDoc.rb
Line 61 in c7f13ad
if strArray[i].index(/SWIGEXPORT void Init_/) != nil then
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/SwigWrapToRDoc.rb:72 (
OpenStudio/developer/ruby/SwigWrapToRDoc.rb
Line 72 in c7f13ad
if strArray[i].index(/Document-[\w]*: /) == nil then
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/SwigWrapToRDoc.rb:79 (
OpenStudio/developer/ruby/SwigWrapToRDoc.rb
Line 79 in c7f13ad
if ans != nil then
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/SwigWrapToRDoc.rb:105 (
OpenStudio/developer/ruby/SwigWrapToRDoc.rb
Line 105 in c7f13ad
if (ln.match(/static VALUE/) != nil) ||
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/SwigWrapToRDoc.rb:106 (
OpenStudio/developer/ruby/SwigWrapToRDoc.rb
Line 106 in c7f13ad
(ln.match(/wrap/) != nil) ||
• Deep method chains (use &. / guards)
ℹ Info (307 found)
Fragile deep chaining
Consider &. or guard clauses
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/ruby/engine/measure_manager_server.rb:163 (
OpenStudio/ruby/engine/measure_manager_server.rb
Line 163 in c7f13ad
OpenStudio::LocalBCL.instance.measures.each do |local_measure|
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/ruby/engine/embedded_help.rb:28 (
OpenStudio/ruby/engine/embedded_help.rb
Line 28 in c7f13ad
if p.to_s.chars.first == ':' then
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/ruby/engine/embedded_help.rb:183 (
OpenStudio/ruby/engine/embedded_help.rb
Line 183 in c7f13ad
elsif path_with_extension.to_s.chars.first == ':'
• Hash#[] chained without dig
ℹ Info (96 found)
Nested [] access
Consider Hash#dig(:a,:b)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2. NUMERIC / ARITHMETIC PITFALLS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
▓▓▓ Detects: division by variable, float equality, modulo hazards
Guard divisors and avoid exact float equality.
• Division by variable (possible ÷0)
⚠ Warning (999 found)
Division by variable - verify non-zero
Guard: raise if denom.zero?
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/FindEncodingProblems.rb:9 (
OpenStudio/developer/ruby/FindEncodingProblems.rb
Line 9 in c7f13ad
if /build/.match(p) || /style/.match(p)
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/FindEncodingProblems.rb:16 (
OpenStudio/developer/ruby/FindEncodingProblems.rb
Line 16 in c7f13ad
if /BOM/.match(output)
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/AnalyzeDumpbin.rb:1 (
OpenStudio/developer/ruby/AnalyzeDumpbin.rb
Line 1 in c7f13ad
# to run this script, change directories to /build/src and call it
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/AnalyzeDumpbin.rb:9 (
OpenStudio/developer/ruby/AnalyzeDumpbin.rb
Line 9 in c7f13ad
excludes = [/boost-log/, /expat/, /gtest/, /libssh/, /litesql/, /qwt/, /sqlite/]
/Users/achapin/OpenStudio/openstudio-full/OpenStudio/developer/ruby/AnalyzeDumpbin.rb:24 (https://github.com/NREL/OpenStudio/blob/c7f13ad61579ceacf4fbe742b9a6e0c71a14cb4d/developer/r...
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.