From: Ingo Bürk Date: Sat, 17 Oct 2015 20:14:48 +0000 (+0200) Subject: Use sasprintf() instead of alloc'ing and strncpy() in i3bar. X-Git-Tag: 4.12~132^2 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=0750b450b2289351c927c0bda97f718f8386e4b1;p=i3%2Fi3 Use sasprintf() instead of alloc'ing and strncpy() in i3bar. resolves #1995 --- diff --git a/i3bar/src/child.c b/i3bar/src/child.c index e73a2920..2b10be49 100644 --- a/i3bar/src/child.c +++ b/i3bar/src/child.c @@ -220,24 +220,15 @@ static int stdin_string(void *context, const unsigned char *val, size_t len) { return 1; } if (strcasecmp(ctx->last_map_key, "min_width") == 0) { - char *copy = (char *)smalloc(len + 1); - strncpy(copy, (const char *)val, len); - copy[len] = 0; - ctx->block.min_width_str = copy; + sasprintf(&(ctx->block.min_width_str), "%.*s", len, val); return 1; } if (strcasecmp(ctx->last_map_key, "name") == 0) { - char *copy = (char *)smalloc(len + 1); - strncpy(copy, (const char *)val, len); - copy[len] = 0; - ctx->block.name = copy; + sasprintf(&(ctx->block.name), "%.*s", len, val); return 1; } if (strcasecmp(ctx->last_map_key, "instance") == 0) { - char *copy = (char *)smalloc(len + 1); - strncpy(copy, (const char *)val, len); - copy[len] = 0; - ctx->block.instance = copy; + sasprintf(&(ctx->block.instance), "%.*s", len, val); return 1; } diff --git a/i3bar/src/config.c b/i3bar/src/config.c index 0e2dd05a..f3412719 100644 --- a/i3bar/src/config.c +++ b/i3bar/src/config.c @@ -30,10 +30,7 @@ static bool parsing_bindings; */ static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) { FREE(cur_key); - - cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1)); - strncpy(cur_key, (const char *)keyVal, keyLen); - cur_key[keyLen] = '\0'; + sasprintf(&(cur_key), "%.*s", keyLen, keyVal); if (strcmp(cur_key, "bindings") == 0) parsing_bindings = true; diff --git a/i3bar/src/mode.c b/i3bar/src/mode.c index 4b27b110..d6767786 100644 --- a/i3bar/src/mode.c +++ b/i3bar/src/mode.c @@ -33,11 +33,7 @@ static int mode_string_cb(void *params_, const unsigned char *val, size_t len) { struct mode_json_params *params = (struct mode_json_params *)params_; if (!strcmp(params->cur_key, "change")) { - char *copy = smalloc(sizeof(const unsigned char) * (len + 1)); - strncpy(copy, (const char *)val, len); - copy[len] = '\0'; - - params->name = copy; + sasprintf(&(params->name), "%.*s", len, val); FREE(params->cur_key); return 1; } @@ -74,11 +70,7 @@ static int mode_boolean_cb(void *params_, int val) { static int mode_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) { struct mode_json_params *params = (struct mode_json_params *)params_; FREE(params->cur_key); - - params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1)); - strncpy(params->cur_key, (const char *)keyVal, keyLen); - params->cur_key[keyLen] = '\0'; - + sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal); return 1; } diff --git a/i3bar/src/outputs.c b/i3bar/src/outputs.c index fd2b1363..d0d175ca 100644 --- a/i3bar/src/outputs.c +++ b/i3bar/src/outputs.c @@ -108,9 +108,8 @@ static int outputs_string_cb(void *params_, const unsigned char *val, size_t len struct outputs_json_params *params = (struct outputs_json_params *)params_; if (!strcmp(params->cur_key, "current_workspace")) { - char *copy = smalloc(sizeof(const unsigned char) * (len + 1)); - strncpy(copy, (const char *)val, len); - copy[len] = '\0'; + char *copy = NULL; + sasprintf(©, "%.*s", len, val); char *end; errno = 0; @@ -118,7 +117,8 @@ static int outputs_string_cb(void *params_, const unsigned char *val, size_t len if (errno == 0 && (end && *end == '\0')) params->outputs_walk->ws = parsed_num; - free(copy); + + FREE(copy); FREE(params->cur_key); return 1; } @@ -127,14 +127,9 @@ static int outputs_string_cb(void *params_, const unsigned char *val, size_t len return 0; } - char *name = smalloc(sizeof(const unsigned char) * (len + 1)); - strncpy(name, (const char *)val, len); - name[len] = '\0'; - - params->outputs_walk->name = name; + sasprintf(&(params->outputs_walk->name), "%.*s", len, val); FREE(params->cur_key); - return 1; } @@ -228,11 +223,7 @@ static int outputs_end_map_cb(void *params_) { static int outputs_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) { struct outputs_json_params *params = (struct outputs_json_params *)params_; FREE(params->cur_key); - - params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1)); - strncpy(params->cur_key, (const char *)keyVal, keyLen); - params->cur_key[keyLen] = '\0'; - + sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal); return 1; } diff --git a/i3bar/src/workspaces.c b/i3bar/src/workspaces.c index 961a41f5..77b351e8 100644 --- a/i3bar/src/workspaces.c +++ b/i3bar/src/workspaces.c @@ -102,8 +102,6 @@ static int workspaces_integer_cb(void *params_, long long val) { static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) { struct workspaces_json_params *params = (struct workspaces_json_params *)params_; - char *output_name; - if (!strcmp(params->cur_key, "name")) { const char *ws_name = (const char *)val; params->workspaces_walk->canonical_name = sstrndup(ws_name, len); @@ -147,11 +145,11 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, size_t if (!strcmp(params->cur_key, "output")) { /* We add the ws to the TAILQ of the output, it belongs to */ - output_name = smalloc(sizeof(const unsigned char) * (len + 1)); - strncpy(output_name, (const char *)val, len); - output_name[len] = '\0'; + char *output_name = NULL; + sasprintf(&output_name, "%.*s", len, val); + i3_output *target = get_output_by_name(output_name); - if (target) { + if (target != NULL) { params->workspaces_walk->output = target; TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces, @@ -201,11 +199,7 @@ static int workspaces_start_map_cb(void *params_) { static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) { struct workspaces_json_params *params = (struct workspaces_json_params *)params_; FREE(params->cur_key); - - params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1)); - strncpy(params->cur_key, (const char *)keyVal, keyLen); - params->cur_key[keyLen] = '\0'; - + sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal); return 1; }