Skip to content

Commit

Permalink
Fix for memory overflow error
Browse files Browse the repository at this point in the history
Raises argument error if a large integer argument is passed.

Closes #478
  • Loading branch information
iamanujvrma authored and stympy committed Sep 7, 2016
1 parent dd46486 commit cad281d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/faker/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def user_name(specifier = nil, separators = %w(. _))
if specifier.kind_of? String
return specifier.scan(/\w+/).shuffle.join(separators.sample).downcase
elsif specifier.kind_of? Integer
# If specifier is Integer and has large value, Argument error exception is raised to overcome memory full error
raise ArgumentError, "Given argument is too large" if specifier > 10**6
tries = 0 # Don't try forever in case we get something like 1_000_000.
begin
result = user_name nil, separators
tries += 1
end while result.length < specifier and tries < 7
until result.length >= specifier
result = result * 2
end
result = result * (specifier/result.length + 1) if specifier > 0
return result
elsif specifier.kind_of? Range
tries = 0
Expand Down
5 changes: 5 additions & 0 deletions test/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def test_user_name_with_integer_arg
assert @tester.user_name(min_length).length >= min_length
end
end

def test_user_name_with_very_large_integer_arg
exception = assert_raises(ArgumentError) { @tester.user_name(10000000) }
assert_equal('Given argument is too large', exception.message)
end

def test_user_name_with_closed_range_arg
(1..32).each do |min_length|
Expand Down

0 comments on commit cad281d

Please sign in to comment.