This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push: new 0988941b Refactor U2 value check in Code 0988941b is described below commit 0988941bc3e8b1550ac392a901e6642d7d667e7e Author: Gary David Gregory (Code signing key) <ggreg...@apache.org> AuthorDate: Tue Nov 15 10:46:33 2022 -0500 Refactor U2 value check in Code --- src/main/java/org/apache/bcel/classfile/Code.java | 7 +-- src/main/java/org/apache/bcel/util/Args.java | 69 +++++++++++++++++++++++ 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/bcel/classfile/Code.java b/src/main/java/org/apache/bcel/classfile/Code.java index 38fe68a7..0ec8f6f4 100644 --- a/src/main/java/org/apache/bcel/classfile/Code.java +++ b/src/main/java/org/apache/bcel/classfile/Code.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.Arrays; import org.apache.bcel.Const; +import org.apache.bcel.util.Args; import org.apache.commons.lang3.ArrayUtils; /** @@ -65,11 +66,7 @@ public final class Code extends Attribute { Code(final int nameIndex, final int length, final DataInput file, final ConstantPool constantPool) throws IOException { // Initialize with some default values which will be overwritten later this(nameIndex, length, file.readUnsignedShort(), file.readUnsignedShort(), (byte[]) null, (CodeException[]) null, (Attribute[]) null, constantPool); - final int codeLength = file.readInt(); - if (codeLength < 1 || codeLength > Const.MAX_SHORT) { - throw new ClassFormatException( - String.format("Invalid length %,d for Code attribute. Must be greater than zero and less than %,d.", codeLength, Const.MAX_SHORT)); - } + final int codeLength = Args.requireU2(file.readInt(), 1, "Invalid length for Code attribute"); code = new byte[codeLength]; // Read byte code file.readFully(code); /* diff --git a/src/main/java/org/apache/bcel/util/Args.java b/src/main/java/org/apache/bcel/util/Args.java new file mode 100644 index 00000000..e6d49dc3 --- /dev/null +++ b/src/main/java/org/apache/bcel/util/Args.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.bcel.util; + +import org.apache.bcel.Const; +import org.apache.bcel.classfile.ClassFormatException; + +/** + * Argument validation. + * + * @since 6.6.2 + */ +public class Args { + + /** + * Requires a non-0 value. + * + * @param value The value to test. + * @param message The message prefix + * @return The value to test. + */ + public static int require0(final int value, final String message) { + if (value != 0) { + throw new ClassFormatException(String.format("%s [Value must be 0: %,d]", message, value)); + } + return value; + } + + /** + * Requires a u2 value of at least {@code min}. + * + * @param value The value to test. + * @param min The minimum required value. + * @param message The message prefix + * @return The value to test. + */ + public static int requireU2(final int value, final int min, final String message) { + if (value < min || value > Const.MAX_SHORT) { + throw new ClassFormatException(String.format("%s [Value out of range (%,d - %,d) for type u2: %,d]", message, min, Const.MAX_SHORT, value)); + } + return value; + } + + /** + * Requires a u2 value. + * + * @param value The value to test. + * @param message The message prefix + * @return The value to test. + */ + public static int requireU2(final int value, final String message) { + return requireU2(value, 0, message); + } +}