Skip to content

Commit

Permalink
AVRO-3403: Improve number parsing
Browse files Browse the repository at this point in the history
Integer/Long parsing now also allows the hexadecimal and octal syntax
that the grammar allows.
  • Loading branch information
opwvhk committed Aug 9, 2023
1 parent 634f1d4 commit b76d39d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
63 changes: 32 additions & 31 deletions lang/java/idl/src/main/java/org/apache/avro/idl/IdlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@
*/
package org.apache.avro.idl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
Expand Down Expand Up @@ -46,6 +73,7 @@
import org.apache.avro.idl.IdlParser.FieldDeclarationContext;
import org.apache.avro.idl.IdlParser.FixedDeclarationContext;
import org.apache.avro.idl.IdlParser.FormalParameterContext;
import org.apache.avro.idl.IdlParser.FullTypeContext;
import org.apache.avro.idl.IdlParser.IdentifierContext;
import org.apache.avro.idl.IdlParser.IdlFileContext;
import org.apache.avro.idl.IdlParser.ImportStatementContext;
Expand All @@ -64,39 +92,11 @@
import org.apache.avro.idl.IdlParser.RecordDeclarationContext;
import org.apache.avro.idl.IdlParser.ResultTypeContext;
import org.apache.avro.idl.IdlParser.SchemaPropertyContext;
import org.apache.avro.idl.IdlParser.FullTypeContext;
import org.apache.avro.idl.IdlParser.UnionTypeContext;
import org.apache.avro.idl.IdlParser.VariableDeclarationContext;
import org.apache.avro.util.internal.Accessor;
import org.apache.commons.text.StringEscapeUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableMap;

Expand All @@ -106,6 +106,7 @@ public class IdlReader {
* easy access to the (reasonably readable) error message elsewhere.
*/
private static final BaseErrorListener SIMPLE_AVRO_ERROR_LISTENER = new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException e) {
throw new SchemaParseException("line " + line + ":" + charPositionInLine + " " + msg);
Expand Down Expand Up @@ -492,7 +493,7 @@ public void exitFixedDeclaration(FixedDeclarationContext ctx) {
String identifier = identifier(ctx.name);
String name = name(identifier);
String space = namespace(identifier, properties.namespace());
int size = Integer.parseInt(ctx.size.getText());
int size = Integer.decode(ctx.size.getText());

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
Schema schema = Schema.createFixed(name, doc, space, size);
properties.copyAliases(schema::addAlias);
properties.copyProperties(schema);
Expand Down Expand Up @@ -810,8 +811,8 @@ public void exitPrimitiveType(PrimitiveTypeContext ctx) {
typeStack.push(LogicalTypes.uuid().addToSchema(Schema.create(Schema.Type.STRING)));
break;
default: // Only option left: decimal
int precision = Integer.parseInt(ctx.precision.getText());
int scale = ctx.scale == null ? 0 : Integer.parseInt(ctx.scale.getText());
int precision = Integer.decode(ctx.precision.getText());

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
int scale = ctx.scale == null ? 0 : Integer.decode(ctx.scale.getText());

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
typeStack.push(LogicalTypes.decimal(precision, scale).addToSchema(Schema.create(Schema.Type.BYTES)));
break;
}
Expand Down
2 changes: 1 addition & 1 deletion lang/java/idl/src/test/idl/input/simple.avdl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protocol Simple {
}

/** An MD5 hash. */
fixed MD5(16);
fixed MD5(0x10);

error TestError {
string message;
Expand Down

0 comments on commit b76d39d

Please sign in to comment.