-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
runtime/cgo: C.CString causes index out of range when parameter is too long #53958
Comments
The string is being passed back and forth correctly to Go. The only problem is that the returned string is >1GB in size, which is beyond the limit for |
From https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource
It looks like you're well above that. Maybe |
Looks like |
Hi, @randall77 , thanks for your reply. Yeah the returned string is extremely large. But since C.CString is a primitive for the cgo runtime, we didn't expect it to fail. BTW, what should the users do to raise the limit of CString? |
You can't. It's hardcoded at the moment. I'm working on a fix to remove the limit. |
Change https://go.dev/cl/418557 mentions this issue: |
There's no real reason to limit to 1<<30 bytes. Maybe it would catch some mistakes, but probably ones that would quickly manifest in other ways. We can't use the fancy new unsafe.Slice function because this code may still be generated for people with 1.16 or earlier in their go.mod file. Use unsafe shenanigans instead. Fixes golang#53965 Fixes golang#53958 Change-Id: Ibfa095192f50276091d6c2532e8ccd7832b57ca8 Reviewed-on: https://go-review.googlesource.com/c/go/+/418557 Run-TryBot: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Not sure, only tested on go1.18.3 linux/amd64
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
We tried to build a go function that utilizes a k8s function to parse quantites. Then, we wanted to call the go function we built from C code to test whether it would work. However, we found that when the number we parsed had a very large absolute value (e.g.
-92743e6047801799
), the go function crashed with index out of range. The index out of range happens on the linereturn C.CString(q.AsDec().String())
, we suspect this is caused because the parameter of theC.CString
is too long.reproduce.zip
The Go file and C file we created are as follows. Note that
k8sutil.h
we used was generated by cmd/cgo.Go file
C file
We compiled the code by running the following commands:
go build -buildmode=c-shared -o k8sutil.so k8sutil.go gcc test.c -o test ./k8sutil.so
Then, we ran the program by running
./test
.What did you expect to see?
We expected to see the long string gets returned correctly and the program does not crash.
What did you see instead?
The program crashed with the following error:
panic: runtime error: index out of range [1752834508] with length 1073741824 goroutine 17 [running, locked to thread]: main._Cfunc_CString({0xc000680000, 0x687a21cc}) _cgo_gotypes.go:49 +0xab main.parse(0x0?) /home/hoyhbx/k8sutil.go:18 +0xba Aborted (core dumped)
The text was updated successfully, but these errors were encountered: