Use sasprintf() instead of alloc'ing and strncpy() in i3bar.

resolves #1995
This commit is contained in:
Ingo Bürk 2015-10-17 22:14:48 +02:00
parent 22b05f0073
commit 0750b450b2
5 changed files with 17 additions and 52 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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(&copy, "%.*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;
}

View File

@ -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;
}