diff --git a/plugins/restful/license_handle.c b/plugins/restful/license_handle.c index c07f5c63..bb92a076 100644 --- a/plugins/restful/license_handle.c +++ b/plugins/restful/license_handle.c @@ -26,6 +26,7 @@ #include "errcodes.h" #include "parser/neu_json_license.h" #include "plugin.h" +#include "utils/asprintf.h" #include "utils/log.h" #include "json/neu_json_error.h" #include "json/neu_json_fn.h" @@ -41,13 +42,9 @@ int get_plugin_names(const license_t *lic, UT_array *plugin_names); static inline char *get_license_path() { - size_t n = 1 + snprintf(NULL, 0, "%s/%s", g_config_dir, LICENSE_FNAME); - char * p = malloc(n); - if (NULL == p) { - return NULL; - } - snprintf(p, n, "%s/%s", g_config_dir, LICENSE_FNAME); - return p; + char *s = NULL; + neu_asprintf(&s, "%s/%s", g_config_dir, LICENSE_FNAME); + return s; } void handle_get_license(nng_aio *aio) @@ -149,26 +146,22 @@ static inline bool file_exists(const char *const path) static int set_license(const char *lic_str) { - int rv = 0; - size_t len = 0; - const char *suffix = ".tmp"; - char * license_path = NULL; - char * fname_tmp = NULL; - FILE * fp = NULL; - license_t new_lic = {}; + int rv = 0; + size_t len = 0; + char * license_path = NULL; + char * fname_tmp = NULL; + FILE * fp = NULL; + license_t new_lic = {}; license_path = get_license_path(); if (NULL == license_path) { return NEU_ERR_EINTERNAL; } - len = strlen(license_path) + strlen(suffix) + 1; - fname_tmp = malloc(len); - if (NULL == fname_tmp) { + if (0 > neu_asprintf(&fname_tmp, "%s.tmp", license_path)) { rv = NEU_ERR_EINTERNAL; goto final; } - snprintf(fname_tmp, len, "%s%s", license_path, suffix); len = strlen(lic_str); if (len > 15000) { @@ -249,7 +242,7 @@ int backup_license_file(const char *lic_fname) return NEU_ERR_EINTERNAL; } - size_t n = snprintf(fname_bak, len, "%s.bak", lic_fname); + size_t n = snprintf(fname_bak, len, "%s%s", lic_fname, suffix); gmtime_r(&now, &tm); if (0 == strftime(fname_bak + n, len - n, ".%Y%m%d%H%M%S", &tm)) { diff --git a/plugins/restful/normal_handle.c b/plugins/restful/normal_handle.c index b4dd4adf..2bfb8a6f 100644 --- a/plugins/restful/normal_handle.c +++ b/plugins/restful/normal_handle.c @@ -28,6 +28,7 @@ #include #include +#include "utils/asprintf.h" #include "utils/log.h" #include "argparse.h" @@ -119,8 +120,8 @@ static char *file_string_read(size_t *length, const char *const path) void handle_get_plugin_schema(nng_aio *aio) { - size_t len = 0; - char schema_path[256] = { 0 }; + size_t len = 0; + char * schema_path = NULL; VALIDATE_JWT(aio); @@ -130,17 +131,24 @@ void handle_get_plugin_schema(nng_aio *aio) return; } - snprintf(schema_path, sizeof(schema_path) - 1, "%s/schema/%s.json", - g_plugin_dir, plugin_name); + if (0 > neu_asprintf(&schema_path, "%s/schema/%s.json", g_plugin_dir, + plugin_name)) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { + http_response(aio, error_code.error, result_error); + }); + return; + } char *buf = NULL; buf = file_string_read(&len, schema_path); if (NULL == buf) { nlog_info("open %s error: %d", schema_path, errno); http_not_found(aio, "{\"status\": \"error\"}"); + free(schema_path); return; } http_ok(aio, buf); free(buf); + free(schema_path); } diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index 3e7a7654..984573c8 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -111,7 +111,7 @@ end: static nng_http_server *server_init(char *type) { nng_url * url; - char host_port[128] = { 0 }; + char host_port[24] = { 0 }; nng_http_server *server; if (strncmp(type, "api", strlen("api")) == 0) { diff --git a/plugins/restful/version_handle.c b/plugins/restful/version_handle.c index 9e26d767..432c89e3 100644 --- a/plugins/restful/version_handle.c +++ b/plugins/restful/version_handle.c @@ -17,6 +17,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **/ +#include "utils/asprintf.h" #include "utils/log.h" #include "version.h" #include "json/neu_json_error.h" @@ -27,16 +28,16 @@ void handle_get_version(nng_aio *aio) { - char result[128] = { 0 }; + char *result = NULL; VALIDATE_JWT(aio); - int ret = snprintf( - result, sizeof(result), + int ret = neu_asprintf( + &result, "{\"version\":\"%s\", \"revision\":\"%s\", \"build_date\":\"%s\"}", NEURON_VERSION, NEURON_GIT_REV NEURON_GIT_DIFF, NEURON_BUILD_DATE); - if ((size_t) ret >= sizeof(result)) { + if (ret < 0) { NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { http_response(aio, error_code.error, result_error); });