Skip to content

Commit 46d9f10

Browse files
author
Eduardo Gulias Davis
committed
control unclosed brackets
1 parent 1500ff0 commit 46d9f10

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/main/java/emailvalidator4j/parser/DomainPart.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void parse(String domainPart) throws InvalidEmail {
5353

5454
private void doParseDomainPart() throws InvalidEmail {
5555
int domainPartOpenedParenthesis = 0;
56+
boolean openBrackets = false;
5657
do {
5758
if (this.lexer.getCurrent().equals(Tokens.SEMICOLON)) {
5859
throw new ExpectedATEXT("Expected ATEXT");
@@ -84,7 +85,8 @@ private void doParseDomainPart() throws InvalidEmail {
8485
this.checkConsecutiveDots();
8586
this.checkExceptions();
8687

87-
if (this.lexer.getCurrent().equals(Tokens.OPENBRACKET)) {
88+
if (isDomainLiteral(openBrackets)) {
89+
openBrackets = true;
8890
this.parseLiteralPart();
8991
}
9092

@@ -98,6 +100,14 @@ private void doParseDomainPart() throws InvalidEmail {
98100
} while (!this.lexer.isAtEnd());
99101
}
100102

103+
private boolean isDomainLiteral(boolean openBrackets) throws UnclosedDomainLiteral {
104+
if (this.lexer.getCurrent().equals(Tokens.CLOSEBRACKET) && !openBrackets) {
105+
throw new UnclosedDomainLiteral("Missing open bracket [");
106+
}
107+
108+
return this.lexer.getCurrent().equals(Tokens.OPENBRACKET);
109+
}
110+
101111
private void checkNotAllowedChars(TokenInterface token) throws DomainNotAllowedCharacter {
102112
if (notAllowedTokens.contains(token)) {
103113
throw new DomainNotAllowedCharacter(

src/test/java/emailvalidator4j/parser/DomainPartTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public static Object[][] invalidDomainParts() {
4949
{DomainHyphen.class, "@atstart-.com"},
5050
{DomainNotAllowedCharacter.class, "@atst\\art.com"},
5151
{DomainNotAllowedCharacter.class, "@example\\"},
52-
{DomainNotAllowedCharacter.class, "@exa\\mple"}
52+
{DomainNotAllowedCharacter.class, "@exa\\mple"},
53+
{UnclosedDomainLiteral.class, "@example]"},
5354
};
5455
}
5556

0 commit comments

Comments
 (0)