From 28ef2bf7ec01aab25ea528db0efc5f416b40f7d1 Mon Sep 17 00:00:00 2001
From: Morten Stevens <mstevens@fedoraproject.org>
Date: Mon, 15 Feb 2016 15:43:42 +0100
Subject: [PATCH] Fix for #1308359

---
 htop-1.0-libpagemap.patch            | 425 ---------------------------
 htop-2.0.0-fix-buffer-reuse.patch    |  22 ++
 htop-2.0.0-fix-emptying-column.patch |  25 ++
 htop.spec                            |  10 +-
 4 files changed, 56 insertions(+), 426 deletions(-)
 delete mode 100644 htop-1.0-libpagemap.patch
 create mode 100644 htop-2.0.0-fix-buffer-reuse.patch
 create mode 100644 htop-2.0.0-fix-emptying-column.patch

diff --git a/htop-1.0-libpagemap.patch b/htop-1.0-libpagemap.patch
deleted file mode 100644
index 051e815..0000000
--- a/htop-1.0-libpagemap.patch
+++ /dev/null
@@ -1,425 +0,0 @@
-diff -Naur htop-1.0/configure.ac.orig htop-1.0/configure.ac
---- htop-1.0/configure.ac.orig   2011-11-21 03:46:48.000000000 +0100
-+++ htop-1.0/configure.ac        2012-02-07 17:21:59.098852884 +0100
-@@ -23,11 +23,13 @@
- 
- # Checks for libraries.
- AC_CHECK_LIB([m], [ceil], [], [missing_libraries="$missing_libraries libm"])
-+AC_CHECK_LIB([pagemap], [init_pgmap_table], [], [], [])
-+AC_CHECK_LIB([pthread], [pthread_create], [], [], [])
- 
- # Checks for header files.
- AC_HEADER_DIRENT
- AC_HEADER_STDC
--AC_CHECK_HEADERS([stdlib.h string.h strings.h sys/param.h sys/time.h unistd.h curses.h],[:],[
-+AC_CHECK_HEADERS([stdlib.h string.h strings.h sys/param.h sys/time.h unistd.h curses.h pthread.h],[:],[
-   missing_headers="$missing_headers $ac_header"
- ])
- AC_CHECK_HEADERS([execinfo.h],[:],[:])
-diff -Naur htop-1.0/htop.1.orig htop-1.0/htop.1
---- htop-1.0/htop.1.orig 2012-02-07 17:28:48.935889755 +0100
-+++ htop-1.0/htop.1      2012-02-07 17:27:44.087881744 +0100
-@@ -32,6 +32,9 @@
- \fB\-u \-\-user=USERNAME\fR
- Show only the processes of a given user
- .TP
-+\fB\-p\fR
-+Start working thread for pagemap memory stats
-+.TP
- \fB\-s \-\-sort\-key COLUMN\fR
- Sort by this column (use \-\-sort\-key help for a column list)
- .TP
-diff -Naur htop-1.0/htop.1.in.orig htop-1.0/htop.1.in
---- htop-1.0/htop.1.in.orig      2011-09-27 01:59:36.000000000 +0200
-+++ htop-1.0/htop.1.in   2012-02-07 17:25:23.161867112 +0100
-@@ -32,6 +32,9 @@
- \fB\-u \-\-user=USERNAME\fR
- Show only the processes of a given user
- .TP
-+\fB\-p\fR
-+Start working thread for pagemap memory stats
-+.TP
- \fB\-s \-\-sort\-key COLUMN\fR
- Sort by this column (use \-\-sort\-key help for a column list)
- .TP
-diff -Naur htop-1.0/htop.c.orig htop-1.0/htop.c
---- htop-1.0/htop.c.orig	2011-11-21 03:48:11.000000000 +0100
-+++ htop-1.0/htop.c	2012-02-07 17:36:15.348965585 +0100
-@@ -13,6 +13,7 @@
- #include <stdbool.h>
- #include <locale.h>
- #include <getopt.h>
-+#include <pthread.h>
- 
- #include "ProcessList.h"
- #include "CRT.h"
-@@ -51,6 +52,9 @@
-          "-C --no-color         Use a monochrome color scheme\n"
-          "-d --delay=DELAY      Set the delay between updates, in tenths of seconds\n"
-          "-h --help             Print this help screen\n"
-+#ifdef HAVE_LIBPAGEMAP
-+         "-p --pagemap          Count memory stats from pagemap kernel interface\n"
-+#endif
-          "-s --sort-key=COLUMN  Sort by COLUMN (try --sort-key=help for a list)\n"
-          "-u --user=USERNAME    Show only processes of a given user\n"
-          "-v --version          Print version info\n"
-@@ -238,6 +242,16 @@
-    ProcessList_printHeader(pl, Panel_getHeader(panel));
- }
- 
-+#ifdef HAVE_LIBPAGEMAP
-+static void * pagemapCnt(ProcessList * pl) {
-+   while(1) {
-+   pl->pagemap_table = init_pgmap_table(pl->pagemap_table);
-+   open_pgmap_table(pl->pagemap_table,0);
-+   }
-+   return;
-+}
-+#endif
-+
- typedef struct IncBuffer_ {
-    char buffer[INCSEARCH_MAX];
-    int index;
-@@ -255,6 +269,7 @@
-    bool userOnly = false;
-    uid_t userId = 0;
-    int usecolors = 1;
-+   int pagemap_enable = 0;
-    TreeType treeType = TREE_TYPE_AUTO;
- 
-    int opt, opti=0;
-@@ -267,6 +282,9 @@
-       {"user",     required_argument,   0, 'u'},
-       {"no-color", no_argument,         0, 'C'},
-       {"no-colour",no_argument,         0, 'C'},
-+#ifdef HAVE_LIBPAGEMAP
-+      {"pagemap",  no_argument,         0, 'p'},
-+#endif
-       {0,0,0,0}
-    };
-    int sortKey = 0;
-@@ -280,7 +298,7 @@
-       setlocale(LC_CTYPE, "");
- 
-    /* Parse arguments */
--   while ((opt = getopt_long(argc, argv, "hvCs:d:u:", long_opts, &opti))) {
-+   while ((opt = getopt_long(argc, argv, "hpvCs:d:u:", long_opts, &opti))) {
-       if (opt == EOF) break;
-       switch (opt) {
-          case 'h':
-@@ -320,6 +338,11 @@
-          case 'C':
-             usecolors=0;
-             break;
-+#ifdef HAVE_LIBPAGEMAP
-+         case 'p':
-+            pagemap_enable=1;
-+            break;
-+#endif
-          default:
-             exit(1);
-       }
-@@ -432,6 +455,18 @@
-    int ch = ERR;
-    int closeTimeout = 0;
- 
-+#ifdef HAVE_LIBPAGEMAP
-+   // declare threading stuff
-+   static pthread_t libpagemap_thread;
-+   static pthread_attr_t t_attr;
-+   if (pagemap_enable) {
-+       // start thread
-+       pthread_attr_init(&t_attr);
-+       pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
-+       pthread_create(&libpagemap_thread, &t_attr, (void * (*)(void *)) &pagemapCnt, (void *) pl);
-+   }
-+#endif
-+
-    while (!quit) {
-       gettimeofday(&tv, NULL);
-       newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);
-@@ -912,6 +947,12 @@
-    if (settings->changed)
-       Settings_write(settings);
-    Header_delete(header);
-+#ifdef HAVE_LIBPAGEMAP
-+   if (pagemap_enable) {
-+      pthread_cancel(libpagemap_thread);
-+      free_pgmap_table(pl->pagemap_table);
-+   }
-+#endif
-    ProcessList_delete(pl);
-    FunctionBar_delete((Object*)incFilter.bar);
-    FunctionBar_delete((Object*)incSearch.bar);
-diff -ru --exclude=config.h htop-0.9/htop.h htop-0.9-patched/htop.h
---- htop-0.9/htop.h	2010-11-24 19:45:40.000000000 +0100
-+++ htop-0.9-patched/htop.h	2011-03-09 16:48:37.672644570 +0100
-@@ -17,6 +17,9 @@
- #include <stdbool.h>
- #include <locale.h>
- #include <getopt.h>
-+#ifdef HAVE_LIBPAGEMAP
-+#include <pthread.h>
-+#endif
- 
- #include "ProcessList.h"
- #include "CRT.h"
-diff -Naur htop-1.0/Process.c.orig htop-1.0/Process.c
---- htop-1.0/Process.c.orig	2011-11-21 03:47:49.000000000 +0100
-+++ htop-1.0/Process.c	2012-02-07 17:41:02.322032224 +0100
-@@ -72,6 +72,9 @@
-    #ifdef HAVE_CGROUP
-    CGROUP,
-    #endif
-+   #ifdef HAVE_LIBPAGEMAP 
-+   M_USS, M_PSS, M_SWAP,
-+   #endif
-    LAST_PROCESSFIELD
- } ProcessField;
- 
-@@ -115,7 +118,7 @@
-    #ifdef DEBUG
-    long int itrealvalue;
-    unsigned long int vsize;
--   long int rss;
-+   long 1nt rss;
-    unsigned long int rlim;
-    unsigned long int startcode;
-    unsigned long int endcode;
-@@ -139,6 +142,9 @@
-    int m_drs;
-    int m_lrs;
-    int m_dt;
-+   unsigned int m_uss;
-+   unsigned int m_pss;
-+   unsigned int m_swap;
-    uid_t st_uid;
-    float percent_cpu;
-    float percent_mem;
-@@ -198,6 +204,9 @@
- #ifdef HAVE_CGROUP
-    "CGROUP",
- #endif
-+#ifdef HAVE_LIBPAGEMAP
-+   "M_USS", "M_PSS", "M_SWAP",
-+#endif
- "*** report bug! ***"
- };
- 
-@@ -223,6 +232,9 @@
- #ifdef HAVE_CGROUP
-    "    CGROUP ",
- #endif
-+#ifdef HAVE_LIBPAGEMAP
-+   "  USS ", "  PSS ", " SWAP ",
-+#endif
- "*** report bug! ***"
- };
- 
-@@ -440,6 +452,32 @@
-    case M_SIZE: Process_humanNumber(this, str, this->m_size * PAGE_SIZE_KB); return;
-    case M_RESIDENT: Process_humanNumber(this, str, this->m_resident * PAGE_SIZE_KB); return;
-    case M_SHARE: Process_humanNumber(this, str, this->m_share * PAGE_SIZE_KB); return;
-+   #ifdef HAVE_LIBPAGEMAP
-+   case M_USS:
-+      if (Process_getuid == 0 && this->pl->pagemap_table != NULL) {
-+          Process_humanNumber(this, str, this->m_uss * PAGE_SIZE_KB); 
-+          return;
-+      } else {
-+          snprintf(buffer, n, "    - ");
-+      }
-+      break;
-+   case M_PSS:
-+      if (Process_getuid == 0 && this->pl->pagemap_table != NULL) {
-+          Process_humanNumber(this, str, this->m_pss * PAGE_SIZE_KB); 
-+          return;
-+      } else {
-+          snprintf(buffer, n, "    - ");
-+      }
-+      break;
-+   case M_SWAP:
-+      if ((Process_getuid == 0 || Process_getuid == this->st_uid) && this->pl->pagemap_table != NULL) {
-+          Process_humanNumber(this, str, this->m_swap * PAGE_SIZE_KB); 
-+          return;
-+      } else {
-+          snprintf(buffer, n, "    - ");
-+      }
-+      break;
-+   #endif
-    case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break;
-    case USER: {
-       if (Process_getuid != (int) this->st_uid)
-@@ -677,6 +715,14 @@
-       return (p2->m_resident - p1->m_resident);
-    case M_SHARE:
-       return (p2->m_share - p1->m_share);
-+   #ifdef HAVE_LIBPAGEMAP
-+   case M_USS:
-+      return (p2->m_uss - p1->m_uss);
-+   case M_PSS:
-+      return (p2->m_pss - p1->m_pss);
-+   case M_SWAP:
-+      return (p2->m_swap - p1->m_swap);
-+   #endif
-    case PERCENT_CPU:
-       return (p2->percent_cpu > p1->percent_cpu ? 1 : -1);
-    case PERCENT_MEM:
-diff -ru --exclude=config.h htop-0.9/Process.h htop-0.9-patched/Process.h
---- htop-0.9/Process.h	2010-11-23 16:56:32.000000000 +0100
-+++ htop-0.9-patched/Process.h	2011-03-09 16:44:07.070449492 +0100
-@@ -74,6 +74,9 @@
-    #ifdef HAVE_CGROUP
-    CGROUP,
-    #endif
-+   #ifdef HAVE_LIBPAGEMAP 
-+   M_USS, M_PSS, M_SWAP,
-+   #endif
-    LAST_PROCESSFIELD
- } ProcessField;
- 
-@@ -117,7 +120,7 @@
-    #ifdef DEBUG
-    long int itrealvalue;
-    unsigned long int vsize;
--   long int rss;
-+   long 1nt rss;
-    unsigned long int rlim;
-    unsigned long int startcode;
-    unsigned long int endcode;
-@@ -141,6 +144,9 @@
-    int m_drs;
-    int m_lrs;
-    int m_dt;
-+   unsigned int m_uss;
-+   unsigned int m_pss;
-+   unsigned int m_swap;
-    uid_t st_uid;
-    float percent_cpu;
-    float percent_mem;
-diff -Naur htop-1.0/ProcessList.c.orig htop-1.0/ProcessList.c
---- htop-1.0/ProcessList.c.orig	2011-11-21 03:47:37.000000000 +0100
-+++ htop-1.0/ProcessList.c	2012-02-07 17:43:52.289077822 +0100
-@@ -10,6 +10,9 @@
- #include "config.h"
- #endif
- 
-+#ifdef HAVE_LIBPAGEMAP
-+#include "libpagemap.h"
-+#endif
- #include "ProcessList.h"
- #include "Process.h"
- #include "Vector.h"
-@@ -28,6 +31,7 @@
- #include <sys/utsname.h>
- #include <stdarg.h>
- #include <math.h>
-+#include <pthread.h>
- 
- #include "debug.h"
- #include <assert.h>
-@@ -148,6 +152,10 @@
-    bool countCPUsFromZero;
-    const char **treeStr;
- 
-+#ifdef HAVE_LIBPAGEMAP
-+   pagemap_tbl * pagemap_table;
-+#endif
-+
- } ProcessList;
- 
- }*/
-@@ -230,6 +238,9 @@
-    this->detailedCPUTime = false;
-    this->countCPUsFromZero = false;
-    this->treeStr = NULL;
-+#ifdef HAVE_LIBPAGEMAP
-+   this->pagemap_table = NULL;
-+#endif   
- 
-    return this;
- }
-@@ -496,6 +507,29 @@
-    return (num == 7);
- }
- 
-+#ifdef HAVE_LIBPAGEMAP
-+
-+static bool ProcessList_readPagemap(ProcessList* plist ,Process* process, const char * name) {
-+
-+   process_pagemap_t* p = NULL;
-+   int pid = 0;
-+
-+   pid = atoi(name);
-+
-+   if ((p = get_single_pgmap(plist->pagemap_table,pid)) == NULL) {
-+      process->m_uss = 0;
-+      process->m_pss = 0;
-+      process->m_swap = 0;
-+   } else {
-+      process->m_uss = p->uss;
-+      process->m_pss = p->pss;
-+      process->m_swap = p->swap;
-+   }
-+   return true;
-+}
-+
-+#endif
-+
- #ifdef HAVE_OPENVZ
- 
- static void ProcessList_readOpenVZData(Process* process, const char* dirname, const char* name) {
-@@ -660,6 +694,11 @@
-       if (! ProcessList_readStatmFile(process, dirname, name))
-          goto errorReadingProcess;
- 
-+      #ifdef HAVE_LIBPAGEMAP
-+      if (! ProcessList_readPagemap(this, process, name))
-+         goto errorReadingProcess;
-+      #endif
-+
-       process->show = ! ((hideKernelThreads && Process_isKernelThread(process)) || (hideUserlandThreads && Process_isUserlandThread(process)));
- 
-       char command[MAX_NAME+1];
-diff -Naur htop-1.0/ProcessList.h.orig htop-1.0/ProcessList.h
---- htop-1.0/ProcessList.h.orig	2011-11-21 03:49:01.000000000 +0100
-+++ htop-1.0/ProcessList.h	2012-02-07 17:45:46.224110828 +0100
-@@ -14,6 +14,9 @@
- #include "config.h"
- #endif
- 
-+#ifdef HAVE_LIBPAGEMAP
-+#include "libpagemap.h"
-+#endif
- #include "Process.h"
- #include "Vector.h"
- #include "UsersTable.h"
-@@ -31,6 +34,7 @@
- #include <sys/utsname.h>
- #include <stdarg.h>
- #include <math.h>
-+#include <pthread.h>
- 
- #include "debug.h"
- #include <assert.h>
-@@ -150,6 +154,10 @@
-    bool countCPUsFromZero;
-    const char **treeStr;
- 
-+#ifdef HAVE_LIBPAGEMAP
-+   pagemap_tbl * pagemap_table;
-+#endif
-+
- } ProcessList;
- 
- 
-@@ -175,6 +183,10 @@
- 
- #endif
- 
-+#ifdef HAVE_LIBPAGEMAP
-+
-+#endif
-+
- #ifdef HAVE_OPENVZ
- 
- #endif
diff --git a/htop-2.0.0-fix-buffer-reuse.patch b/htop-2.0.0-fix-buffer-reuse.patch
new file mode 100644
index 0000000..f44e43e
--- /dev/null
+++ b/htop-2.0.0-fix-buffer-reuse.patch
@@ -0,0 +1,22 @@
+From 0b70439316b4e4608c0916317ded7e6e56982de6 Mon Sep 17 00:00:00 2001
+From: Hisham <hisham@gobolinux.org>
+Date: Sat, 13 Feb 2016 02:18:28 -0200
+Subject: [PATCH] Fix buffer reuse.
+
+---
+ linux/LinuxProcessList.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c
+index 591210e..ec643ab 100644
+--- a/linux/LinuxProcessList.c
++++ b/linux/LinuxProcessList.c
+@@ -446,7 +446,7 @@ static void LinuxProcessList_readOomData(LinuxProcess* process, const char* dirn
+ }
+ 
+ static void setCommand(Process* process, const char* command, int len) {
+-   if (process->comm && process->commLen <= len) {
++   if (process->comm && process->commLen >= len) {
+       strncpy(process->comm, command, len + 1);
+    } else {
+       free(process->comm);
diff --git a/htop-2.0.0-fix-emptying-column.patch b/htop-2.0.0-fix-emptying-column.patch
new file mode 100644
index 0000000..1b39bef
--- /dev/null
+++ b/htop-2.0.0-fix-emptying-column.patch
@@ -0,0 +1,25 @@
+From 0e3cf6975f96291fa203332aad9e44086ff8f2f2 Mon Sep 17 00:00:00 2001
+From: Hisham <hisham@gobolinux.org>
+Date: Fri, 12 Feb 2016 23:33:53 -0200
+Subject: [PATCH] Fix crash when emptying column. Closes #381.
+
+---
+ MetersPanel.c | 5 ++++-
+ 1 file changed, 4 insertions(+), 1 deletion(-)
+
+diff --git a/MetersPanel.c b/MetersPanel.c
+index bdbe83e..302b6d1 100644
+--- a/MetersPanel.c
++++ b/MetersPanel.c
+@@ -50,7 +50,10 @@ static void MetersPanel_delete(Object* object) {
+ void MetersPanel_setMoving(MetersPanel* this, bool moving) {
+    Panel* super = (Panel*) this;
+    this->moving = moving;
+-   ((ListItem*)Panel_getSelected(super))->moving = moving;
++   ListItem* selected = (ListItem*)Panel_getSelected(super);
++   if (selected) {
++      selected->moving = moving;
++   }
+    if (!moving) {
+       Panel_setSelectionColor(super, CRT_colors[PANEL_SELECTION_FOCUS]);
+       Panel_setDefaultBar(super);
diff --git a/htop.spec b/htop.spec
index bacece2..7c736a1 100644
--- a/htop.spec
+++ b/htop.spec
@@ -1,11 +1,13 @@
 Name: htop
 Version: 2.0.0
-Release: 1%{?dist}
+Release: 2%{?dist}
 Summary: Interactive process viewer
 Group: Applications/System
 License: GPL+
 URL: http://hisham.hm/htop/
 Source0: http://hisham.hm/htop/releases/%{version}/%{name}-%{version}.tar.gz
+Patch0: htop-2.0.0-fix-emptying-column.patch
+Patch1: htop-2.0.0-fix-buffer-reuse.patch
 
 BuildRequires: desktop-file-utils
 BuildRequires: ncurses-devel
@@ -18,6 +20,8 @@ top(1).
 
 %prep
 %setup -q
+%patch0 -p1
+%patch1 -p1
 sed -i s#"INSTALL_DATA = @INSTALL_DATA@"#"INSTALL_DATA = @INSTALL_DATA@ -p"# Makefile.in
 
 %build
@@ -53,6 +57,10 @@ rm -rf $RPM_BUILD_ROOT%{_datadir}/applications/
 %{_mandir}/man1/htop.1*
 
 %changelog
+* Mon Feb 15 2016 Morten Stevens <mstevens@fedoraproject.org> - 2.0.0-2
+- Fix buffer reuse (#1308359)
+- Fix crash when emptying column
+
 * Thu Feb 11 2016 Morten Stevens <mstevens@fedoraproject.org> - 2.0.0-1
 - Update to 2.0.0 (#1306817)