Merge pull request #2186 from martinetd/static-analysis

Static analysis fixes
This commit is contained in:
emersion 2018-07-02 09:06:02 +01:00 committed by GitHub
commit defb73596f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 113 additions and 62 deletions

View file

@ -92,7 +92,7 @@ static const struct {
int utf8_size(const char *s) {
uint8_t c = (uint8_t)*s;
for (size_t i = 0; i < sizeof(sizes) / 2; ++i) {
for (size_t i = 0; i < sizeof(sizes) / sizeof(*sizes); ++i) {
if ((c & sizes[i].mask) == sizes[i].result) {
return sizes[i].octets;
}

View file

@ -95,7 +95,7 @@ pid_t get_parent_pid(pid_t child) {
token = strtok(NULL, sep); // parent pid
parent = strtol(token, NULL, 10);
}
free(buffer);
fclose(stat);
}

View file

@ -428,8 +428,7 @@ struct cmd_results *config_commands_command(char *exec) {
struct cmd_handler *handler = find_handler(cmd, NULL, 0);
if (!handler && strcmp(cmd, "*") != 0) {
char *input = cmd ? cmd : "(empty)";
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command");
goto cleanup;
}
@ -471,10 +470,12 @@ struct cmd_results *config_commands_command(char *exec) {
}
if (!policy) {
policy = alloc_command_policy(cmd);
sway_assert(policy, "Unable to allocate security policy");
if (policy) {
list_add(config->command_policies, policy);
if (!sway_assert(policy, "Unable to allocate security policy")) {
results = cmd_results_new(CMD_INVALID, cmd,
"Unable to allocate memory");
goto cleanup;
}
list_add(config->command_policies, policy);
}
policy->context = context;

View file

@ -27,6 +27,7 @@ struct cmd_results *cmd_assign(int argc, char **argv) {
if (strncmp(*argv, "", strlen("")) == 0) {
if (argc < 3) {
free(criteria);
return cmd_results_new(CMD_INVALID, "assign", "Missing workspace");
}
++argv;

View file

@ -14,7 +14,7 @@ struct cmd_results *bar_cmd_font(int argc, char **argv) {
}
char *font = join_args(argv, argc);
free(config->current_bar->font);
config->current_bar->font = strdup(font);
config->current_bar->font = font;
wlr_log(L_DEBUG, "Settings font '%s' for bar: %s",
config->current_bar->font, config->current_bar->id);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);

View file

@ -22,9 +22,10 @@ struct cmd_results *bar_cmd_modifier(int argc, char **argv) {
mod |= tmp_mod;
continue;
} else {
free_flat_list(split);
return cmd_results_new(CMD_INVALID, "modifier",
error = cmd_results_new(CMD_INVALID, "modifier",
"Unknown modifier '%s'", split->items[i]);
free_flat_list(split);
return error;
}
}
free_flat_list(split);

View file

@ -23,6 +23,7 @@ struct cmd_results *input_cmd_accel_profile(int argc, char **argv) {
} else if (strcasecmp(argv[0], "flat") == 0) {
new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "accel_profile",
"Expected 'accel_profile <adaptive|flat>'");
}

View file

@ -26,6 +26,7 @@ struct cmd_results *input_cmd_click_method(int argc, char **argv) {
} else if (strcasecmp(argv[0], "clickfinger") == 0) {
new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "click_method",
"Expected 'click_method <none|button_areas|clickfinger'");
}

View file

@ -23,6 +23,7 @@ struct cmd_results *input_cmd_drag_lock(int argc, char **argv) {
} else if (strcasecmp(argv[0], "disabled") == 0) {
new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_DISABLED;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "drag_lock",
"Expected 'drag_lock <enabled|disabled>'");
}

View file

@ -22,6 +22,7 @@ struct cmd_results *input_cmd_dwt(int argc, char **argv) {
} else if (strcasecmp(argv[0], "disabled") == 0) {
new_config->dwt = LIBINPUT_CONFIG_DWT_DISABLED;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "dwt",
"Expected 'dwt <enabled|disabled>'");
}

View file

@ -29,6 +29,7 @@ struct cmd_results *input_cmd_events(int argc, char **argv) {
new_config->send_events =
LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "events",
"Expected 'events <enabled|disabled|disabled_on_external_mouse>'");
}

View file

@ -23,6 +23,7 @@ struct cmd_results *input_cmd_left_handed(int argc, char **argv) {
} else if (strcasecmp(argv[0], "disabled") == 0) {
new_config->left_handed = 0;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "left_handed",
"Expected 'left_handed <enabled|disabled>'");
}

