From b0963cf8e2b31ac6a60c314bad148cd93d8eaca1 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 18 Jul 2023 01:59:39 +0200 Subject: net: don't copy data in struct buf --- src/json.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) (limited to 'src/json.c') diff --git a/src/json.c b/src/json.c index 3f22b39..10b34a9 100644 --- a/src/json.c +++ b/src/json.c @@ -17,21 +17,14 @@ #include #include -char *json_to_string(struct json_object *obj) +const char *json_to_string(struct json_object *obj) { const char *result = json_object_to_json_string(obj); if (!result) { json_errno("json_object_to_json_string"); return NULL; } - - char *_result = strdup(result); - if (!_result) { - log_errno("strdup"); - return NULL; - } - - return _result; + return result; } struct json_object *json_from_string(const char *src) @@ -76,15 +69,11 @@ int json_send(struct json_object *obj, int fd) struct buf *buf = NULL; ret = buf_create_from_string(&buf, str); - free((char *)str); if (ret < 0) return ret; ret = net_send_buf(fd, buf); buf_destroy(buf); - if (ret < 0) - return ret; - return ret; } @@ -103,6 +92,7 @@ struct json_object *json_recv(int fd) goto destroy_buf; destroy_buf: + free((void *)buf_get_data(buf)); buf_destroy(buf); return result; -- cgit v1.3