Use safe wrappers wherever possible

This commit is contained in:
shdown 2015-08-03 12:50:50 +03:00
parent bc52fae15c
commit c85d16faa4
14 changed files with 31 additions and 52 deletions

View File

@ -768,7 +768,7 @@ int main(int argc, char *argv[]) {
switch (o) { switch (o) {
case 's': case 's':
FREE(socket_path); FREE(socket_path);
socket_path = strdup(optarg); socket_path = sstrdup(optarg);
break; break;
case 'v': case 'v':
printf("i3-config-wizard " I3_VERSION "\n"); printf("i3-config-wizard " I3_VERSION "\n");

View File

@ -103,7 +103,7 @@ static void restore_input_focus(void) {
* *
*/ */
static uint8_t *concat_strings(char **glyphs, int max) { static uint8_t *concat_strings(char **glyphs, int max) {
uint8_t *output = calloc(max + 1, 4); uint8_t *output = scalloc(max + 1, 4);
uint8_t *walk = output; uint8_t *walk = output;
for (int c = 0; c < max; c++) { for (int c = 0; c < max; c++) {
printf("at %c\n", glyphs[c][0]); printf("at %c\n", glyphs[c][0]);
@ -187,10 +187,10 @@ static void finish_input() {
/* allocate space for the output */ /* allocate space for the output */
int inputlen = strlen(command); int inputlen = strlen(command);
char *full = calloc(1, char *full = scalloc(strlen(format) - (2 * cnt) /* format without all %s */
strlen(format) - (2 * cnt) /* format without all %s */ + (inputlen * cnt) /* replaced %s */
+ (inputlen * cnt) /* replaced %s */ + 1, /* trailing NUL */
+ 1); /* trailing NUL */ 1);
char *dest = full; char *dest = full;
for (c = 0; c < len; c++) { for (c = 0; c < len; c++) {
/* if this is not % or it is % but without a following 's', /* if this is not % or it is % but without a following 's',
@ -359,7 +359,7 @@ free_resources:
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
format = strdup("%s"); format = sstrdup("%s");
socket_path = getenv("I3SOCK"); socket_path = getenv("I3SOCK");
char *pattern = sstrdup("pango:monospace 8"); char *pattern = sstrdup("pango:monospace 8");
int o, option_index = 0; int o, option_index = 0;
@ -381,7 +381,7 @@ int main(int argc, char *argv[]) {
switch (o) { switch (o) {
case 's': case 's':
FREE(socket_path); FREE(socket_path);
socket_path = strdup(optarg); socket_path = sstrdup(optarg);
break; break;
case 'v': case 'v':
printf("i3-input " I3_VERSION); printf("i3-input " I3_VERSION);
@ -401,11 +401,11 @@ int main(int argc, char *argv[]) {
break; break;
case 'f': case 'f':
FREE(pattern); FREE(pattern);
pattern = strdup(optarg); pattern = sstrdup(optarg);
break; break;
case 'F': case 'F':
FREE(format); FREE(format);
format = strdup(optarg); format = sstrdup(optarg);
break; break;
case 'h': case 'h':
printf("i3-input " I3_VERSION "\n"); printf("i3-input " I3_VERSION "\n");

View File

@ -187,8 +187,7 @@ int main(int argc, char *argv[]) {
payload = sstrdup(argv[optind]); payload = sstrdup(argv[optind]);
} else { } else {
char *both; char *both;
if (asprintf(&both, "%s %s", payload, argv[optind]) == -1) sasprintf(&both, "%s %s", payload, argv[optind]);
err(EXIT_FAILURE, "asprintf");
free(payload); free(payload);
payload = both; payload = both;
} }

View File

@ -371,7 +371,7 @@ int main(int argc, char *argv[]) {
if (argv0_len > strlen(".nagbar_cmd") && if (argv0_len > strlen(".nagbar_cmd") &&
strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) { strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) {
unlink(argv[0]); unlink(argv[0]);
cmd = strdup(argv[0]); cmd = sstrdup(argv[0]);
*(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0'; *(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0';
execl("/bin/sh", "/bin/sh", cmd, NULL); execl("/bin/sh", "/bin/sh", cmd, NULL);
err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd); err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd);
@ -418,7 +418,7 @@ int main(int argc, char *argv[]) {
printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n"); printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
return 0; return 0;
case 'b': case 'b':
buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1)); buttons = srealloc(buttons, sizeof(button_t) * (buttoncnt + 1));
buttons[buttoncnt].label = i3string_from_utf8(optarg); buttons[buttoncnt].label = i3string_from_utf8(optarg);
buttons[buttoncnt].action = argv[optind]; buttons[buttoncnt].action = argv[optind];
printf("button with label *%s* and action *%s*\n", printf("button with label *%s* and action *%s*\n",

View File

@ -220,21 +220,21 @@ 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 *)malloc(len + 1); char *copy = (char *)smalloc(len + 1);
strncpy(copy, (const char *)val, len); strncpy(copy, (const char *)val, len);
copy[len] = 0; copy[len] = 0;
ctx->block.min_width_str = copy; 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 *)malloc(len + 1); char *copy = (char *)smalloc(len + 1);
strncpy(copy, (const char *)val, len); strncpy(copy, (const char *)val, len);
copy[len] = 0; copy[len] = 0;
ctx->block.name = copy; 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 *)malloc(len + 1); char *copy = (char *)smalloc(len + 1);
strncpy(copy, (const char *)val, len); strncpy(copy, (const char *)val, len);
copy[len] = 0; copy[len] = 0;
ctx->block.instance = copy; ctx->block.instance = copy;

View File

@ -1654,8 +1654,7 @@ void reconfig_windows(bool redraw_bars) {
"i3bar\0i3bar\0"); "i3bar\0i3bar\0");
char *name; char *name;
if (asprintf(&name, "i3bar for output %s", walk->name) == -1) sasprintf(&name, "i3bar for output %s", walk->name);
err(EXIT_FAILURE, "asprintf()");
xcb_void_cookie_t name_cookie; xcb_void_cookie_t name_cookie;
name_cookie = xcb_change_property(xcb_connection, name_cookie = xcb_change_property(xcb_connection,
XCB_PROP_MODE_REPLACE, XCB_PROP_MODE_REPLACE,

View File

@ -73,7 +73,7 @@ char *get_exe_path(const char *argv0) {
} }
sasprintf(&path, ":%s", tmp); sasprintf(&path, ":%s", tmp);
} else { } else {
path = strdup(path); path = sstrdup(path);
} }
const char *component; const char *component;
char *str = path; char *str = path;

View File

@ -80,18 +80,10 @@ char *root_atom_contents(const char *atomname, xcb_connection_t *provided_conn,
if (prop_reply->type == XCB_ATOM_CARDINAL) { if (prop_reply->type == XCB_ATOM_CARDINAL) {
/* We treat a CARDINAL as a >= 32-bit unsigned int. The only CARDINAL /* We treat a CARDINAL as a >= 32-bit unsigned int. The only CARDINAL
* we query is I3_PID, which is 32-bit. */ * we query is I3_PID, which is 32-bit. */
if (asprintf(&content, "%u", *((unsigned int *)xcb_get_property_value(prop_reply))) == -1) { sasprintf(&content, "%u", *((unsigned int *)xcb_get_property_value(prop_reply)));
free(atom_reply);
free(prop_reply);
return NULL;
}
} else { } else {
if (asprintf(&content, "%.*s", xcb_get_property_value_length(prop_reply), sasprintf(&content, "%.*s", xcb_get_property_value_length(prop_reply),
(char *)xcb_get_property_value(prop_reply)) == -1) { (char *)xcb_get_property_value(prop_reply));
free(atom_reply);
free(prop_reply);
return NULL;
}
} }
if (provided_conn == NULL) if (provided_conn == NULL)
xcb_disconnect(conn); xcb_disconnect(conn);

View File

@ -393,9 +393,9 @@ static Binding *binding_copy(Binding *bind) {
Binding *ret = smalloc(sizeof(Binding)); Binding *ret = smalloc(sizeof(Binding));
*ret = *bind; *ret = *bind;
if (bind->symbol != NULL) if (bind->symbol != NULL)
ret->symbol = strdup(bind->symbol); ret->symbol = sstrdup(bind->symbol);
if (bind->command != NULL) if (bind->command != NULL)
ret->command = strdup(bind->command); ret->command = sstrdup(bind->command);
if (bind->translated_to != NULL) { if (bind->translated_to != NULL) {
ret->translated_to = smalloc(sizeof(xcb_keycode_t) * bind->number_keycodes); ret->translated_to = smalloc(sizeof(xcb_keycode_t) * bind->number_keycodes);
memcpy(ret->translated_to, bind->translated_to, sizeof(xcb_keycode_t) * bind->number_keycodes); memcpy(ret->translated_to, bind->translated_to, sizeof(xcb_keycode_t) * bind->number_keycodes);

View File

@ -789,12 +789,12 @@ static char *migrate_config(char *input, off_t size) {
/* read the scripts output */ /* read the scripts output */
int conv_size = 65535; int conv_size = 65535;
char *converted = malloc(conv_size); char *converted = smalloc(conv_size);
int read_bytes = 0, ret; int read_bytes = 0, ret;
do { do {
if (read_bytes == conv_size) { if (read_bytes == conv_size) {
conv_size += 65535; conv_size += 65535;
converted = realloc(converted, conv_size); converted = srealloc(converted, conv_size);
} }
ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes); ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
if (ret == -1) { if (ret == -1) {

View File

@ -900,7 +900,7 @@ static int add_subscription(void *extra, const unsigned char *s,
int event = client->num_events; int event = client->num_events;
client->num_events++; client->num_events++;
client->events = realloc(client->events, client->num_events * sizeof(char *)); client->events = srealloc(client->events, client->num_events * sizeof(char *));
/* We copy the string because it is not null-terminated and strndup() /* We copy the string because it is not null-terminated and strndup()
* is missing on some BSD systems */ * is missing on some BSD systems */
client->events[event] = scalloc(len + 1, 1); client->events[event] = scalloc(len + 1, 1);

View File

@ -316,14 +316,8 @@ struct Startup_Sequence *startup_sequence_get(i3Window *cwindow,
} }
char *startup_id; char *startup_id;
if (asprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply), sasprintf(&startup_id, "%.*s", xcb_get_property_value_length(startup_id_reply),
(char *)xcb_get_property_value(startup_id_reply)) == -1) { (char *)xcb_get_property_value(startup_id_reply));
perror("asprintf()");
DLOG("Could not get _NET_STARTUP_ID\n");
free(startup_id_reply);
return NULL;
}
struct Startup_Sequence *current, *sequence = NULL; struct Startup_Sequence *current, *sequence = NULL;
TAILQ_FOREACH(current, &startup_sequences, sequences) { TAILQ_FOREACH(current, &startup_sequences, sequences) {
if (strcmp(current->id, startup_id) != 0) if (strcmp(current->id, startup_id) != 0)

View File

@ -122,7 +122,7 @@ void exec_i3_utility(char *name, char *argv[]) {
/* if the script is not in path, maybe the user installed to a strange /* if the script is not in path, maybe the user installed to a strange
* location and runs the i3 binary with an absolute path. We use * location and runs the i3 binary with an absolute path. We use
* argv[0]s dirname */ * argv[0]s dirname */
char *pathbuf = strdup(start_argv[0]); char *pathbuf = sstrdup(start_argv[0]);
char *dir = dirname(pathbuf); char *dir = dirname(pathbuf);
sasprintf(&migratepath, "%s/%s", dir, name); sasprintf(&migratepath, "%s/%s", dir, name);
argv[0] = migratepath; argv[0] = migratepath;

View File

@ -208,13 +208,8 @@ void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool befo
} }
char *new_role; char *new_role;
if (asprintf(&new_role, "%.*s", xcb_get_property_value_length(prop), sasprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
(char *)xcb_get_property_value(prop)) == -1) { (char *)xcb_get_property_value(prop));
perror("asprintf()");
DLOG("Could not get WM_WINDOW_ROLE\n");
free(prop);
return;
}
FREE(win->role); FREE(win->role);
win->role = new_role; win->role = new_role;
LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role); LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);