View file

@ -54,20 +54,28 @@ struct cmd_results *input_cmd_map_from_region(int argc, char **argv) {
bool mm1, mm2;
if (!parse_coords(argv[0], &new_config->mapped_from_region->x1,
&new_config->mapped_from_region->y1, &mm1)) {
free(new_config->mapped_from_region);
free_input_config(new_config);
return cmd_results_new(CMD_FAILURE, "map_from_region",
"Invalid top-left coordinates");
}
if (!parse_coords(argv[1], &new_config->mapped_from_region->x2,
&new_config->mapped_from_region->y2, &mm2)) {
free(new_config->mapped_from_region);
free_input_config(new_config);
return cmd_results_new(CMD_FAILURE, "map_from_region",
"Invalid bottom-right coordinates");
}
if (new_config->mapped_from_region->x1 > new_config->mapped_from_region->x2 ||
new_config->mapped_from_region->y1 > new_config->mapped_from_region->y2) {
free(new_config->mapped_from_region);
free_input_config(new_config);
return cmd_results_new(CMD_FAILURE, "map_from_region",
"Invalid rectangle");
}
if (mm1 != mm2) {
free(new_config->mapped_from_region);
free_input_config(new_config);
return cmd_results_new(CMD_FAILURE, "map_from_region",
"Both coordinates must be in the same unit");
}

View file

@ -24,6 +24,7 @@ struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) {
new_config->middle_emulation =
LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "middle_emulation",
"Expected 'middle_emulation <enabled|disabled>'");
}

View file

@ -23,6 +23,7 @@ struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) {
} else if (strcasecmp(argv[0], "disabled") == 0) {
new_config->natural_scroll = 0;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "natural_scroll",
"Expected 'natural_scroll <enabled|disabled>'");
}

View file

@ -20,6 +20,7 @@ struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) {
float pointer_accel = atof(argv[0]);
if (pointer_accel < -1 || pointer_accel > 1) {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "pointer_accel",
"Input out of range [-1, 1]");
}

View file

@ -20,6 +20,7 @@ struct cmd_results *input_cmd_repeat_delay(int argc, char **argv) {
int repeat_delay = atoi(argv[0]);
if (repeat_delay < 0) {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "repeat_delay",
"Repeat delay cannot be negative");
}

View file

@ -20,6 +20,7 @@ struct cmd_results *input_cmd_repeat_rate(int argc, char **argv) {
int repeat_rate = atoi(argv[0]);
if (repeat_rate < 0) {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "repeat_rate",
"Repeat rate cannot be negative");
}

View file

@ -27,6 +27,7 @@ struct cmd_results *input_cmd_scroll_method(int argc, char **argv) {
} else if (strcasecmp(argv[0], "on_button_down") == 0) {
new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "scroll_method",
"Expected 'scroll_method <none|two_finger|edge|on_button_down>'");
}

View file

@ -23,6 +23,7 @@ struct cmd_results *input_cmd_tap(int argc, char **argv) {
} else if (strcasecmp(argv[0], "disabled") == 0) {
new_config->tap = LIBINPUT_CONFIG_TAP_DISABLED;
} else {
free_input_config(new_config);
return cmd_results_new(CMD_INVALID, "tap",
"Expected 'tap <enabled|disabled>'");
}

View file

@ -83,6 +83,7 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
char *conf = strdup(config->current_config);
if (!conf) {
wlr_log(L_ERROR, "Failed to duplicate string");
free(src);
return cmd_results_new(CMD_FAILURE, "output",
"Unable to allocate resources");
}

View file

