Skip to content

Commit ffc390c

Browse files
committed
Fix potential memory leak issues
1 parent 496e3da commit ffc390c

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

src/mjsonrpc.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,13 @@ static cJSON* rpc_handle_ary_req(const mjrpc_handle_t* handle, const cJSON* requ
189189

190190
cJSON* mjrpc_request_cjson(const char* method, cJSON* params, cJSON* id)
191191
{
192-
if (method == NULL)
193-
return NULL;
194-
195192
cJSON* json = cJSON_CreateObject();
196-
if (json == NULL)
193+
if (json == NULL || method == NULL)
194+
{
195+
cJSON_Delete(params);
196+
cJSON_Delete(id);
197197
return NULL;
198+
}
198199

199200
cJSON_AddStringToObject(json, "jsonrpc", "2.0");
200201
cJSON_AddStringToObject(json, "method", method);
@@ -210,7 +211,7 @@ char* mjrpc_request_str(const char* method, cJSON* params, cJSON* id)
210211
if (json == NULL)
211212
return NULL;
212213

213-
const char* json_str = cJSON_PrintUnformatted(json);
214+
char* json_str = cJSON_PrintUnformatted(json);
214215
cJSON_Delete(json);
215216

216217
return json_str;

src/mjsonrpc.h

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -80,91 +80,91 @@ typedef struct mjrpc_handle
8080
/* --- For client --- */
8181

8282
/**
83-
* @brief build a jsonrpc string request
84-
* @param method name of method
85-
* @param params parameters for the method
86-
* @param id request id, can be NULL for notification
87-
* @return string pointer
83+
* @brief Build a jsonrpc string request
84+
* @param method Name of method
85+
* @param params Parameters for the method, will be released (free)
86+
* @param id Request id, can be NULL for notification, will be released (free)
87+
* @return String pointer (free this after use), if return NULL, id and params will also be released (free)
8888
*/
8989
char* mjrpc_request_str(const char* method, cJSON* params, cJSON* id);
9090

9191
/**
92-
* @brief build a jsonrpc cjson request
93-
* @param method name of method
94-
* @param params parameters for the method
95-
* @param id request id, can be NULL for notification
96-
* @return cjson pointer
92+
* @brief Build a jsonrpc cjson request
93+
* @param method Name of method
94+
* @param params Parameters for the method, will be owned by the request
95+
* @param id Request id, can be NULL for notification, will be owned by the request
96+
* @return cJSON pointer (delete this after use), if return NULL, id and params will be released (free)
9797
*/
9898
cJSON* mjrpc_request_cjson(const char* method, cJSON* params, cJSON* id);
9999

100100

101101
/* --- For server --- */
102102

103103
/**
104-
* @brief build a jsonrpc response with result
105-
* @param result call result
106-
* @param id client request id
107-
* @return cjson pointer
104+
* @brief Build a jsonrpc response with result
105+
* @param result Result of the method
106+
* @param id Client request id
107+
* @return cJSON pointer
108108
*/
109109
cJSON* mjrpc_response_ok(cJSON* result, cJSON* id);
110110

111111
/**
112-
* @brief build a jsonrpc response with error
113-
* @param code error code
114-
* @param message error message
115-
* @param id client request id
116-
* @return cjson pointer
112+
* @brief Build a jsonrpc response with error
113+
* @param code Error code
114+
* @param message Error message
115+
* @param id Client request id
116+
* @return cJSON pointer
117117
*/
118118
cJSON* mjrpc_response_error(int code, char* message, cJSON* id);
119119

120120
/**
121-
* @brief allocate a mjsonrpc handle
122-
* @param initial_capacity initial capacity of builtin hash table, 0 means use default capacity
123-
* @return pointer of mjrpc handle (destroy the handle after use)
121+
* @brief Allocate a mjsonrpc handle
122+
* @param initial_capacity Initial capacity of builtin hash table, 0 means use default capacity
123+
* @return Pointer of mjrpc handle (destroy the handle after use)
124124
*/
125125
mjrpc_handle_t* mjrpc_create_handle(size_t initial_capacity);
126126

127127
/**
128-
* @brief destroy (free) a handle
129-
* @param handle handle to be free
128+
* @brief Destroy (free) a handle
129+
* @param handle Handle to be free
130130
* @return enum mjrpc_error_return
131131
*/
132132
int mjrpc_destroy_handle(mjrpc_handle_t* handle);
133133

134134
/**
135-
* @brief add a method to jsonrpc handle
135+
* @brief Add a method to jsonrpc handle
136136
* @param handle mjrpc handle
137-
* @param function_pointer callback function
138-
* @param method_name method name
139-
* @param arg2func argument to callback function
137+
* @param function_pointer Callback function
138+
* @param method_name Method name
139+
* @param arg2func Argument to callback function
140140
* @return enum mjrpc_error_return
141141
*/
142142
int mjrpc_add_method(mjrpc_handle_t* handle, mjrpc_func function_pointer, const char* method_name,
143143
void* arg2func);
144144

145145
/**
146-
* @brief delete a method from jsonrpc handle
146+
* @brief Delete a method from jsonrpc handle
147147
* @param handle mjrpc handle
148-
* @param method_name method name if NULL, delete all methods
148+
* @param method_name Method name to be deleted
149149
* @return enum mjrpc_error_return
150150
*/
151151
int mjrpc_del_method(mjrpc_handle_t* handle, const char* method_name);
152152

153153
/**
154-
* @brief process a string typed jsonrpc request
154+
* @brief Process a string typed jsonrpc request
155155
* @param handle mjrpc handle
156-
* @param reqeust_str request string
157-
* @param ret_code return code
158-
* @return response string (need to be free)
156+
* @param reqeust_str Request string
157+
* @param ret_code Return code
158+
* @return Response string (need to be free)
159159
*/
160160
char* mjrpc_process_str(mjrpc_handle_t* handle, const char* reqeust_str, int* ret_code);
161161

162162
/**
163-
* @brief process a cjson typed jsonrpc request
163+
* @brief Process a cjson typed jsonrpc request
164164
* @param handle mjrpc handle
165-
* @param request_cjson request cjson
166-
* @param ret_code return code
167-
* @return response cjson pointer (need to be free)
165+
* @param request_cjson Request cjson
166+
* @param ret_code Return code
167+
* @return Response cjson pointer (need to be free)
168168
*/
169169
cJSON* mjrpc_process_cjson(mjrpc_handle_t* handle, const cJSON* request_cjson, int* ret_code);
170170

0 commit comments

Comments
 (0)