Skip to content

Commit

Permalink
Check for invalid integers in compiler's CLI (crystal-lang#13959)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored and Blacksmoke16 committed Dec 11, 2023
1 parent 3110863 commit 79d067b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ class Crystal::Command
compiler.release!
end
opts.on("-O LEVEL", "Optimization mode: 0 (default), 1, 2, 3") do |level|
compiler.optimization_mode = Compiler::OptimizationMode.from_value?(level.to_i) || raise Error.new("Unknown optimization mode #{level}")
optimization_mode = level.to_i?.try { |v| Compiler::OptimizationMode.from_value?(v) }
compiler.optimization_mode = optimization_mode || raise Error.new("Invalid optimization mode: #{level}")
end
end

Expand All @@ -524,7 +525,7 @@ class Crystal::Command
compiler.single_module = true
end
opts.on("--threads NUM", "Maximum number of threads to use") do |n_threads|
compiler.n_threads = n_threads.to_i
compiler.n_threads = n_threads.to_i? || raise Error.new("Invalid thread count: #{n_threads}")
end
unless run
opts.on("--target TRIPLE", "Target triple") do |triple|
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/crystal/command/playground.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Crystal::Command
opts.banner = "Usage: crystal play [options] [file]\n\nOptions:"

opts.on("-p PORT", "--port PORT", "Runs the playground on the specified port") do |port|
server.port = port.to_i
port = port.to_i?
raise Error.new("Invalid port number: #{port}") unless port && Socket::IPAddress.valid_port?(port)
server.port = port
end

opts.on("-b HOST", "--binding HOST", "Binds the playground to the specified IP") do |host|
Expand Down

0 comments on commit 79d067b

Please sign in to comment.