[ 
https://issues.apache.org/jira/browse/OFBIZ-12838?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17743622#comment-17743622
 ] 

Chirag Jain commented on OFBIZ-12838:
-------------------------------------

The changes made in the ShowPromoText.groovy script are causing an error with 
the message "java.lang.IllegalArgumentException: Error running script at 
location [component://order/groovyScripts/entry/ShowPromoText.groovy]: 
java.lang.IndexOutOfBoundsException".

The specific line causing the error involves adding an element from 
productPromosRandomTemp to productPromos.

#productPromos.add(productPromosRandomTemp.remove(randomIndex)) 
Here code removes a element at randomIndex from productPromosRandomTemp and add 
that element in productPromos list.

Here randomIndex is generated using SecureRandom().nextInt() and the 
randomIndex generated using SecureRandom().nextInt() can be All 2^32 possible 
int values.
#randomIndex = Math.round(new SecureRandom().nextInt() * 
(productPromosRandomTemp.size() - 1)) as int

If we give a index to remove function which is greater than the size() of the 
list on which remove applied , Then remove give the 
"java.lang.IndexOutOfBoundsException"

To resolve this issue, we need to ensure that the random index generated is 
within the bounds of productPromosRandomTemp. Currently, the random index is 
generated using SecureRandom().nextInt() without any bounds. This can 
potentially lead to an index greater than or equal to 
productPromosRandomTemp.size(), causing the IndexOutOfBoundsException.

By using Math.random(), we generate a random number between 0 and 1. Then, by 
multiplying this random number by productPromosRandomTemp.size(), we ensure 
that the random index is within the valid range for the list.

Here is the possible solution for this to give a bound as the parameter to the 
nextInt() function of secureRandom 
randomIndex = new SecureRandom().nextInt(productPromosRandomTemp.size())
In this case, SecureRandom().nextInt(n) generates a random integer between 0 
(inclusive) and n (exclusive), so it will be within the bounds of the list.

Alternatively use java.lang.Math.random() which generate value between 0 to 1 
and then can multiple that random number by productPromosRandomTemp.size() - 1 
for valid randomIndex.

> IndexOutOfBoundsException occured while placing order
> -----------------------------------------------------
>
>                 Key: OFBIZ-12838
>                 URL: https://issues.apache.org/jira/browse/OFBIZ-12838
>             Project: OFBiz
>          Issue Type: Bug
>          Components: order
>    Affects Versions: Upcoming Branch
>            Reporter: Chirag Jain
>            Assignee: Jacques Le Roux
>            Priority: Minor
>         Attachments: screenshot-1.png
>
>
> Faced error while order entry process
> Steps to know where the error is coming
> 1. click on the order WebApp 
> https://demo-trunk.ofbiz.apache.org/ordermgr/control/main
> 2. Then there is a New Order option on main page
> 3. When click to the New Order an error Occured which is
> " java.lang.IllegalArgumentException: Error running script at location 
> [component://order/groovyScripts/entry/ShowPromoText.groovy]: 
> java.lang.IndexOutOfBoundsException: Index -925041216 out of bounds for 
> length 14 "



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to