mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	Merge pull request #2164 from ael-code/background_fixes
output bg command: fix memleak and refactoring
This commit is contained in:
		
						commit
						50ad949185
					
				
					 1 changed files with 34 additions and 24 deletions
				
			
		| 
						 | 
					@ -62,46 +62,56 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
 | 
				
			||||||
		wordexp_t p;
 | 
							wordexp_t p;
 | 
				
			||||||
		char *src = join_args(argv, j);
 | 
							char *src = join_args(argv, j);
 | 
				
			||||||
		if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
 | 
							if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
 | 
				
			||||||
			return cmd_results_new(CMD_INVALID, "output",
 | 
								struct cmd_results *cmd_res = cmd_results_new(CMD_INVALID, "output",
 | 
				
			||||||
				"Invalid syntax (%s).", src);
 | 
									"Invalid syntax (%s)", src);
 | 
				
			||||||
 | 
								free(src);
 | 
				
			||||||
 | 
								wordfree(&p);
 | 
				
			||||||
 | 
								return cmd_res;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		free(src);
 | 
							free(src);
 | 
				
			||||||
		src = p.we_wordv[0];
 | 
							src = strdup(p.we_wordv[0]);
 | 
				
			||||||
		if (config->reading && *src != '/') {
 | 
					 | 
				
			||||||
			char *conf = strdup(config->current_config);
 | 
					 | 
				
			||||||
			if (conf) {
 | 
					 | 
				
			||||||
				char *conf_path = dirname(conf);
 | 
					 | 
				
			||||||
				src = malloc(strlen(conf_path) + strlen(src) + 2);
 | 
					 | 
				
			||||||
				if (!src) {
 | 
					 | 
				
			||||||
					free(conf);
 | 
					 | 
				
			||||||
		wordfree(&p);
 | 
							wordfree(&p);
 | 
				
			||||||
					wlr_log(L_ERROR,
 | 
							if (!src) {
 | 
				
			||||||
						"Unable to allocate resource: Not enough memory");
 | 
								wlr_log(L_ERROR, "Failed to duplicate string");
 | 
				
			||||||
 | 
								return cmd_results_new(CMD_FAILURE, "output",
 | 
				
			||||||
 | 
									"Unable to allocate resource");
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (config->reading && *src != '/') {
 | 
				
			||||||
 | 
								// src file is inside configuration dir
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								char *conf = strdup(config->current_config);
 | 
				
			||||||
 | 
								if(!conf) {
 | 
				
			||||||
 | 
									wlr_log(L_ERROR, "Failed to duplicate string");
 | 
				
			||||||
				return cmd_results_new(CMD_FAILURE, "output",
 | 
									return cmd_results_new(CMD_FAILURE, "output",
 | 
				
			||||||
						"Unable to allocate resources");
 | 
											"Unable to allocate resources");
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
				sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
 | 
					
 | 
				
			||||||
 | 
								char *conf_path = dirname(conf);
 | 
				
			||||||
 | 
								char *rel_path = src;
 | 
				
			||||||
 | 
								src = malloc(strlen(conf_path) + strlen(src) + 2);
 | 
				
			||||||
 | 
								if (!src) {
 | 
				
			||||||
 | 
									free(rel_path);
 | 
				
			||||||
				free(conf);
 | 
									free(conf);
 | 
				
			||||||
			} else {
 | 
									wlr_log(L_ERROR, "Unable to allocate memory");
 | 
				
			||||||
				wlr_log(L_ERROR, "Unable to allocate background source");
 | 
									return cmd_results_new(CMD_FAILURE, "output",
 | 
				
			||||||
 | 
											"Unable to allocate resources");
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								sprintf(src, "%s/%s", conf_path, rel_path);
 | 
				
			||||||
 | 
								free(rel_path);
 | 
				
			||||||
 | 
								free(conf);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (access(src, F_OK) == -1) {
 | 
							if (access(src, F_OK) == -1) {
 | 
				
			||||||
			struct cmd_results *cmd_res = cmd_results_new(CMD_FAILURE, "output",
 | 
								struct cmd_results *cmd_res = cmd_results_new(CMD_FAILURE, "output",
 | 
				
			||||||
				"Unable to access background file '%s': %s", src, strerror(errno));
 | 
									"Unable to access background file '%s': %s", src, strerror(errno));
 | 
				
			||||||
			free(src);
 | 
								free(src);
 | 
				
			||||||
			wordfree(&p);
 | 
					 | 
				
			||||||
			return cmd_res;
 | 
								return cmd_res;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		output->background = strdup(src);
 | 
							output->background = src;
 | 
				
			||||||
		output->background_option = strdup(mode);
 | 
							output->background_option = strdup(mode);
 | 
				
			||||||
		if (src != p.we_wordv[0]) {
 | 
					 | 
				
			||||||
			free(src);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		wordfree(&p);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		argc -= j + 1; argv += j + 1;
 | 
							argc -= j + 1; argv += j + 1;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue