Skip to content

Commit 7566a1e

Browse files
committed
implemented freeing memory in secretservice and made minor edits to osxkeychain
Signed-off-by: Avi Vaid <[email protected]>
1 parent 72661b3 commit 7566a1e

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

osxkeychain/osxkeychain_darwin.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ package osxkeychain
1010
import "C"
1111
import (
1212
"errors"
13+
"github.com/docker/docker-credential-helpers/credentials"
1314
"net/url"
1415
"strconv"
1516
"strings"
1617
"unsafe"
17-
"github.com/docker/docker-credential-helpers/credentials"
1818
)
1919

2020
// errCredentialsNotFound is the specific error message returned by OS X
@@ -94,28 +94,28 @@ func (h Osxkeychain) Get(serverURL string) (string, string, error) {
9494
return user, pass, nil
9595
}
9696

97-
func (h Osxkeychain) List() ([]string, []string, error){
98-
var pathsC** C.char
97+
func (h Osxkeychain) List() ([]string, []string, error) {
98+
var pathsC **C.char
9999
defer C.free(unsafe.Pointer(pathsC))
100-
var acctsC** C.char
100+
var acctsC **C.char
101101
defer C.free(unsafe.Pointer(acctsC))
102102
var listLenC C.uint
103103
errMsg := C.keychain_list(&pathsC, &acctsC, &listLenC)
104-
if errMsg!=nil {
104+
if errMsg != nil {
105105
defer C.free(unsafe.Pointer(errMsg))
106106
goMsg := C.GoString(errMsg)
107107
return nil, nil, errors.New(goMsg)
108108
}
109-
var listLen int;
109+
var listLen int
110110
listLen = int(listLenC)
111111
pathTmp := (*[1 << 30]*C.char)(unsafe.Pointer(pathsC))[:listLen:listLen]
112112
acctTmp := (*[1 << 30]*C.char)(unsafe.Pointer(acctsC))[:listLen:listLen]
113113
//taking the array of c strings into go while ignoring all the stuff irrelevant to credentials-helper
114114
paths := make([]string, listLen)
115115
accts := make([]string, listLen)
116116
at := 0
117-
for i := 0; i < listLen ; i++ {
118-
if C.GoString(pathTmp[i])=="0" {
117+
for i := 0; i < listLen; i++ {
118+
if C.GoString(pathTmp[i]) == "0" {
119119
continue
120120
}
121121
paths[at] = C.GoString(pathTmp[i])
@@ -124,9 +124,8 @@ func (h Osxkeychain) List() ([]string, []string, error){
124124
}
125125
paths = paths[:at]
126126
accts = accts[:at]
127-
//still need to free all the memory we allocated in the c file
128-
//do it here >>
129127
C.freeListData(&pathsC, listLenC)
128+
C.freeListData(&acctsC, listLenC)
130129
return paths, accts, nil
131130
}
132131

@@ -164,4 +163,3 @@ func freeServer(s *C.struct_Server) {
164163
C.free(unsafe.Pointer(s.host))
165164
C.free(unsafe.Pointer(s.path))
166165
}
167-

secretservice/secretservice_linux.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <string.h>
2+
#include <stdlib.h>
23
#include "secretservice_linux.h"
34

45
const SecretSchema *docker_get_schema(void)
@@ -137,3 +138,10 @@ GError *list(char *** paths, char *** accts, unsigned int *list_l) {
137138
*list_l = numKeys;
138139
return NULL;
139140
}
141+
142+
void freeListData(char *** data, unsigned int length) {
143+
for(int i=0; i<length; i++) {
144+
free((*data)[i]);
145+
}
146+
free(*data);
147+
}

secretservice/secretservice_linux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,7 @@ func (h Secretservice) List() ([]string, []string, error) {
9999
paths[i] = C.GoString(pathTmp[i])
100100
accts[i] = C.GoString(acctTmp[i])
101101
}
102+
C.freeListData(&pathsC, listLenC)
103+
C.freeListData(&acctsC, listLenC)
102104
return paths, accts, nil
103105
}

secretservice/secretservice_linux.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ GError *add(char *server, char *username, char *secret);
1010
GError *delete(char *server);
1111
GError *get(char *server, char **username, char **secret);
1212
GError *list(char *** paths, char *** accts, unsigned int *list_l);
13+
void freeListData(char *** data, unsigned int length);

0 commit comments

Comments
 (0)