Remove all sprintf calls

Replace them with snprintf, which ensures buffer overflows won't
happen.
master
Simon Ser 3 years ago committed by Simon Zeni
parent ac7892371c
commit f707f583e1

@ -1706,7 +1706,7 @@ static void update_marks_texture(struct sway_container *con,
for (int i = 0; i < con->marks->length; ++i) { for (int i = 0; i < con->marks->length; ++i) {
char *mark = con->marks->items[i]; char *mark = con->marks->items[i];
if (mark[0] != '_') { if (mark[0] != '_') {
sprintf(part, "[%s]", mark); snprintf(part, len + 1, "[%s]", mark);
strcat(buffer, part); strcat(buffer, part);
} }
} }

@ -209,7 +209,7 @@ static pid_t get_parent_pid(pid_t child) {
FILE *stat = NULL; FILE *stat = NULL;
size_t buf_size = 0; size_t buf_size = 0;
sprintf(file_name, "/proc/%d/stat", child); snprintf(file_name, sizeof(file_name), "/proc/%d/stat", child);
if ((stat = fopen(file_name, "r"))) { if ((stat = fopen(file_name, "r"))) {
if (getline(&buffer, &buf_size, stat) != -1) { if (getline(&buffer, &buf_size, stat) != -1) {

@ -414,8 +414,8 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
} }
free(name); free(name);
} else { } else {
char *flag = malloc(sizeof(char) * (nread + 3)); char *flag = malloc(nread + 3);
sprintf(flag, "--%s", line); snprintf(flag, nread + 3, "--%s", line);
char *argv[] = {"swaynag", flag}; char *argv[] = {"swaynag", flag};
result = swaynag_parse_options(2, argv, swaynag, types, type, result = swaynag_parse_options(2, argv, swaynag, types, type,
NULL, NULL); NULL, NULL);

@ -28,8 +28,9 @@ static bool terminal_execute(char *terminal, char *command) {
fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command);
fclose(tmp); fclose(tmp);
chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR);
char *cmd = malloc(sizeof(char) * (strlen(terminal) + strlen(" -e ") + strlen(fname) + 1)); size_t cmd_size = strlen(terminal) + strlen(" -e ") + strlen(fname) + 1;
sprintf(cmd, "%s -e %s", terminal, fname); char *cmd = malloc(cmd_size);
snprintf(cmd, cmd_size, "%s -e %s", terminal, fname);
execlp("sh", "sh", "-c", cmd, NULL); execlp("sh", "sh", "-c", cmd, NULL);
sway_log_errno(SWAY_ERROR, "Failed to run command, execlp() returned."); sway_log_errno(SWAY_ERROR, "Failed to run command, execlp() returned.");
free(cmd); free(cmd);

Loading…
Cancel
Save