On Wed, 8 May 2024 19:06:35 GMT, ExE Boss <d...@openjdk.org> wrote:

>> Claes Redestad has updated the pull request incrementally with one 
>> additional commit since the last revision:
>> 
>>   Refactor to further avoid re-validating arguments
>
> src/java.base/share/classes/jdk/internal/constant/MethodTypeDescImpl.java 
> line 179:
> 
>> 177: 
>> 178:         for (ClassDesc param : paramTypes)
>> 179:             validateArgument(param);
> 
> This check should be performed as part of copying `paramTypes` to `newArgs` 
> to avoid TOC/TOU issues, e.g.:
> 
> @Override
> public MethodTypeDesc insertParameterTypes(int pos, ClassDesc... paramTypes) {
>       if (pos < 0 || pos > argTypes.length)
>               throw new IndexOutOfBoundsException(pos);
> 
>       ClassDesc[] newArgs = new ClassDesc[argTypes.length + 
> paramTypes.length];
>       if (pos > 0) {
>               System.arraycopy(argTypes, 0, newArgs, 0, pos);
>       }
>       for (int i = 0; i < paramTypes.length; i++) {
>               newArgs[pos + i] = validateArgument(paramTypes[i]);
>       }
>       if (pos < argTypes.length) {
>               System.arraycopy(argTypes, pos, newArgs, pos + 
> paramTypes.length, argTypes.length - pos);
>       }
>       return ofValidated(returnType, newArgs);
> }
> 
> 
> See also:
> https://github.com/openjdk/jdk/blob/230fac80f25e9608006c8928a8a7708bf13a818c/src/java.base/share/classes/java/util/ImmutableCollections.java#L186-L195

Nice catch! I conservatively just move the validation loop after to keep the 
arraycopying.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/19105#discussion_r1594579344

Reply via email to