Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poco::Process::launch (UNIX) - possible memory leak when launching invalid command #2366

Closed
bassdscho opened this issue Jun 13, 2018 · 4 comments · Fixed by #4267
Closed
Assignees

Comments

@bassdscho
Copy link

Steps to reproduce the problem

  • Build this code
#include <Poco/Process.h>
#include <Poco/Pipe.h>

int main()
{
    Poco::Pipe out_pipe;
    if (Poco::Process::launch("some-invalid-command", {}, 0, &out_pipe, 0).wait() == EXIT_SUCCESS) { }
    return 0;
}

  • Check with valgrind, note --trace-children=yes switch

valgrind --leak-check=full --trace-children=yes ./hello_world
==2686== Memcheck, a memory error detector
==2686== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2686== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2686== Command: ./hello_world
==2686==
==2687==
==2687== HEAP SUMMARY:
==2687== in use at exit: 10,627 bytes in 102 blocks
==2687== total heap usage: 113 allocs, 11 frees, 13,043 bytes allocated
==2687==
==2687== 16 bytes in 1 blocks are definitely lost in loss record 3 of 102
==2687== at 0x4C28215: operator new(unsigned long) (vg_replace_malloc.c:334)
==2687== by 0x7B40147: Poco::ProcessImpl::launchByForkExecImpl(std::string const&, std::vector<std::string, std::allocatorstd::string > const&, std::string const&, Poco::Pipe*, Poco::Pipe*, Poco::Pipe*, std::map<std::string, std::string, std::lessstd::string, std::allocator<std::pair<std::string const, std::string> > > const&) (in /home/user/dev/zedboard/build/deps/host/Debug/lib/libPocoFoundation.so.60)
==2687== by 0x7B41648: Poco::Process::launch(std::string const&, std::vector<std::string, std::allocatorstd::string > const&, Poco::Pipe*, Poco::Pipe*, Poco::Pipe*) (in /home/user/dev/zedboard/build/deps/host/Debug/lib/libPocoFoundation.so.60)
==2687== by 0x401D69: main (main.cpp:7)
==2687==
==2687== LEAK SUMMARY:
==2687== definitely lost: 16 bytes in 1 blocks
==2687== indirectly lost: 0 bytes in 0 blocks
==2687== possibly lost: 0 bytes in 0 blocks
==2687== still reachable: 10,611 bytes in 101 blocks
==2687== of which reachable via heuristic:
==2687== stdstring : 3,138 bytes in 87 blocks
==2687== suppressed: 0 bytes in 0 blocks
==2687== Reachable blocks (those to which a pointer was found) are not shown.
==2687== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2687==
==2687== For counts of detected and suppressed errors, rerun with: -v
==2687== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==2686==
==2686== HEAP SUMMARY:
==2686== in use at exit: 0 bytes in 0 blocks
==2686== total heap usage: 116 allocs, 116 frees, 13,852 bytes allocated
==2686==
==2686== All heap blocks were freed -- no leaks are possible
==2686==
==2686== For counts of detected and suppressed errors, rerun with: -v
==2686== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

POCO version

  • 1.7.5

Compiler and version

  • gcc 4.9.2
  • valgrind 3.13.0

Operating system and version

Debian 3.16.0-4-amd64 #1 SMP Debian 3.16.51-3 (2017-12-13) x86_64 GNU/Linux (from uname -a)

@github-actions
Copy link

This issue is stale because it has been open for 365 days with no activity.

@github-actions github-actions bot added the stale label Jun 28, 2022
@aleks-f aleks-f removed the stale label Jun 28, 2022
@aleks-f aleks-f added this to the Release 1.13.0 milestone Jun 28, 2022
@aleks-f
Copy link
Member

aleks-f commented Nov 11, 2023

@pavledragisic please check this

@pavledragisic
Copy link
Member

This issue is real and occurs when an invalid command is sent to the Launch method. I have tested for leaks using the sample code mentioned in this issue, as well as with the Poco Foundation's ProcessTest, where the launch method is used. Leaks occurred in both cases. Leaks do not occur when the command is valid.
I am currently working on finding the source of the issue. @aleks-f

@pavledragisic
Copy link
Member

pavledragisic commented Nov 15, 2023

Memory leak should be fixed in 6382744

Valgrind version: 3.16.1
gcc version: gcc (Debian 10.2.1-6) 10.2.1 20210110
OS: Debian GNU/Linux 11 (bullseye) aarch64

Running valgrind before the fix:

