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; return 1;
} }
if (strcasecmp(ctx->last_map_key, "min_width") == 0) { if (strcasecmp(ctx->last_map_key, "min_width") == 0) {
char *copy = (char *)smalloc(len + 1); sasprintf(&(ctx->block.min_width_str), "%.*s", len, val);
strncpy(copy, (const char *)val, len);
copy[len] = 0;
ctx->block.min_width_str = copy;
return 1; return 1;
} }
if (strcasecmp(ctx->last_map_key, "name") == 0) { if (strcasecmp(ctx->last_map_key, "name") == 0) {
char *copy = (char *)smalloc(len + 1); sasprintf(&(ctx->block.name), "%.*s", len, val);
strncpy(copy, (const char *)val, len);
copy[len] = 0;
ctx->block.name = copy;
return 1; return 1;
} }
if (strcasecmp(ctx->last_map_key, "instance") == 0) { if (strcasecmp(ctx->last_map_key, "instance") == 0) {
char *copy = (char *)smalloc(len + 1); sasprintf(&(ctx->block.instance), "%.*s", len, val);
strncpy(copy, (const char *)val, len);
copy[len] = 0;
ctx->block.instance = copy;
return 1; 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) { static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
FREE(cur_key); FREE(cur_key);
sasprintf(&(cur_key), "%.*s", keyLen, keyVal);
cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
strncpy(cur_key, (const char *)keyVal, keyLen);
cur_key[keyLen] = '\0';
if (strcmp(cur_key, "bindings") == 0) if (strcmp(cur_key, "bindings") == 0)
parsing_bindings = true; 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_; struct mode_json_params *params = (struct mode_json_params *)params_;
if (!strcmp(params->cur_key, "change")) { if (!strcmp(params->cur_key, "change")) {
char *copy = smalloc(sizeof(const unsigned char) * (len + 1)); sasprintf(&(params->name), "%.*s", len, val);
strncpy(copy, (const char *)val, len);
copy[len] = '\0';
params->name = copy;
FREE(params->cur_key); FREE(params->cur_key);
return 1; 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) { 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_; struct mode_json_params *params = (struct mode_json_params *)params_;
FREE(params->cur_key); FREE(params->cur_key);
sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
strncpy(params->cur_key, (const char *)keyVal, keyLen);
params->cur_key[keyLen] = '\0';
return 1; 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_; struct outputs_json_params *params = (struct outputs_json_params *)params_;
if (!strcmp(params->cur_key, "current_workspace")) { if (!strcmp(params->cur_key, "current_workspace")) {
char *copy = smalloc(sizeof(const unsigned char) * (len + 1)); char *copy = NULL;
strncpy(copy, (const char *)val, len); sasprintf(&copy, "%.*s", len, val);
copy[len] = '\0';
char *end; char *end;
errno = 0; errno = 0;
@ -118,7 +117,8 @@ static int outputs_string_cb(void *params_, const unsigned char *val, size_t len
if (errno == 0 && if (errno == 0 &&
(end && *end == '\0')) (end && *end == '\0'))
params->outputs_walk->ws = parsed_num; params->outputs_walk->ws = parsed_num;
free(copy);
FREE(copy);
FREE(params->cur_key); FREE(params->cur_key);
return 1; return 1;
} }
@ -127,14 +127,9 @@ static int outputs_string_cb(void *params_, const unsigned char *val, size_t len
return 0; return 0;
} }
char *name = smalloc(sizeof(const unsigned char) * (len + 1)); sasprintf(&(params->outputs_walk->name), "%.*s", len, val);
strncpy(name, (const char *)val, len);
name[len] = '\0';
params->outputs_walk->name = name;
FREE(params->cur_key); FREE(params->cur_key);
return 1; 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) { 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_; struct outputs_json_params *params = (struct outputs_json_params *)params_;
FREE(params->cur_key); FREE(params->cur_key);
sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
strncpy(params->cur_key, (const char *)keyVal, keyLen);
params->cur_key[keyLen] = '\0';
return 1; 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) { static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
struct workspaces_json_params *params = (struct workspaces_json_params *)params_; struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
char *output_name;
if (!strcmp(params->cur_key, "name")) { if (!strcmp(params->cur_key, "name")) {
const char *ws_name = (const char *)val; const char *ws_name = (const char *)val;
params->workspaces_walk->canonical_name = sstrndup(ws_name, len); 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")) { if (!strcmp(params->cur_key, "output")) {
/* We add the ws to the TAILQ of the output, it belongs to */ /* We add the ws to the TAILQ of the output, it belongs to */
output_name = smalloc(sizeof(const unsigned char) * (len + 1)); char *output_name = NULL;
strncpy(output_name, (const char *)val, len); sasprintf(&output_name, "%.*s", len, val);
output_name[len] = '\0';
i3_output *target = get_output_by_name(output_name); i3_output *target = get_output_by_name(output_name);
if (target) { if (target != NULL) {
params->workspaces_walk->output = target; params->workspaces_walk->output = target;
TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces, 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) { 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_; struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
FREE(params->cur_key); FREE(params->cur_key);
sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
strncpy(params->cur_key, (const char *)keyVal, keyLen);
params->cur_key[keyLen] = '\0';
return 1; return 1;
} }