Skip to content

StackOverflowError when using DefaultValue.parsed("") #805

@McModknower

Description

@McModknower

When using DefaultValue.parsed("") or @Default(""), and the command input does not contain the corresponding Parameter, a StackOverflowError is thrown.

Test cases for reproduction:

  1. DefaultValue.parsed("")
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.issue;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.TestCommandSender;
import org.incendo.cloud.component.DefaultValue;
import org.incendo.cloud.execution.CommandResult;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.internal.CommandRegistrationHandler;
import org.incendo.cloud.parser.standard.StringParser;
import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;

/**
 * Test for https://github.com/Incendo/cloud/issues/todo.
 */
class ProbableIssue {

    @Test
    void emptyDefaultDoesNotCauseStackOverflow() {
        // Arrange
        final CommandManager<TestCommandSender> commandManager = new CommandManager<TestCommandSender>(
                ExecutionCoordinator.simpleCoordinator(),
                CommandRegistrationHandler.nullCommandRegistrationHandler()
        ) {
            @Override
            public boolean hasPermission(
                    final @NonNull TestCommandSender sender,
                    final @NonNull String permission
            ) {
                return true;
            }
        };

        commandManager.command(
                commandManager.commandBuilder("test")
                        .optional("arg", StringParser.stringParser(), DefaultValue.parsed(""))
                        .handler(context -> {})
                        .build());

        // Act
        final CommandResult<?> result = commandManager.commandExecutor().executeCommand(new TestCommandSender(), "test").join();

        // Assert
        assertThat(result.commandContext().<String>get("arg")).isEqualTo("");
    }
}
  1. @Default("")
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.annotations.issue;

import org.incendo.cloud.CommandManager;
import org.incendo.cloud.annotations.AnnotationParser;
import org.incendo.cloud.annotations.Command;
import org.incendo.cloud.annotations.Default;
import org.incendo.cloud.annotations.TestCommandManager;
import org.incendo.cloud.annotations.TestCommandSender;
import org.incendo.cloud.execution.CommandResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;

class ProbableIssue {

    private CommandManager<TestCommandSender> commandManager;
    private AnnotationParser<TestCommandSender> annotationParser;

    @BeforeEach
    void setup() {
        this.commandManager = new TestCommandManager();
        this.annotationParser = new AnnotationParser<>(
                this.commandManager,
                TestCommandSender.class
        );
    }

    @Test
    void test() {
        // Arrange
        this.annotationParser.parse(new TestClass());

        // Act
        final CommandResult<?> result =
                this.commandManager.commandExecutor().executeCommand(new TestCommandSender(), "empty").join();

        // Assert
        assertThat(result.commandContext().<String>get("arg")).isEqualTo("");
    }

    static class TestClass {

        @Command("empty [arg]")
        public void empty(@Default("") String arg) {
        }

    }
}

Relevant output of my example test case:

java.lang.StackOverflowError
java.util.concurrent.CompletionException: java.lang.StackOverflowError
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320)
	at java.base/java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:1159)
	at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:482)
	at org.incendo.cloud.execution.ExecutionCoordinatorImpl$NonSchedulingExecutor.execute(ExecutionCoordinatorImpl.java:53)
	at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1184)
	at java.base/java.util.concurrent.CompletableFuture.thenComposeAsync(CompletableFuture.java:2352)
	at org.incendo.cloud.CommandTree.lambda$parseCommand$4(CommandTree.java:306)
	at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1187)
	at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2341)
	at org.incendo.cloud.CommandTree.parseCommand(CommandTree.java:289)
	at org.incendo.cloud.CommandTree.parseDirect(CommandTree.java:205)
	at org.incendo.cloud.CommandTree.lambda$parse$0(CommandTree.java:180)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
	at org.incendo.cloud.execution.ExecutionCoordinatorImpl$NonSchedulingExecutor.execute(ExecutionCoordinatorImpl.java:53)
	at java.base/java.util.concurrent.CompletableFuture.asyncSupplyStage(CompletableFuture.java:1782)
	at java.base/java.util.concurrent.CompletableFuture.supplyAsync(CompletableFuture.java:2005)
	at org.incendo.cloud.util.CompletableFutures.scheduleOn(CompletableFutures.java:70)
	at org.incendo.cloud.CommandTree.parse(CommandTree.java:180)
	at org.incendo.cloud.execution.ExecutionCoordinatorImpl.coordinateExecution(ExecutionCoordinatorImpl.java:98)
	at org.incendo.cloud.StandardCommandExecutor.executeCommand(StandardCommandExecutor.java:91)
	at org.incendo.cloud.StandardCommandExecutor.executeCommand(StandardCommandExecutor.java:65)
	at org.incendo.cloud.execution.CommandExecutor.executeCommand(CommandExecutor.java:63)
	at org.incendo.cloud.issue.ProbableIssue.emptyDefaultDoesNotCauseStackOverflow(ProbableIssue.java:66)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	<internal lines not relevant to this issue>
	Suppressed: java.util.concurrent.CompletionException: java.lang.StackOverflowError
		at org.incendo.cloud.StandardCommandExecutor.lambda$executeCommand$0(StandardCommandExecutor.java:77)
		at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
		at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887)
		at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2357)
		... 83 more
	Caused by: java.lang.StackOverflowError
		<internal lines not relevant to this issue>
		at io.leangen.geantyref.GenericTypeReflector.isSuperType(GenericTypeReflector.java:400)
		at org.incendo.cloud.CommandTree.determineAccess(CommandTree.java:1037)
		at org.incendo.cloud.CommandTree.attemptParseUnambiguousChild(CommandTree.java:399)
		at org.incendo.cloud.CommandTree.attemptParseUnambiguousChild(CommandTree.java:435)
		at org.incendo.cloud.CommandTree.attemptParseUnambiguousChild(CommandTree.java:435)
		<CommandTree.java:435 repeated a lot>
Caused by: [CIRCULAR REFERENCE: java.lang.StackOverflowError]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions