Skip to content

Commit 4bf732f

Browse files
committed
fixup! Fix misaligned pointer conversion
1 parent ee22e7d commit 4bf732f

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

internal/users/localentries/getgrent_c.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@ package localentries
88
#include <pwd.h>
99
#include <grp.h>
1010
11-
// Copy a NULL-terminated char** into a new array and return length.
12-
char **copy_strv(char **strv, int *out_len) {
11+
// Return the length of a NULL-terminated char** array.
12+
int strv_len(char **strv) {
1313
int n = 0;
1414
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;
15+
return n;
2216
}
17+
2318
*/
2419
import "C"
2520

@@ -90,13 +85,11 @@ func strvToSlice(strv **C.char) []string {
9085
if strv == nil {
9186
return nil
9287
}
93-
var n C.int
94-
tmp := C.copy_strv(strv, &n)
95-
defer C.free(unsafe.Pointer(tmp))
88+
n := C.strv_len(strv)
9689

9790
out := make([]string, int(n))
9891
for i := 0; i < int(n); i++ {
99-
p := *(**C.char)(unsafe.Add(unsafe.Pointer(tmp), uintptr(i)*unsafe.Sizeof(*tmp)))
92+
p := *(**C.char)(unsafe.Add(unsafe.Pointer(strv), uintptr(i)*unsafe.Sizeof(*strv)))
10093
out[i] = C.GoString(p)
10194
}
10295
return out

0 commit comments

Comments
 (0)