@ -36,11 +36,11 @@ struct cmd_results *output_cmd_mode(int argc, char **argv) {
}
} else {
// Format is 1234 4321
argc--; argv++;
if (!argc) {
return cmd_results_new(CMD_INVALID, "output",
"Missing mode argument (height).");
}
argc--; argv++;
output->height = strtol(*argv, &end, 10);
if (*end) {
return cmd_results_new(CMD_INVALID, "output",

View file

@ -27,11 +27,11 @@ struct cmd_results *output_cmd_position(int argc, char **argv) {
}
} else {
// Format is 1234 4321 (legacy)
argc--; argv++;
if (!argc) {
return cmd_results_new(CMD_INVALID, "output",
"Missing position argument (y).");
}
argc--; argv++;
config->handler_context.output_config->y = strtol(*argv, &end, 10);
if (*end) {
return cmd_results_new(CMD_INVALID, "output",

View file

@ -302,6 +302,11 @@ static char *get_config_path(void) {
const char *current_config_path;
static bool load_config(const char *path, struct sway_config *config) {
if (path == NULL) {
wlr_log(L_ERROR, "Unable to find a config file!");
return false;
}
wlr_log(L_INFO, "Loading config from %s", path);
current_config_path = path;
@ -310,11 +315,6 @@ static bool load_config(const char *path, struct sway_config *config) {
return false;
}
if (path == NULL) {
wlr_log(L_ERROR, "Unable to find a config file!");
return false;
}
FILE *f = fopen(path, "r");
if (!f) {
wlr_log(L_ERROR, "Unable to open %s for reading", path);
@ -425,7 +425,7 @@ static bool load_include_config(const char *path, const char *parent_dir,
// save parent config
const char *parent_config = config->current_config;
char *full_path = strdup(path);
char *full_path;
int len = strlen(path);
if (len >= 1 && path[0] != '/') {
len = len + strlen(parent_dir) + 2;
@ -436,6 +436,8 @@ static bool load_include_config(const char *path, const char *parent_dir,
return false;
}
snprintf(full_path, len, "%s/%s", parent_dir, path);
} else {
full_path = strdup(path);
}
char *real_path = realpath(full_path, NULL);
@ -583,6 +585,8 @@ bool read_config(FILE *file, struct sway_config *config) {
}
char *expanded = expand_line(block, line, brace_detected > 0);
if (!expanded) {
list_foreach(stack, free);
list_free(stack);
return false;
}
wlr_log(L_DEBUG, "Expanded line: %s", expanded);

View file

@ -70,16 +70,12 @@ void free_bar_config(struct bar_config *bar) {
struct bar_config *default_bar_config(void) {
struct bar_config *bar = NULL;
bar = malloc(sizeof(struct bar_config));
bar = calloc(1, sizeof(struct bar_config));
if (!bar) {
return NULL;
}
if (!(bar->mode = strdup("dock"))) goto cleanup;
if (!(bar->hidden_state = strdup("hide"))) goto cleanup;
bar->outputs = NULL;
bar->position = strdup("bottom");
if (!(bar->bindings = create_list())) goto cleanup;
if (!(bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p'; sleep 1; done"))) goto cleanup;
bar->pango_markup = false;
bar->swaybar_command = NULL;
bar->font = NULL;
@ -91,6 +87,19 @@ struct bar_config *default_bar_config(void) {
bar->binding_mode_indicator = true;
bar->verbose = false;
bar->pid = 0;
if (!(bar->mode = strdup("dock"))) {
goto cleanup;
}
if (!(bar->hidden_state = strdup("hide"))) {
goto cleanup;
}
if (!(bar->bindings = create_list())) {
goto cleanup;
}
if (!(bar->status_command =
strdup("while date +'%Y-%m-%d %l:%M:%S %p'; do sleep 1; done"))) {
goto cleanup;
}
// set default colors
if (!(bar->colors.background = strndup("#000000ff", 9))) {
goto cleanup;
@ -174,7 +183,7 @@ void invoke_swaybar(struct bar_config *bar) {
if (!command) {
const char msg[] = "Unable to allocate swaybar command string";
size_t msg_len = sizeof(msg);
if (write(filedes[1], &msg_len, sizeof(int))) {};
if (write(filedes[1], &msg_len, sizeof(size_t))) {};
if (write(filedes[1], msg, msg_len)) {};
close(filedes[1]);
exit(1);
@ -189,8 +198,8 @@ void invoke_swaybar(struct bar_config *bar) {
}
wlr_log(L_DEBUG, "Spawned swaybar %d", bar->pid);
close(filedes[0]);
ssize_t len;
if (read(filedes[1], &len, sizeof(int)) == sizeof(int)) {
size_t len;
if (read(filedes[1], &len, sizeof(size_t)) == sizeof(size_t)) {
char *buf = malloc(len);
if(!buf) {
wlr_log(L_ERROR, "Cannot allocate error string");

View file

@ -187,7 +187,7 @@ static void transaction_apply(struct sway_transaction *transaction) {
float ms_total = ms_arranging + ms_waiting;
wlr_log(L_DEBUG, "Transaction %p: %.1fms arranging, %.1fms waiting, "
"%.1fms total (%.1f frames if 60Hz)", transaction,
ms_arranging, ms_waiting, ms_total, ms_total / (1000 / 60));
ms_arranging, ms_waiting, ms_total, ms_total / (1000.0f / 60));
}
// Apply the instruction state to the container's current state

View file

@ -263,7 +263,10 @@ static void ipc_send_event(const char *json_string, enum ipc_command_type event)
client->current_command = event;
if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) {
wlr_log_errno(L_INFO, "Unable to send reply to IPC client");
ipc_client_disconnect(client);
/* ipc_send_reply destroys client on error, which also
* removes it from the list, so we need to process
* current index again */
i--;
}
}
}
@ -383,9 +386,7 @@ void ipc_client_disconnect(struct ipc_client *client) {
return;
}
if (client->fd != -1) {
shutdown(client->fd, SHUT_RDWR);
}
wlr_log(L_INFO, "IPC Client %d disconnected", client->fd);
wl_event_source_remove(client->event_source);
@ -465,8 +466,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
}
buf[client->payload_length] = '\0';
const char *error_denied = "{ \"success\": false, \"error\": \"Permission denied\" }";
bool client_valid = true;
switch (client->current_command) {
case IPC_COMMAND:
{
@ -474,7 +474,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
const char *json = cmd_results_to_json(results);
char reply[256];
int length = snprintf(reply, sizeof(reply), "%s", json);
ipc_send_reply(client, reply, (uint32_t) length);
client_valid = ipc_send_reply(client, reply, (uint32_t)length);
free_cmd_results(results);
goto exit_cleanup;
}
@ -497,6 +497,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
}
}
const char *json_string = json_object_to_json_string(outputs);
client_valid =
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
json_object_put(outputs); // free
goto exit_cleanup;
@ -508,6 +509,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
container_for_each_descendant_dfs(&root_container,
ipc_get_workspaces_callback, workspaces);
const char *json_string = json_object_to_json_string(workspaces);
client_valid =
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
json_object_put(workspaces); // free
goto exit_cleanup;
@ -518,7 +520,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
// TODO: Check if they're permitted to use these events
struct json_object *request = json_tokener_parse(buf);
if (request == NULL) {
ipc_send_reply(client, "{\"success\": false}", 18);
client_valid = ipc_send_reply(client, "{\"success\": false}", 18);
wlr_log_errno(L_INFO, "Failed to read request");
goto exit_cleanup;
}
@ -539,6 +541,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
} else if (strcmp(event_type, "binding") == 0) {
client->subscribed_events |= event_mask(IPC_EVENT_BINDING);
} else {
client_valid =
ipc_send_reply(client, "{\"success\": false}", 18);
json_object_put(request);
wlr_log_errno(L_INFO, "Failed to parse request");
@ -547,7 +550,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
}
json_object_put(request);
ipc_send_reply(client, "{\"success\": true}", 17);
client_valid = ipc_send_reply(client, "{\"success\": true}", 17);
goto exit_cleanup;
}
@ -559,6 +562,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
json_object_array_add(inputs, ipc_json_describe_input(device));
}
const char *json_string = json_object_to_json_string(inputs);
client_valid =
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
json_object_put(inputs); // free
goto exit_cleanup;
@ -572,6 +576,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
json_object_array_add(seats, ipc_json_describe_seat(seat));
}
const char *json_string = json_object_to_json_string(seats);
client_valid =
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
json_object_put(seats); // free
goto exit_cleanup;
@ -582,6 +587,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
json_object *tree =
ipc_json_describe_container_recursive(&root_container);
const char *json_string = json_object_to_json_string(tree);
client_valid =
ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
json_object_put(tree);
goto exit_cleanup;
@ -593,6 +599,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
container_descendants(&root_container, C_VIEW, ipc_get_marks_callback,
marks);
const char *json_string = json_object_to_json_string(marks);
client_valid =
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
json_object_put(marks);
goto exit_cleanup;
@ -602,6 +609,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
{
json_object *version = ipc_json_get_version();
const char *json_string = json_object_to_json_string(version);
client_valid =
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
json_object_put(version); // free
goto exit_cleanup;
@ -617,7 +625,9 @@ void ipc_client_handle_command(struct ipc_client *client) {
json_object_array_add(bars, json_object_new_string(bar->id));
}
const char *json_string = json_object_to_json_string(bars);
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
client_valid =
ipc_send_reply(client, json_string,
(uint32_t)strlen(json_string));
json_object_put(bars); // free
} else {
// Send particular bar's details
@ -631,12 +641,15 @@ void ipc_client_handle_command(struct ipc_client *client) {
}
if (!bar) {
const char *error = "{ \"success\": false, \"error\": \"No bar with that ID\" }";
client_valid =
ipc_send_reply(client, error, (uint32_t)strlen(error));
goto exit_cleanup;
}
json_object *json = ipc_json_describe_bar_config(bar);
const char *json_string = json_object_to_json_string(json);
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
client_valid =
ipc_send_reply(client, json_string,
(uint32_t)strlen(json_string));
json_object_put(json); // free
}
goto exit_cleanup;
@ -647,11 +660,10 @@ void ipc_client_handle_command(struct ipc_client *client) {
goto exit_cleanup;
}
ipc_send_reply(client, error_denied, (uint32_t)strlen(error_denied));
wlr_log(L_DEBUG, "Denied IPC client access to %i", client->current_command);
exit_cleanup:
if (client_valid) {
client->payload_length = 0;
}
free(buf);
return;
}

View file

@ -175,7 +175,7 @@ static void log_kernel() {
}
free(line);
}
fclose(f);
pclose(f);
}
static void security_sanity_check() {

View file

@ -109,7 +109,6 @@ static bool workspace_valid_on_output(const char *output_name,
char *workspace_next_name(const char *output_name) {
wlr_log(L_DEBUG, "Workspace: Generating new workspace name for output %s",
output_name);
int l = 1;
// Scan all workspace bindings to find the next available workspace name,
// if none are found/available then default to a number
struct sway_mode *mode = config->current_mode;
@ -202,14 +201,9 @@ char *workspace_next_name(const char *output_name) {
// As a fall back, get the current number of active workspaces
// and return that + 1 for the next workspace's name
int ws_num = root_container.children->length;
if (ws_num >= 10) {
l = 2;
} else if (ws_num >= 100) {
l = 3;
}
int l = snprintf(NULL, 0, "%d", ws_num);
char *name = malloc(l + 1);
if (!name) {
wlr_log(L_ERROR, "Could not allocate workspace name");
if (!sway_assert(name, "Cloud not allocate workspace name")) {
return NULL;
}
sprintf(name, "%d", ws_num++);
@ -271,6 +265,9 @@ struct sway_container *workspace_by_name(const char *name) {
*/
struct sway_container *workspace_output_prev_next_impl(
struct sway_container *output, bool next) {
if (!output) {
return NULL;
}
if (!sway_assert(output->type == C_OUTPUT,
"Argument must be an output, is %d", output->type)) {
return NULL;
@ -303,6 +300,9 @@ struct sway_container *workspace_output_prev_next_impl(
*/
struct sway_container *workspace_prev_next_impl(
struct sway_container *workspace, bool next) {
if (!workspace) {
return NULL;
}
if (!sway_assert(workspace->type == C_WORKSPACE,
"Argument must be a workspace, is %d", workspace->type)) {
return NULL;

View file

@ -42,6 +42,7 @@ static void daemonize() {
int devnull = open("/dev/null", O_RDWR);
dup2(STDOUT_FILENO, devnull);
dup2(STDERR_FILENO, devnull);
close(devnull);
uint8_t success = 0;
if (chdir("/") != 0) {
write(fds[1], &success, 1);