From: Orestis Floros Date: Sat, 30 Mar 2019 11:20:32 +0000 (+0200) Subject: Replace scalloc + strncpy with sstrndup X-Git-Url: https://git.sur5r.net/?p=i3%2Fi3;a=commitdiff_plain;h=ea6068a02d54c7f9995c94d89c53078bb0097906 Replace scalloc + strncpy with sstrndup --- diff --git a/i3-msg/main.c b/i3-msg/main.c index 0ada5f64..3a897416 100644 --- a/i3-msg/main.c +++ b/i3-msg/main.c @@ -77,8 +77,8 @@ static int reply_boolean_cb(void *params, int val) { } static int reply_string_cb(void *params, const unsigned char *val, size_t len) { - char *str = scalloc(len + 1, 1); - strncpy(str, (const char *)val, len); + char *str = sstrndup((const char *)val, len); + if (strcmp(last_key, "error") == 0) last_reply.error = str; else if (strcmp(last_key, "input") == 0) @@ -108,8 +108,7 @@ static int reply_end_map_cb(void *params) { static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) { free(last_key); - last_key = scalloc(keyLen + 1, 1); - strncpy(last_key, (const char *)keyVal, keyLen); + last_key = sstrndup((const char *)keyVal, keyLen); return 1; } @@ -128,8 +127,7 @@ static yajl_callbacks reply_callbacks = { static char *config_last_key = NULL; static int config_string_cb(void *params, const unsigned char *val, size_t len) { - char *str = scalloc(len + 1, 1); - strncpy(str, (const char *)val, len); + char *str = sstrndup((const char *)val, len); if (strcmp(config_last_key, "config") == 0) { fprintf(stdout, "%s", str); } @@ -146,8 +144,7 @@ static int config_end_map_cb(void *params) { } static int config_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) { - config_last_key = scalloc(keyLen + 1, 1); - strncpy(config_last_key, (const char *)keyVal, keyLen); + config_last_key = sstrndup((const char *)keyVal, keyLen); return 1; } diff --git a/src/ipc.c b/src/ipc.c index 2432d7a5..e548b5a6 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -215,12 +215,11 @@ void ipc_shutdown(shutdown_reason_t reason) { IPC_HANDLER(run_command) { /* To get a properly terminated buffer, we copy * message_size bytes out of the buffer */ - char *command = scalloc(message_size + 1, 1); - strncpy(command, (const char *)message, message_size); + char *command = sstrndup((const char *)message, message_size); LOG("IPC: received: *%s*\n", command); yajl_gen gen = yajl_gen_alloc(NULL); - CommandResult *result = parse_command((const char *)command, gen); + CommandResult *result = parse_command(command, gen); free(command); if (result->needs_tree_render)