diff --git a/src/shellmatta.c b/src/shellmatta.c index 2caa763..9cd1ae0 100644 --- a/src/shellmatta.c +++ b/src/shellmatta.c @@ -486,9 +486,16 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle, inst->stdinIdx ++; } /** -# calculate length and terminate stdin string */ - inst->stdinLength = inst->lastNewlineIdx - inst->stdinIdx; - inst->buffer[inst->lastNewlineIdx] = '\0'; - + if(inst->stdinIdx < inst->lastNewlineIdx) + { + inst->stdinLength = inst->lastNewlineIdx - inst->stdinIdx; + inst->buffer[inst->lastNewlineIdx] = '\0'; + } + else + { + inst->stdinLength = 0u; + } + /** -# calculate length and terminate argument string */ inst->inputCount = inst->hereStartIdx; inst->buffer[inst->hereStartIdx] = '\0'; diff --git a/test/integrationtest/test_integration.cpp b/test/integrationtest/test_integration.cpp index 7e039e9..969a453 100644 --- a/test/integrationtest/test_integration.cpp +++ b/test/integrationtest/test_integration.cpp @@ -175,6 +175,38 @@ TEST_CASE( "shellmatta heredoc test" ) { REQUIRE( strcmp(dummyData, doSomethingArguments) == 0); } +TEST_CASE( "shellmatta heredoc test empty" ) { + + shellmatta_instance_t inst; + shellmatta_handle_t handle; + char buffer[1024]; + char historyBuffer[1024]; + char *dummyData = (char*)"do this "; + + shellmatta_doInit( &inst, + &handle, + buffer, + sizeof(buffer), + historyBuffer, + sizeof(historyBuffer), + "shellmatta->", + NULL, + writeFct); + shellmatta_addCmd(handle, &doSomethingCmd); + + doSomethingArguments = NULL; + doSomethingLength = 0u; + + shellmatta_processData(handle, (char*)"do this << EOF\r\n" + "EOF\r\n" + , 21); + + CHECK( doSomethingStdinLength == 0u); + CHECK( NULL == doSomethingStdin ); + CHECK( doSomethingLength == 8u); + REQUIRE( strcmp(dummyData, doSomethingArguments) == 0); +} + TEST_CASE( "shellmatta remove function" ) { shellmatta_instance_t inst;