[OPENDJ-2299] ResultCode.valueOf throw java.lang.IndexOutOfBoundsException if the value is not in [0,16655[ Created: 23/Sep/15  Updated: 08/Nov/19  Resolved: 08/Oct/15

Status: Done
Project: OpenDJ
Component/s: core apis
Affects Version/s: 2.6.11-sdk
Fix Version/s: 2.6.11-sdk

Type: Bug Priority: Minor
Reporter: Quentin CASTEL [X] (Inactive) Assignee: Matthew Swift
Resolution: Fixed Votes: 0
Labels: Verified, release-notes

Issue Links:
Depends
is required by OPENAM-6867 changePassword REST endpoint is not r... Resolved

 Description   

The safe check is not correct

Currently:

 if (intValue >= 0 || intValue < ELEMENTS.length) {

should be:

 if (intValue >= 0 && intValue < ELEMENTS.length) {

I like (0 <= intValue && intValue < ELEMENTS.length), I found it more readable

ResultCode.java
    /**
     * Returns the result code having the specified integer value as defined in
     * RFC 4511 section 4.1.9. If there is no result code associated with the
     * specified integer value then a temporary result code is automatically
     * created in order to handle cases where unexpected result codes are
     * returned from the server.
     *
     * @param intValue
     *            The integer value of the result code.
     * @return The result code.
     */
    public static ResultCode valueOf(final int intValue) {
        ResultCode resultCode = null;

        if (intValue >= 0 || intValue < ELEMENTS.length) {
            resultCode = ELEMENTS[intValue];
        }

        if (resultCode == null) {
            resultCode =
                    new ResultCode(intValue, LocalizableMessage.raw("undefined(" + intValue + ")"),
                            true);
        }

        return resultCode;
    }


 Comments   
Comment by Jean-Noël Rouvignac [ 23/Sep/15 ]

Note: this is not a problem in opendj-sdk 3.0.0

Quentin CASTEL [X] I agree with your comment regarding how the condition should be written.

Comment by Christophe Sovant [ 08/Oct/15 ]

Verified using SDK 2.6.11 rev ee8adac5225770115e0106224b7911fe9c7f9ba9

I wrote a small test to use valueOf() with different values.

Here is the result before the fix:

Number elements: 16655

Example with RC: 89
Get: Parameter Error

Try with RC: -1
Get exception: java.lang.ArrayIndexOutOfBoundsException: -1

Try with RC: 0
Get: Success

Try with RC: 1
Get: Operations Error

Try with RC: 16653
Get: undefined(16653)

Try with RC: 16654
Get: No Operation

Try with RC: 16655
Get exception: java.lang.ArrayIndexOutOfBoundsException: 16655

Try with RC: 16656
Get exception: java.lang.ArrayIndexOutOfBoundsException: 16656

==> we get the "ArrayIndexOutOfBoundsException" as described in the issue

Here is the result with the fix:

Number elements: 16655

Example with RC: 89
Get: Parameter Error

Try with RC: -1
Get: undefined(-1)

Try with RC: 0
Get: Success

Try with RC: 1
Get: Operations Error

Try with RC: 16653
Get: undefined(16653)

Try with RC: 16654
Get: No Operation

Try with RC: 16655
Get: undefined(16655)

Try with RC: 16656
Get: undefined(16656)

=> no more "ArrayIndexOutOfBoundsException"

Comment by Mark Craig [ 08/Oct/15 ]

Apply release-notes label

Generated at Tue Nov 24 00:47:26 UTC 2020 using Jira 7.13.12#713012-sha1:6e07c38070d5191bbf7353952ed38f111754533a.