You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Jira)" <ji...@apache.org> on 2025/11/04 22:40:00 UTC
[jira] [Comment Edited] (VALIDATOR-502) Circular dependency in static initialization causes NPE in GenericValidator.isCreditCard()
[ https://issues.apache.org/jira/browse/VALIDATOR-502?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18035452#comment-18035452 ]
Gary D. Gregory edited comment on VALIDATOR-502 at 11/4/25 10:39 PM:
---------------------------------------------------------------------
Hello [~markmiller]
Thank you for your report.
I took a simpler approach to fix this in git master.
Please test a local build or a snapshot from [https://repository.apache.org/content/repositories/snapshots/]
Let us know if you find other cases and the above works for you.
was (Author: garydgregory):
Hello [~markmiller]
Thank you for your report.
I took a simpler approach to fix this in git master.
Please test a local build or a snapshot from [https://repository.apache.org/content/repositories/snapshots/]
> Circular dependency in static initialization causes NPE in GenericValidator.isCreditCard()
> ------------------------------------------------------------------------------------------
>
> Key: VALIDATOR-502
> URL: https://issues.apache.org/jira/browse/VALIDATOR-502
> Project: Commons Validator
> Issue Type: Bug
> Components: CreditCard
> Affects Versions: 1.9.0, 1.10.0
> Reporter: Mark Miller
> Priority: Critical
> Attachments: GenericValidator.patch
>
>
>
> commons-validator 1.9.0 and 1.10.0 will throw a NullPointerException in {{GenericValidator.isCreditCard()}} (and likely elsewhere) if {{{}routines.CreditCardValidator{}}}'s class is initialized before {{{}GenericValidator{}}}'s class is initialized.
> h4. Dependency Chain
> {{{}CreditCardValidator{}}}'s clinit calls {{{}GenericValidator.isBlankOrNull(){}}}, which runs {{{}GenericValidator{}}}'s clinit, which initializes:
> {noformat}
> private static final CreditCardValidator CREDIT_CARD_VALIDATOR = new CreditCardValidator();{noformat}
> which references (among others):
> {noformat}
> public static final CodeValidator AMEX_VALIDATOR = new CodeValidator("^(3[47]\\d{13})$", LUHN_VALIDATOR);{noformat}
> ... but that field has not yet been initialized, so {{{}GenericValidator.CREDIT_CARD_VALIDATOR{}}}'s AMEX_VALIDATOR is unexpectedly null.
>
> Bug is reproducible by this test code:
> {noformat}
> @Test
> public void testGenericValidatorCircularDependency() {
> CreditCardValidator ccv = new CreditCardValidator(); // commenting out this line will avoid the NPE
> GenericValidator.isCreditCard("1234567890"); // throws NPE!!!
> }{noformat}
>
> Suggested patch to GenericValidator.java is attached - it simply puts the relevant fields into a LazyHolder inner class (which incidentally might provide a small performance boost in cases where `GenericValidator.isCreditCard()` and `GenericValidator.isUrl()` are not used).
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)