This is my answer, in my case it solved my need.
Edit: I was wrong, this solution is not valid. Read @Hasturkun comment.
I have a program that asks the user to enter a password through the prompt:
./my_program
Password:
I write a password, for example "abcd1234" and press enter, the program ends successfully. Ok, but ... how can I do a memory check of my program using Valgrind, and the prompt doesn't wait for me to enter a password and press enter?
I simply put echo "fake-password"
with a pipe before the exec of my program, like this:
valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all -v echo "fake-password" | ./my_program > /dev/null
In this way, the program will not ask the user to enter any data at the prompt and the string "fake-password" will be sent by the stdIn to the program in the same way as if you had written it and pressed Enter.
This is the result:
==907664== Memcheck, a memory error detector
==907664== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==907664== Using Valgrind-3.15.0-608cb11914-20190413 and LibVEX; rerun with -h for copyright info
==907664== Command: echo fake-password
==907664==
--907664-- Valgrind options:
--907664-- --tool=memcheck
--907664-- --leak-check=full
--907664-- --show-leak-kinds=all
--907664-- -v
...
...
--907664-- REDIR: 0x49f2650 (libc.so.6:__mempcpy_avx_unaligned_erms) redirected to 0x4843660 (mempcpy)
--907664-- REDIR: 0x49f2670 (libc.so.6:__memcpy_avx_unaligned_erms) redirected to 0x48429f0 (memmove)
--907664-- REDIR: 0x4901850 (libc.so.6:free) redirected to 0x483c9d0 (free)
==907664==
==907664== HEAP SUMMARY:
==907664== in use at exit: 0 bytes in 0 blocks
==907664== total heap usage: 31 allocs, 31 frees, 8,161 bytes allocated
==907664==
==907664== All heap blocks were freed -- no leaks are possible
==907664==
==907664== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
I hope it works for you, it worked for me.