public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] lib/colors: remove memory leak
@ 2014-07-16 20:54 Sami Kerola
  2014-07-16 20:54 ` [PATCH 2/3] fallocate: avoid unnecessary computation Sami Kerola
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sami Kerola @ 2014-07-16 20:54 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 lib/colors.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/colors.c b/lib/colors.c
index cb342a0..8de1d2f 100644
--- a/lib/colors.c
+++ b/lib/colors.c
@@ -425,9 +425,9 @@ static int colors_add_scheme(struct ul_color_ctl *cc,
 		return -EINVAL;
 
 	rc = cn_sequence(seq0, &seq);
+	free(seq0);
 	if (rc)
 		return rc;
-	free(seq0);
 
 	/* convert logical name (e.g. "red") to real ESC code */
 	if (isalpha(*seq)) {
-- 
2.0.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] fallocate: avoid unnecessary computation
  2014-07-16 20:54 [PATCH 1/3] lib/colors: remove memory leak Sami Kerola
@ 2014-07-16 20:54 ` Sami Kerola
  2014-07-16 20:54 ` [PATCH 3/3] fstrim: avoid TOCTOU race Sami Kerola
  2014-07-17 12:02 ` [PATCH 1/3] lib/colors: remove memory leak Karel Zak
  2 siblings, 0 replies; 4+ messages in thread
From: Sami Kerola @ 2014-07-16 20:54 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Where POSIX_FADV_SEQUENTIAL and HAVE_POSIX_FADVISE are not available it
is waste of resources to have variables that are meaningful only for
posix_fadvise().  Also initialize the variables immediately to correct
values, and since cachesz is never changed mark it read only.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 sys-utils/fallocate.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/sys-utils/fallocate.c b/sys-utils/fallocate.c
index 93fd3b4..c619f64 100644
--- a/sys-utils/fallocate.c
+++ b/sys-utils/fallocate.c
@@ -174,17 +174,12 @@ static void dig_holes(int fd, off_t off, off_t len)
 {
 	off_t end = len ? off + len : 0;
 	off_t hole_start = 0, hole_sz = 0;
-	off_t cache_start = 0;
 	uintmax_t ct = 0;
-	size_t bufsz, cachesz;
+	size_t  bufsz;
 	char *buf;
 	struct stat st;
-
-	if (fstat(fd, &st) != 0)
-		err(EXIT_FAILURE, _("stat failed %s"), filename);
-
-	bufsz = st.st_blksize;
-
+#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
+	off_t cache_start = off;
 	/*
 	 * We don't want to call POSIX_FADV_DONTNEED to discard cached
 	 * data in PAGE_SIZE steps. IMHO it's overkill (too many syscalls).
@@ -193,15 +188,19 @@ static void dig_holes(int fd, off_t off, off_t len)
 	 * a good compromise.
 	 *					    -- kzak Feb-2014
 	 */
-	cachesz = getpagesize() * 256;
+	const size_t cachesz = getpagesize() * 256;
+#endif
+
+	if (fstat(fd, &st) != 0)
+		err(EXIT_FAILURE, _("stat failed %s"), filename);
+
+	bufsz = st.st_blksize;
 
 	if (lseek(fd, off, SEEK_SET) < 0)
 		err(EXIT_FAILURE, _("seek on %s failed"), filename);
 
 	/* buffer + extra space for is_nul() sentinel */
 	buf = xmalloc(bufsz + sizeof(uintptr_t));
-	cache_start = off;
-
 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
 	posix_fadvise(fd, off, 0, POSIX_FADV_SEQUENTIAL);
 #endif
-- 
2.0.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] fstrim: avoid TOCTOU race
  2014-07-16 20:54 [PATCH 1/3] lib/colors: remove memory leak Sami Kerola
  2014-07-16 20:54 ` [PATCH 2/3] fallocate: avoid unnecessary computation Sami Kerola
@ 2014-07-16 20:54 ` Sami Kerola
  2014-07-17 12:02 ` [PATCH 1/3] lib/colors: remove memory leak Karel Zak
  2 siblings, 0 replies; 4+ messages in thread
From: Sami Kerola @ 2014-07-16 20:54 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 sys-utils/fstrim.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
index d52d831..5a238c7 100644
--- a/sys-utils/fstrim.c
+++ b/sys-utils/fstrim.c
@@ -67,7 +67,12 @@ static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
 	/* kernel modifies the range */
 	memcpy(&range, rangetpl, sizeof(range));
 
-	if (stat(path, &sb) == -1) {
+	fd = open(path, O_RDONLY);
+	if (fd < 0) {
+		warn(_("cannot open %s"), path);
+		return -1;
+	}
+	if (fstat(fd, &sb) == -1) {
 		warn(_("stat failed %s"), path);
 		return -1;
 	}
@@ -75,12 +80,6 @@ static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
 		warnx(_("%s: not a directory"), path);
 		return -1;
 	}
-
-	fd = open(path, O_RDONLY);
-	if (fd < 0) {
-		warn(_("cannot open %s"), path);
-		return -1;
-	}
 	errno = 0;
 	if (ioctl(fd, FITRIM, &range)) {
 		int rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -1;
-- 
2.0.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] lib/colors: remove memory leak
  2014-07-16 20:54 [PATCH 1/3] lib/colors: remove memory leak Sami Kerola
  2014-07-16 20:54 ` [PATCH 2/3] fallocate: avoid unnecessary computation Sami Kerola
  2014-07-16 20:54 ` [PATCH 3/3] fstrim: avoid TOCTOU race Sami Kerola
@ 2014-07-17 12:02 ` Karel Zak
  2 siblings, 0 replies; 4+ messages in thread
From: Karel Zak @ 2014-07-17 12:02 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux

On Wed, Jul 16, 2014 at 09:54:56PM +0100, Sami Kerola wrote:
>  lib/colors.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

 Applied all 3 patches, thanks. 

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-07-17 12:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-07-16 20:54 [PATCH 1/3] lib/colors: remove memory leak Sami Kerola
2014-07-16 20:54 ` [PATCH 2/3] fallocate: avoid unnecessary computation Sami Kerola
2014-07-16 20:54 ` [PATCH 3/3] fstrim: avoid TOCTOU race Sami Kerola
2014-07-17 12:02 ` [PATCH 1/3] lib/colors: remove memory leak Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox