-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
flag: index out of range when argc is 0 #32775
Comments
Not sure there's anything to fix. That binary compiles and runs fine; it's the caller that's making a mistake: argv should never be empty. |
From execve(2) man page: argv is an array of argument strings passed to the new program. By convention, the first of these strings should contain the filename associated with the file being executed On Linux, argv can be specified as NULL, which has the same effect as So, I'am also not sure if we need to fix something. |
I've spotted this issue when my binary was ran by rsyslog. |
What config was used for Something like this:
|
yep, binary was invoked by |
It was fixed in latest versions of rsyslog apparently:
|
I searched Github and found another examples of |
It seems to me that the C But I guess if someone sends a patch for 1.14 we can take a look. |
|
In the initialization of |
Change https://golang.org/cl/184637 mentions this issue: |
Thank you for filing this issue @Kneht and for the patience and everyone for chiming in. Welcome to the Go project! I agree with what @robpike and @ianlancetaylor say: I don't think we should do anything here. You are incorrectly using PosixThe all encompassing standard says Linuxhttp://man7.org/linux/man-pages/man2/execve.2.html Windowshttps://docs.microsoft.com/en-us/cpp/c-runtime-library/exec-wexec-functions?view=vs-2019 Darwinhttps://man.openbsd.org/execve.2 FreeBSDhttps://www.freebsd.org/cgi/man.cgi?query=execve&sektion=2 and running a C variant on my MacbookPro computer #include <unistd.h>
int main(int argc, char **argv) {
getopt(0, NULL, NULL);
} produces a SEGMENTATION FAULT as per $ gcc call.c -o call && ./call
Segmentation fault: 11 Thus I shall close this issue, but nonetheless thank you for filing it, for the discourse, patience and for the CL. Looking forward to continuing to interact more with you on Go. |
Hi @odeke-em, thank you for complete reply. Based on links you've posted, standard and Linux documentation says that argv[0] should contain the filename, which sounds like recommendation to me. Obviously you can check in evecve documentation for every OS and run any tests on personal computer of your choice, but it doesn't matter much as more then 50% of golang users are on Linux, at least based on survey 2018. Given that fix has been implemented by Max and pre approved by Ian I guess it's up to them if this change needs to be merged. Thanks for your contributions @odeke-em . |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
When golang compiled binary invoked by execve with 0 arguments like this:
test.cpp
codehttps://play.golang.org/p/zo3YJZTkBu_A
What did you expect to see?
nothing
What did you see instead?
panic: runtime error: index out of range
goroutine 1 [running]:
flag.init.ializers()
/usr/lib/go-1.12/src/flag/flag.go:1009 +0x172
As for documentation, it's sort of guaranteed that os.Args[0] is a program name:
https://golang.org/pkg/os/#pkg-variables
When argc is 0, at least for Linux platform program name can be fetched from procfs(/proc/self/comm), probably here:
go/src/runtime/runtime1.go
Lines 70 to 73 in 0a7bc8f
The text was updated successfully, but these errors were encountered: