Skip to content

Commit

Permalink
Merge pull request #445 from lbcjbb/fix-crash
Browse files Browse the repository at this point in the history
fix: don't use defer in a loop
  • Loading branch information
mefellows authored Jul 15, 2024
2 parents 778a111 + ac7f9cf commit 4d46c69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
16 changes: 7 additions & 9 deletions internal/native/message_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,18 @@ func (m *Message) GivenWithParameter(state string, params map[string]interface{}
defer free(cState)

if len(params) == 0 {
cState := C.CString(state)
defer free(cState)

C.pactffi_given(m.handle, cState)
} else {
for k, v := range params {
cKey := C.CString(k)
defer free(cKey)

param := stringFromInterface(v)
cValue := C.CString(param)
defer free(cValue)

C.pactffi_given_with_param(m.handle, cState, cKey, cValue)

free(cValue)
free(cKey)
}
}

Expand All @@ -152,18 +150,18 @@ func (m *Message) ExpectsToReceive(description string) *Message {

func (m *Message) WithMetadata(valueOrMatcher map[string]string) *Message {
for k, v := range valueOrMatcher {

cName := C.CString(k)
defer free(cName)

// TODO: check if matching rules allowed here
// value := stringFromInterface(v)
// fmt.Printf("withheaders, sending: %+v \n\n", value)
// cValue := C.CString(value)
cValue := C.CString(v)
defer free(cValue)

C.pactffi_message_with_metadata(m.handle, cName, cValue)

free(cValue)
free(cName)
}

return m
Expand Down Expand Up @@ -237,7 +235,7 @@ func (m *MessageServer) UsingPlugin(pluginName string, pluginVersion string) err
defer free(cPluginName)
cPluginVersion := C.CString(pluginVersion)
defer free(cPluginVersion)

r := C.pactffi_using_plugin(m.messagePact.handle, cPluginName, cPluginVersion)
InstallSignalHandlers()

Expand Down
17 changes: 9 additions & 8 deletions internal/native/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,13 @@ func (i *Interaction) GivenWithParameter(state string, params map[string]interfa

for k, v := range params {
cKey := C.CString(k)
defer free(cKey)
param := stringFromInterface(v)
cValue := C.CString(param)
defer free(cValue)

C.pactffi_given_with_param(i.handle, cState, cKey, cValue)

free(cValue)
free(cKey)
}

return i
Expand Down Expand Up @@ -535,36 +535,37 @@ func (i *Interaction) WithResponseHeaders(valueOrMatcher map[string][]interface{

func (i *Interaction) withHeaders(part interactionPart, valueOrMatcher map[string][]interface{}) *Interaction {
for k, v := range valueOrMatcher {

cName := C.CString(k)
defer free(cName)

for _, header := range v {
value := stringFromInterface(header)
cValue := C.CString(value)
defer free(cValue)

C.pactffi_with_header_v2(i.handle, C.int(part), cName, C.ulong(0), cValue)

free(cValue)
}

free(cName)
}

return i
}

func (i *Interaction) WithQuery(valueOrMatcher map[string][]interface{}) *Interaction {
for k, values := range valueOrMatcher {

cName := C.CString(k)
defer free(cName)

for idx, v := range values {
value := stringFromInterface(v)
cValue := C.CString(value)
defer free(cValue)

C.pactffi_with_query_parameter_v2(i.handle, cName, C.ulong(idx), cValue)

free(cValue)
}

free(cName)
}

return i
Expand Down

0 comments on commit 4d46c69

Please sign in to comment.