detached 1bb4460

purego: fix darwin int string mixing (#357)

Add test case that verifies multiple string arguments passed on the stack
each receive their correct distinct values, rather than all receiving the
same duplicated value. This test catches the bug fixed in the previous
commit where v.String() was incorrectly used instead of val.String().

Closes #356
commit
1bb446066e99572ba46bf3db0a3cfe2bb3cda976
author
Travis Cline · · 2025-10-29 14:44 UTC
committer
GitHub · · 2025-10-29 14:44 UTC
parent
4956991
signature
GPG UnverifiedB5690EEEBB952194 The key is unknown
3 files · +17 -1
M func.go +1 -1
@@ -287,7 +287,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
287287
288288 for j, val := range args[i:] {
289289 if val.Kind() == reflect.String {
290- ptr := strings.CString(v.String())
290+ ptr := strings.CString(val.String())
291291 keepAlive = append(keepAlive, ptr)
292292 val = reflect.ValueOf(ptr)
293293 args[i+j] = val
@@ -287,7 +287,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
287 287
288 for j, val := range args[i:] {288 for j, val := range args[i:] {
289 if val.Kind() == reflect.String {289 if val.Kind() == reflect.String {
290- ptr := strings.CString(v.String())290+ ptr := strings.CString(val.String())
291 keepAlive = append(keepAlive, ptr)291 keepAlive = append(keepAlive, ptr)
292 val = reflect.ValueOf(ptr)292 val = reflect.ValueOf(ptr)
293 args[i+j] = val293 args[i+j] = val
M func_test.go +12 -0
@@ -185,6 +185,18 @@ func TestABI(t *testing.T) {
185185 t.Fatalf("%s: got %d, want %d", cName, res, expect)
186186 }
187187 }
188+ {
189+ const cName = "stack_8i32_3strings"
190+ var fn func(*byte, uintptr, int32, int32, int32, int32, int32, int32, int32, int32, string, string, string)
191+ purego.RegisterLibFunc(&fn, lib, cName)
192+ buf := make([]byte, 256)
193+ fn(&buf[0], uintptr(len(buf)), 1, 2, 3, 4, 5, 6, 7, 8, "foo", "bar", "baz")
194+ res := string(buf[:strings.IndexByte(string(buf), 0)])
195+ const want = "1:2:3:4:5:6:7:8:foo:bar:baz"
196+ if res != want {
197+ t.Fatalf("%s: got %q, want %q", cName, res, want)
198+ }
199+ }
188200 }
189201
190202 func buildSharedLib(compilerEnv, libFile string, sources ...string) error {
@@ -185,6 +185,18 @@ func TestABI(t *testing.T) {
185 t.Fatalf("%s: got %d, want %d", cName, res, expect)185 t.Fatalf("%s: got %d, want %d", cName, res, expect)
186 }186 }
187 }187 }
188+ {
189+ const cName = "stack_8i32_3strings"
190+ var fn func(*byte, uintptr, int32, int32, int32, int32, int32, int32, int32, int32, string, string, string)
191+ purego.RegisterLibFunc(&fn, lib, cName)
192+ buf := make([]byte, 256)
193+ fn(&buf[0], uintptr(len(buf)), 1, 2, 3, 4, 5, 6, 7, 8, "foo", "bar", "baz")
194+ res := string(buf[:strings.IndexByte(string(buf), 0)])
195+ const want = "1:2:3:4:5:6:7:8:foo:bar:baz"
196+ if res != want {
197+ t.Fatalf("%s: got %q, want %q", cName, res, want)
198+ }
199+ }
188 }200 }
189 201
190 func buildSharedLib(compilerEnv, libFile string, sources ...string) error {202 func buildSharedLib(compilerEnv, libFile string, sources ...string) error {
M testdata/abitest/abi_test.c +4 -0
@@ -25,3 +25,7 @@ uint32_t stack_string(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e
2525 assert(strcmp(i, "test") == 0);
2626 return a | b | c | d | e | f | g | h;
2727 }
28+
29+void stack_8i32_3strings(char* result, size_t size, int32_t a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5, int32_t a6, int32_t a7, int32_t a8, const char* s1, const char* s2, const char* s3) {
30+ snprintf(result, size, "%d:%d:%d:%d:%d:%d:%d:%d:%s:%s:%s", a1, a2, a3, a4, a5, a6, a7, a8, s1, s2, s3);
31+}
@@ -25,3 +25,7 @@ uint32_t stack_string(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e
25 assert(strcmp(i, "test") == 0);25 assert(strcmp(i, "test") == 0);
26 return a | b | c | d | e | f | g | h;26 return a | b | c | d | e | f | g | h;
27 }27 }
28+
29+void stack_8i32_3strings(char* result, size_t size, int32_t a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5, int32_t a6, int32_t a7, int32_t a8, const char* s1, const char* s2, const char* s3) {
30+ snprintf(result, size, "%d:%d:%d:%d:%d:%d:%d:%d:%s:%s:%s", a1, a2, a3, a4, a5, a6, a7, a8, s1, s2, s3);
31+}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close