diff options
Diffstat (limited to '')
-rw-r--r-- | src/json.c | 16 | ||||
-rw-r--r-- | src/json.h | 2 | ||||
-rw-r--r-- | src/net.c | 24 | ||||
-rw-r--r-- | src/net.h | 2 |
4 files changed, 9 insertions, 35 deletions
@@ -17,21 +17,14 @@ #include <stdlib.h> #include <string.h> -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; @@ -19,7 +19,7 @@ log_err("JSON: %s failed\n", fn); \ } while (0) -char *json_to_string(struct json_object *); +const char *json_to_string(struct json_object *); struct json_object *json_from_string(const char *); int json_clone(const struct json_object *, const char *key, struct json_object **value); @@ -210,35 +210,22 @@ int net_recv(int fd, void *buf, size_t size) struct buf { uint32_t size; - void *data; + const void *data; }; int buf_create(struct buf **_buf, const void *data, uint32_t size) { - int ret = 0; - struct buf *buf = malloc(sizeof(struct buf)); if (!buf) { log_errno("malloc"); return -1; } - buf->data = malloc(size); - if (!buf->data) { - log_errno("malloc"); - goto free; - } - + buf->data = data; buf->size = size; - memcpy(buf->data, data, size); *_buf = buf; - return ret; - -free: - free(buf); - - return ret; + return 0; } int buf_create_from_string(struct buf **buf, const char *str) @@ -248,7 +235,6 @@ int buf_create_from_string(struct buf **buf, const char *str) void buf_destroy(struct buf *buf) { - free(buf->data); free(buf); } @@ -257,7 +243,7 @@ uint32_t buf_get_size(const struct buf *buf) return buf->size; } -void *buf_get_data(const struct buf *buf) +const void *buf_get_data(const struct buf *buf) { return buf->data; } @@ -306,8 +292,6 @@ int net_recv_buf(int fd, struct buf **buf) if (ret < 0) goto free_data; - free(data); - return ret; free_data: @@ -26,7 +26,7 @@ int buf_create_from_string(struct buf **, const char *); void buf_destroy(struct buf *); uint32_t buf_get_size(const struct buf *); -void *buf_get_data(const struct buf *); +const void *buf_get_data(const struct buf *); int net_send_buf(int fd, const struct buf *); int net_recv_buf(int fd, struct buf **); |