Skip to content

Commit 29112b0

Browse files
committed
Fix misaligned pointer conversion
With Go 1.25, `go test -race` failed with: fatal error: checkptr: misaligned pointer conversion goroutine 72 gp=0xc0002281c0 m=8 mp=0xc00018a008 [running]: runtime.throw({0x16ece45?, 0x0?}) /home/user/go/pkg/mod/golang.org/[email protected]/src/runtime/panic.go:1094 +0x48 fp=0xc0002f7150 sp=0xc0002f7120 pc=0x489cc8 runtime.checkptrAlignment(0xc0005fec1e, 0x1586820, 0x1) /home/user/go/pkg/mod/golang.org/[email protected]/src/runtime/checkptr.go:20 +0x9a fp=0xc0002f7170 sp=0xc0002f7150 pc=0x41b8da github.com/ubuntu/authd/internal/users/localentries.strvToSlice(0xc0005fec1e)
1 parent b47ea6f commit 29112b0

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

internal/users/localentries/getgrent_c.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ package localentries
77
#include <stdlib.h>
88
#include <pwd.h>
99
#include <grp.h>
10+
11+
// Copy a NULL-terminated char** into a new array and return length.
12+
char **copy_strv(char **strv, int *out_len) {
13+
int n = 0;
14+
while (strv && strv[n]) n++;
15+
16+
*out_len = n;
17+
char **out = calloc(n + 1, sizeof(char*));
18+
for (int i = 0; i < n; i++) {
19+
out[i] = strv[i];
20+
}
21+
return out;
22+
}
1023
*/
1124
import "C"
1225

@@ -74,15 +87,17 @@ func getGroupEntries() (entries []types.GroupEntry, err error) {
7487
}
7588

7689
func strvToSlice(strv **C.char) []string {
77-
var users []string
78-
for i := C.uint(0); ; i++ {
79-
s := *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(strv)) +
80-
uintptr(i)*unsafe.Sizeof(*strv)))
81-
if s == nil {
82-
break
83-
}
84-
85-
users = append(users, C.GoString(s))
90+
if strv == nil {
91+
return nil
92+
}
93+
var n C.int
94+
tmp := C.copy_strv(strv, &n)
95+
defer C.free(unsafe.Pointer(tmp))
96+
97+
out := make([]string, int(n))
98+
for i := 0; i < int(n); i++ {
99+
p := *(**C.char)(unsafe.Add(unsafe.Pointer(tmp), uintptr(i)*unsafe.Sizeof(*tmp)))
100+
out[i] = C.GoString(p)
86101
}
87-
return users
102+
return out
88103
}

0 commit comments

Comments
 (0)