Skip to content

Commit

Permalink
Ensure PID is an integer
Browse files Browse the repository at this point in the history
This ensures that the `ps` call doesn't invoke something else.
  • Loading branch information
stanhu committed Mar 15, 2021
1 parent 2897a5a commit 70c8762
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/get_process_mem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class GetProcessMem
def initialize(pid = Process.pid)
@status_file = Pathname.new "/proc/#{pid}/status"
@process_file = Pathname.new "/proc/#{pid}/smaps"
@pid = pid
@pid = Integer(pid)
@linux = @status_file.exist?
end

Expand Down
9 changes: 9 additions & 0 deletions test/get_process_mem_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ def test_different_pid_returns_different_memory
Process.wait(pid) if pid
end

def test_invalid_pid
raised_exception = false
GetProcessMem.new('ls')
rescue ArgumentError
raised_exception = true
ensure
assert raised_exception
end

def test_seems_to_work
assert @mem.kb > 0
assert @mem.mb > 0
Expand Down

0 comments on commit 70c8762

Please sign in to comment.