Skip to content

Commit 2d9b52a

Browse files
committed
Fix: Free memory
1 parent 6ae7f98 commit 2d9b52a

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

sqlite3_opt_userauth.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ _sqlite3_user_delete(sqlite3* db, const char* zUsername)
4242
}
4343
*/
4444
import "C"
45+
import (
46+
"unsafe"
47+
)
4548

4649
const (
4750
SQLITE_AUTH = C.SQLITE_AUTH
@@ -61,7 +64,17 @@ const (
6164
// If the SQLITE_USER table is not present in the database file, then
6265
// this interface is a harmless no-op returnning SQLITE_OK.
6366
func (c *SQLiteConn) Authenticate(username, password string) error {
64-
rv := C._sqlite3_user_authenticate(c.db, C.CString(username), C.CString(password), C.int(len(password)))
67+
// Allocate C Variables
68+
cuser := C.CString(username)
69+
cpass := C.CString(password)
70+
71+
// Free C Variables
72+
defer func() {
73+
C.free(unsafe.Pointer(cuser))
74+
C.free(unsafe.Pointer(cpass))
75+
}()
76+
77+
rv := C._sqlite3_user_authenticate(c.db, cuser, cpass, C.int(len(password)))
6578
if rv != C.SQLITE_OK {
6679
return c.lastError()
6780
}
@@ -83,7 +96,17 @@ func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error {
8396
isAdmin = 1
8497
}
8598

86-
rv := C._sqlite3_user_add(c.db, C.CString(username), C.CString(password), C.int(len(password)), C.int(isAdmin))
99+
// Allocate C Variables
100+
cuser := C.CString(username)
101+
cpass := C.CString(password)
102+
103+
// Free C Variables
104+
defer func() {
105+
C.free(unsafe.Pointer(cuser))
106+
C.free(unsafe.Pointer(cpass))
107+
}()
108+
109+
rv := C._sqlite3_user_add(c.db, cuser, cpass, C.int(len(password)), C.int(isAdmin))
87110
if rv != C.SQLITE_OK {
88111
return c.lastError()
89112
}
@@ -102,7 +125,17 @@ func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error
102125
isAdmin = 1
103126
}
104127

105-
rv := C._sqlite3_user_change(c.db, C.CString(username), C.CString(password), C.int(len(password)), C.int(isAdmin))
128+
// Allocate C Variables
129+
cuser := C.CString(username)
130+
cpass := C.CString(password)
131+
132+
// Free C Variables
133+
defer func() {
134+
C.free(unsafe.Pointer(cuser))
135+
C.free(unsafe.Pointer(cpass))
136+
}()
137+
138+
rv := C._sqlite3_user_change(c.db, cuser, cpass, C.int(len(password)), C.int(isAdmin))
106139
if rv != C.SQLITE_OK {
107140
return c.lastError()
108141
}
@@ -116,7 +149,15 @@ func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error
116149
// the database cannot be converted into a no-authentication-required
117150
// database.
118151
func (c *SQLiteConn) AuthUserDelete(username string) error {
119-
rv := C._sqlite3_user_delete(c.db, C.CString(username))
152+
// Allocate C Variables
153+
cuser := C.CString(username)
154+
155+
// Free C Variables
156+
defer func() {
157+
C.free(unsafe.Pointer(cuser))
158+
}()
159+
160+
rv := C._sqlite3_user_delete(c.db, cuser)
120161
if rv != C.SQLITE_OK {
121162
return c.lastError()
122163
}

0 commit comments

Comments
 (0)