diff options
-rw-r--r-- | sway/config.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/sway/config.c b/sway/config.c index 636f5f57..b8c874e6 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -560,8 +560,8 @@ static char *expand_line(const char *block, const char *line, bool add_brace) { | |||
560 | 560 | ||
561 | bool read_config(FILE *file, struct sway_config *config) { | 561 | bool read_config(FILE *file, struct sway_config *config) { |
562 | bool reading_main_config = false; | 562 | bool reading_main_config = false; |
563 | char *this_config = NULL, *config_pos; | 563 | char *this_config = NULL; |
564 | long config_size = 0; | 564 | unsigned long config_size = 0; |
565 | if (config->current_config == NULL) { | 565 | if (config->current_config == NULL) { |
566 | reading_main_config = true; | 566 | reading_main_config = true; |
567 | 567 | ||
@@ -569,7 +569,7 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
569 | config_size = ftell(file); | 569 | config_size = ftell(file); |
570 | rewind(file); | 570 | rewind(file); |
571 | 571 | ||
572 | config_pos = this_config = malloc(config_size + 1); | 572 | config->current_config = this_config = calloc(1, config_size + 1); |
573 | if (this_config == NULL) { | 573 | if (this_config == NULL) { |
574 | wlr_log(WLR_ERROR, "Unable to allocate buffer for config contents"); | 574 | wlr_log(WLR_ERROR, "Unable to allocate buffer for config contents"); |
575 | return false; | 575 | return false; |
@@ -580,6 +580,7 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
580 | int line_number = 0; | 580 | int line_number = 0; |
581 | char *line; | 581 | char *line; |
582 | list_t *stack = create_list(); | 582 | list_t *stack = create_list(); |
583 | size_t read = 0; | ||
583 | while (!feof(file)) { | 584 | while (!feof(file)) { |
584 | char *block = stack->length ? stack->items[0] : NULL; | 585 | char *block = stack->length ? stack->items[0] : NULL; |
585 | line = read_line(file); | 586 | line = read_line(file); |
@@ -590,10 +591,21 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
590 | wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); | 591 | wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); |
591 | 592 | ||
592 | if (reading_main_config) { | 593 | if (reading_main_config) { |
593 | size_t l = strlen(line); | 594 | size_t length = strlen(line); |
594 | memcpy(config_pos, line, l); // don't copy terminating character | 595 | |
595 | config_pos += l; | 596 | if (read + length > config_size) { |
596 | *config_pos++ = '\n'; | 597 | wlr_log(WLR_ERROR, "Config file changed during reading"); |
598 | list_foreach(stack, free); | ||
599 | list_free(stack); | ||
600 | free(line); | ||
601 | return false; | ||
602 | } | ||
603 | |||
604 | strcpy(this_config + read, line); | ||
605 | if (line_number != 1) { | ||
606 | this_config[read - 1] = '\n'; | ||
607 | } | ||
608 | read += length + 1; | ||
597 | } | 609 | } |
598 | 610 | ||
599 | line = strip_whitespace(line); | 611 | line = strip_whitespace(line); |
@@ -616,7 +628,6 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
616 | list_foreach(stack, free); | 628 | list_foreach(stack, free); |
617 | list_free(stack); | 629 | list_free(stack); |
618 | free(line); | 630 | free(line); |
619 | free(this_config); | ||
620 | return false; | 631 | return false; |
621 | } | 632 | } |
622 | wlr_log(WLR_DEBUG, "Expanded line: %s", expanded); | 633 | wlr_log(WLR_DEBUG, "Expanded line: %s", expanded); |
@@ -677,10 +688,6 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
677 | list_foreach(stack, free); | 688 | list_foreach(stack, free); |
678 | list_free(stack); | 689 | list_free(stack); |
679 | 690 | ||
680 | if (reading_main_config) { | ||
681 | this_config[config_size - 1] = '\0'; | ||
682 | config->current_config = this_config; | ||
683 | } | ||
684 | return success; | 691 | return success; |
685 | } | 692 | } |
686 | 693 | ||