coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] uptime: fix a memory leak after printing results


From: Anton Ovchinnikov
Subject: [PATCH] uptime: fix a memory leak after printing results
Date: Sat, 6 Jul 2013 17:13:21 +0400

The issue was found after checking with 'valgrind'.
BTW, several other programs also have some memory issues. I can take a
look at them as well in the nearest future.
--
Best regards,
Anton


>From 19a75ddf4394443c4253323a20508943f0a607c8 Mon Sep 17 00:00:00 2001
From: Anton Ovchinnikov <address@hidden>
Date: Sat, 6 Jul 2013 15:00:02 +0400
Subject: [PATCH] uptime: fix a memory leak after printing results

* src/uptime.c (uptime): Free utmp_buf returned from read_utmp.
Initialize utmp_buf and n_users explicitly to avoid an uninitialized
usage when (HAVE_UTMPX_H || HAVE_UTMP_H) macro condition is not met.
---
 src/uptime.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/uptime.c b/src/uptime.c
index 8e8f2ca..642d9d1 100644
--- a/src/uptime.c
+++ b/src/uptime.c
@@ -175,8 +175,8 @@ print_uptime (size_t n, const STRUCT_UTMP *this)
 static void
 uptime (const char *filename, int options)
 {
-  size_t n_users;
-  STRUCT_UTMP *utmp_buf;
+  size_t n_users = 0;
+  STRUCT_UTMP *utmp_buf = NULL;

 #if HAVE_UTMPX_H || HAVE_UTMP_H
   if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
@@ -184,6 +184,7 @@ uptime (const char *filename, int options)
 #endif

   print_uptime (n_users, utmp_buf);
+  free (utmp_buf);
 }

 void
--
1.8.1.4



reply via email to

[Prev in Thread] Current Thread [Next in Thread]