Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with std::string::c_str()

36 views
Skip to first unread message

RM

unread,
Sep 11, 2020, 3:11:51 PM9/11/20
to
I have such problem:

string exec_system(string command) {
/* here command == "php
/home/robert/dirtyphp_cpp/build/apps/../../src/_dirtyphp2.php
/home/robert/rozgloszenia/application_edit_obfuscated/config/constants.php
" */
const char *cmd = command.c_str();
/* here problem: cmd == "p" */
array<char, 128> buffer;
string result = ""; // cmd's stdout
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}

I don't understand why cmd == "p". Please help.
I use g++ in Linux Mint (g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0).

Keith Thompson

unread,
Sep 11, 2020, 4:18:44 PM9/11/20
to
I can't reproduce the problem.

How do you know that cmd == "p" at that point? It should be
"php /home/..." (and it is when I run a similar program on my system).

Can you construct a small self-contained program whose output
demonstrates the problem?

If cmd is "p" at that point, then the rest of the function should be
irrelevant, and you should be able to remove it from your test case.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Cholo Lennon

unread,
Sep 11, 2020, 5:45:46 PM9/11/20
to
On 9/11/20 4:11 PM, RM wrote:
> I have such problem:
>
> string exec_system(string command) {
> /* here command == "php
> /home/robert/dirtyphp_cpp/build/apps/../../src/_dirtyphp2.php
> /home/robert/rozgloszenia/application_edit_obfuscated/config/constants.php
> " */
>     const char *cmd = command.c_str();
> /* here problem: cmd == "p" */

What are the values of command[1], command[2], and cmd[2], cmd[3]? (I
assume that command[0] == 'p' and cmd[1] == 0). It's weird that c_str
truncates the string in that way. Maybe 'command' contains a double byte
string...

>     array<char, 128> buffer;
>     string result = ""; // cmd's stdout
>     unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
>     if (!pipe) {
>         throw runtime_error("popen() failed!");
>     }
>     while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
>         result += buffer.data();
>     }
>     return result;
> }
>
> I don't understand why cmd == "p". Please help.
> I use g++ in Linux Mint (g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0).

--
Cholo Lennon
Bs.As.
ARG

Richard Damon

unread,
Sep 11, 2020, 7:35:40 PM9/11/20
to
cmd CAN'T be "p", as cmd is a pointer so it is effectively some sort of
address.

*p will be 'p', because that is the character that p points to. It could
also be called p[0].

p[1] would be 'h', etc.

p should point to "php ..." if you treat is as a pointer into a
character array (aka c-string).

RM

unread,
Sep 11, 2020, 11:42:12 PM9/11/20
to

> cmd CAN'T be "p", as cmd is a pointer so it is effectively some sort of
> address.
>
> *p will be 'p', because that is the character that p points to. It could
> also be called p[0].
>
> p[1] would be 'h', etc.
>
> p should point to "php ..." if you treat is as a pointer into a
> character array (aka c-string).
>

Sorry, my mistake, I watched *cmd in VSCode.
First call of my fgets() returns nullptr although identical command cmd
executed from bash prompt returns some ASCII text.

RM

unread,
Sep 12, 2020, 1:11:14 AM9/12/20
to
W dniu 12.09.2020 o 05:41, RM pisze:
> First call of my fgets() returns nullptr although identical command cmd
> executed from bash prompt returns some ASCII text.

I don't have any file opened in any application that time.

RM

unread,
Sep 12, 2020, 2:32:02 AM9/12/20
to
The source code of my exec_system() function is taken from popular
developers' website.

Jorgen Grahn

unread,
Sep 12, 2020, 3:13:27 AM9/12/20
to
On Sat, 2020-09-12, RM wrote:
> The source code of my exec_system() function is taken from popular
> developers' website.

Then it's a good idea to write

/* Taken from http://example.org/some/where/system.html
*/
void exec_system(string cmd)
...

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

RM

unread,
Sep 12, 2020, 6:36:18 AM9/12/20
to
W dniu 12.09.2020 o 09:13, Jorgen Grahn pisze:
> On Sat, 2020-09-12, RM wrote:
>> The source code of my exec_system() function is taken from popular
>> developers' website.
>
> Then it's a good idea to write
>
> /* Taken from http://example.org/some/where/system.html
> */
> void exec_system(string cmd)
> ...
>
> /Jorgen
>
Fine.
I had logical error in called PHP script
/home/robert/dirtyphp_cpp/build/apps/../../src/_dirtyphp2.php - that is
why fgets() received empty output. Sorry for disturbing your attention.

Jorgen Grahn

unread,
Sep 12, 2020, 11:35:02 AM9/12/20
to
Ok, fine, but my tip was unrelated to that.
0 new messages