Hacker News new | ask | show | jobs
by dgfitz 729 days ago
Pretty sure argc is 1 in your example, the name of the binary, no?

Edit: argv is the name, argc will be 1

3 comments

Yes. It's possible to get `argc` to equal zero, though by invoking the program using `execve(prog, {NULL}, {NULL})` on Linux. This has, rather famously, caused at least one out-of-bounds error in a security-critical program (CVE-2021-4034 "Pwnkit", LPE by invoking Polkit's pkexec with a zero-length argv).
Didn’t even think about that. Very good point.
It's possible to call programs without any arguments, not even the path to the binary. I believe passing the path to the binary is merely a shell convention, because when calling binaries directly from code (not through the shell), sometimes it's possible to forget to specify arg 0 (if your chosen abstraction doesn't provide it automatically). I bet this has caused tons of confusion for people.
It is fully possible to launch a program with argc = 0. But yes, replace argc with (argc - 1) in the example to match the typical case.