$ valgrind --leak-check=full --trace-children=yes ./Test
==4112494== Memcheck, a memory error detector
==4112494== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4112494== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==4112494== Command: ./Test
==4112494== 
==4112511== 
==4112511== HEAP SUMMARY:
==4112511==     in use at exit: 2,462 bytes in 19 blocks
==4112511==   total heap usage: 31 allocs, 12 frees, 81,143 bytes allocated
==4112511== 
==4112511== 16 bytes in 1 blocks are definitely lost in loss record 3 of 19
==4112511==    at 0x484A484: operator new(unsigned long) (vg_replace_malloc.c:342)
==4112511==    by 0x497537B: Poco::ProcessImpl::launchByForkExecImpl(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Pipe*, Poco::Pipe*, Poco::Pipe*, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&, int) (in /home/pavle/Desktop/Poco/devel/poco/lib/Linux/aarch64/libPocoFoundation.so.100)
==4112511==    by 0x49758B3: Poco::Process::launch(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, Poco::Pipe*, Poco::Pipe*, Poco::Pipe*, int) (in /home/pavle/Desktop/Poco/devel/poco/lib/Linux/aarch64/libPocoFoundation.so.100)
==4112511==    by 0x108CC3: ??? (in /home/pavle/Desktop/Poco/devel/poco/Sample/bin/Linux/aarch64/Test)
==4112511==    by 0x4CC0E17: (below main) (libc-start.c:308)
==4112511== 
==4112511== LEAK SUMMARY:
==4112511==    definitely lost: 16 bytes in 1 blocks
==4112511==    indirectly lost: 0 bytes in 0 blocks
==4112511==      possibly lost: 0 bytes in 0 blocks
==4112511==    still reachable: 2,446 bytes in 18 blocks
==4112511==         suppressed: 0 bytes in 0 blocks
==4112511== Reachable blocks (those to which a pointer was found) are not shown.
==4112511== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==4112511== 
==4112511== For lists of detected and suppressed errors, rerun with: -s
==4112511== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==4112494== 
==4112494== HEAP SUMMARY:
==4112494==     in use at exit: 0 bytes in 0 blocks
==4112494==   total heap usage: 32 allocs, 32 frees, 81,159 bytes allocated
==4112494== 
==4112494== All heap blocks were freed -- no leaks are possible
==4112494== 
==4112494== For lists of detected and suppressed errors, rerun with: -s
==4112494== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

And after the fix:

$ valgrind --leak-check=full --trace-children=yes ./Test
==4116525== Memcheck, a memory error detector
==4116525== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4116525== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==4116525== Command: ./Test
==4116525== 
==4116526== 
==4116526== HEAP SUMMARY:
==4116526==     in use at exit: 2,445 bytes in 17 blocks
==4116526==   total heap usage: 31 allocs, 14 frees, 81,143 bytes allocated
==4116526== 
==4116526== LEAK SUMMARY:
==4116526==    definitely lost: 0 bytes in 0 blocks
==4116526==    indirectly lost: 0 bytes in 0 blocks
==4116526==      possibly lost: 0 bytes in 0 blocks
==4116526==    still reachable: 2,445 bytes in 17 blocks
==4116526==         suppressed: 0 bytes in 0 blocks
==4116526== Reachable blocks (those to which a pointer was found) are not shown.
==4116526== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==4116526== 
==4116526== For lists of detected and suppressed errors, rerun with: -s
==4116526== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==4116525== 
==4116525== HEAP SUMMARY:
==4116525==     in use at exit: 0 bytes in 0 blocks
==4116525==   total heap usage: 32 allocs, 32 frees, 81,159 bytes allocated
==4116525== 
==4116525== All heap blocks were freed -- no leaks are possible
==4116525== 
==4116525== For lists of detected and suppressed errors, rerun with: -s
==4116525== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Executable on which leak was tested:

#include <Poco/Process.h>
#include <Poco/Pipe.h>

int main()
{
	Poco::Pipe out_pipe;
	if (Poco::Process::launch("some-invalid-command", {}, 0, &out_pipe, 0).wait() == EXIT_SUCCESS) { }
	return 0;

	// std::vector<std::string> args;
	// Poco::ProcessHandle proc = Poco::Process::launch("some-invalid-command", args);
	// int rc = Poco::Process::wait(proc);

	return 0;
}

aleks-f pushed a commit that referenced this issue Nov 21, 2023
… command (#4267)

* fix: memory leak in ProcessImpl  #2366

* fix(Process): variable masking and spelling #2366

* feat(ProcessTest): invalid command launch test #2366

* fix(ProcessTest): handle failed launch on win #2366

* fix(ProcessTest): undefined exception #2366

---------

Co-authored-by: Pavle <pavle@debian-gnu-linux-11.localdomain>
aleks-f pushed a commit that referenced this issue Nov 23, 2023
… command (#4267)

* fix: memory leak in ProcessImpl  #2366

* fix(Process): variable masking and spelling #2366

* feat(ProcessTest): invalid command launch test #2366

* fix(ProcessTest): handle failed launch on win #2366

* fix(ProcessTest): undefined exception #2366

---------

Co-authored-by: Pavle <pavle@debian-gnu-linux-11.localdomain>
aleks-f pushed a commit that referenced this issue Nov 27, 2023
… command (#4267)

* fix: memory leak in ProcessImpl  #2366

* fix(Process): variable masking and spelling #2366

* feat(ProcessTest): invalid command launch test #2366

* fix(ProcessTest): handle failed launch on win #2366

* fix(ProcessTest): undefined exception #2366

---------

Co-authored-by: Pavle <pavle@debian-gnu-linux-11.localdomain>
@aleks-f aleks-f added the fixed label Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants