Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in StringRandomizer for large String #282

Closed
AVE-cesar opened this issue Nov 7, 2017 · 2 comments
Closed

bug in StringRandomizer for large String #282

AVE-cesar opened this issue Nov 7, 2017 · 2 comments
Labels
Milestone

Comments

@AVE-cesar
Copy link

AVE-cesar commented Nov 7, 2017

I've found a little bug when I use your wonderful tool on a large String.
I am using version 3.7.0.

This is an extract of my class:

@Size(max=2147483647)
public String getName() {
	return name;
}

The impacted line is:

    int length = minLength + random.nextInt(maxLength - minLength + 1);

the calculated length is: -2147483648 but the length MUST be positive !

This is the stack trace I've got:

Exception in thread "main" io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class a.b.Person
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.EnhancedRandomImpl.nextObject(EnhancedRandomImpl.java:77)
	at ave.bertrand.MainClass.main(MainClass.java:18)
Caused by: java.lang.IllegalArgumentException: bound must be positive
	at java.util.Random.nextInt(Random.java:388)
	at io.github.benas.randombeans.randomizers.text.StringRandomizer.getRandomValue(StringRandomizer.java:256)
	at io.github.benas.randombeans.randomizers.text.StringRandomizer.getRandomValue(StringRandomizer.java:36)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:78)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 2 more
@fmbenhassine
Copy link
Member

Hi @AVE-cesar

Thank you for reporting this issue!
Indeed, random beans fails miserably on this value.

I will fix this issue and get back to you asap.

Kr
Mahmoud

@fmbenhassine fmbenhassine added this to the v3.8.0 milestone Nov 8, 2017
@fmbenhassine fmbenhassine changed the title bug in StringRandpmizer for large String bug in StringRandomizer for large String Mar 17, 2018
fmbenhassine added a commit that referenced this issue Nov 18, 2018
…Integer.MAX_VALUE

Note there is no test for this case as it fails with OOM when the random
String length is too high.
@fmbenhassine
Copy link
Member

Hi,

2147483647 is actually the value of Integer.MAX_VALUE, so @Size(max=2147483647) is equivalent to @Size. This is perfectly valid value to be specified as a user, and the issue happens because Integer.MAX_VALUE + 1 == Integer.MIN_VALUE (explained in details on SO here).

The +1 in the formula was introduced in 587e600 to fix the case where the generated String can never have the max length.

I fixed the issue and now the random String length is correctly calculated. However, when the random String length is too high, a OOM might happen and random beans can do nothing to prevent it (see #289).

The fix has been deployed to maven central in 3.8.0-SNAPSHOT. I'm closing this issue for now. @AVE-cesar It would be great if you give the fix a try (see here on how to use the snapshot version). Please add a comment here if you find any issue.

Kr,
Mahmoud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants