File tree Expand file tree Collapse file tree 1 file changed +25
-10
lines changed
internal/users/localentries Expand file tree Collapse file tree 1 file changed +25
-10
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,19 @@ package localentries
7
7
#include <stdlib.h>
8
8
#include <pwd.h>
9
9
#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
+ }
10
23
*/
11
24
import "C"
12
25
@@ -74,15 +87,17 @@ func getGroupEntries() (entries []types.GroupEntry, err error) {
74
87
}
75
88
76
89
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 )
86
101
}
87
- return users
102
+ return out
88
103
}
You can’t perform that action at this time.
0 commit comments