git: b2c5a01722f9 - main - config: push path() into moveifchanged()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 16 Feb 2023 20:37:34 UTC
The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=b2c5a01722f9b18a56ab5459dceba1ae5e50fddd commit b2c5a01722f9b18a56ab5459dceba1ae5e50fddd Author: Kyle Evans <kevans@FreeBSD.org> AuthorDate: 2023-02-16 20:36:16 +0000 Commit: Kyle Evans <kevans@FreeBSD.org> CommitDate: 2023-02-16 20:36:42 +0000 config: push path() into moveifchanged() Every caller path()s its arguments anyways, just simplify these and push path() into moveifchanged(). This fixes a number of memory leaks in the process since path() allocates memory, which we will now free(). While we're here, make sure we close from_fd/to_fd appropriately. CID: 1471040, 1471714, 1472045, 1006907, 1006908 Reviewed by: emaste, imp Differential Revision: https://reviews.freebsd.org/D38641 --- usr.sbin/config/main.cc | 33 ++++++++++++++++++++++----------- usr.sbin/config/mkmakefile.cc | 6 +++--- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/usr.sbin/config/main.cc b/usr.sbin/config/main.cc index f155b7c5387a..66a071401183 100644 --- a/usr.sbin/config/main.cc +++ b/usr.sbin/config/main.cc @@ -579,7 +579,7 @@ configfile(void) p += strlen(KERNCONFTAG); fprintf(fo, "%s", p); fclose(fo); - moveifchanged(path("config.c.new"), path("config.c")); + moveifchanged("config.c.new", "config.c"); cfgfile_removeall(); } @@ -591,6 +591,7 @@ void moveifchanged(const char *from_name, const char *to_name) { char *p, *q; + char *from_path, *to_path; int changed; size_t tsize; struct stat from_sb, to_sb; @@ -598,17 +599,19 @@ moveifchanged(const char *from_name, const char *to_name) changed = 0; - if ((from_fd = open(from_name, O_RDONLY)) < 0) + from_path = path(from_name); + to_path = path(to_name); + if ((from_fd = open(from_path, O_RDONLY)) < 0) err(EX_OSERR, "moveifchanged open(%s)", from_name); - if ((to_fd = open(to_name, O_RDONLY)) < 0) + if ((to_fd = open(to_path, O_RDONLY)) < 0) changed++; if (!changed && fstat(from_fd, &from_sb) < 0) - err(EX_OSERR, "moveifchanged fstat(%s)", from_name); + err(EX_OSERR, "moveifchanged fstat(%s)", from_path); if (!changed && fstat(to_fd, &to_sb) < 0) - err(EX_OSERR, "moveifchanged fstat(%s)", to_name); + err(EX_OSERR, "moveifchanged fstat(%s)", to_path); if (!changed && from_sb.st_size != to_sb.st_size) changed++; @@ -619,23 +622,31 @@ moveifchanged(const char *from_name, const char *to_name) p = (char *)mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0); if (p == MAP_FAILED) - err(EX_OSERR, "mmap %s", from_name); + err(EX_OSERR, "mmap %s", from_path); q = (char *)mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0); if (q == MAP_FAILED) - err(EX_OSERR, "mmap %s", to_name); + err(EX_OSERR, "mmap %s", to_path); changed = memcmp(p, q, tsize); munmap(p, tsize); munmap(q, tsize); } + if (changed) { - if (rename(from_name, to_name) < 0) - err(EX_OSERR, "rename(%s, %s)", from_name, to_name); + if (rename(from_path, to_path) < 0) + err(EX_OSERR, "rename(%s, %s)", from_path, to_path); } else { - if (unlink(from_name) < 0) - err(EX_OSERR, "unlink(%s)", from_name); + if (unlink(from_path) < 0) + err(EX_OSERR, "unlink(%s)", from_path); } + + close(from_fd); + if (to_fd >= 0) + close(to_fd); + + free(from_path); + free(to_path); } static void diff --git a/usr.sbin/config/mkmakefile.cc b/usr.sbin/config/mkmakefile.cc index 472f85fa4bc2..969d32cf1389 100644 --- a/usr.sbin/config/mkmakefile.cc +++ b/usr.sbin/config/mkmakefile.cc @@ -184,7 +184,7 @@ makefile(void) } (void) fclose(ifp); (void) fclose(ofp); - moveifchanged(path("Makefile.new"), path("Makefile")); + moveifchanged("Makefile.new", "Makefile"); } static void @@ -327,7 +327,7 @@ makehints(void) } fprintf(ofp, "\"\\0\"\n};\n"); fclose(ofp); - moveifchanged(path("hints.c.new"), path("hints.c")); + moveifchanged("hints.c.new", "hints.c"); } /* @@ -369,7 +369,7 @@ makeenv(void) } fprintf(ofp, "\"\\0\"\n};\n"); fclose(ofp); - moveifchanged(path("env.c.new"), path("env.c")); + moveifchanged("env.c.new", "env.c"); } static void