diff --git a/connection.cpp b/connection.cpp index f65171d..8d608c3 100644 --- a/connection.cpp +++ b/connection.cpp @@ -64,8 +64,7 @@ u_int32_t PackList::sumanddel (timeval t) /* packet may be deleted by caller */ Connection::Connection (Packet * packet) { - if (!ROBUST) - assert (packet != NULL); + assert (packet != NULL); connections = new ConnList (this, connections); sent_packets = new PackList (); recv_packets = new PackList (); diff --git a/connection.h b/connection.h index 6c5925a..8316eef 100644 --- a/connection.h +++ b/connection.h @@ -31,8 +31,7 @@ public: } PackList (Packet * m_val) { - if (!ROBUST) - assert (m_val != NULL); + assert (m_val != NULL); content = new PackListNode(m_val); } ~PackList () diff --git a/cui.cpp b/cui.cpp index 12470d1..c102104 100644 --- a/cui.cpp +++ b/cui.cpp @@ -36,20 +36,14 @@ class Line public: Line (const char * name, double n_recv_value, double n_sent_value, pid_t pid, uid_t uid, const char * n_devicename) { - if (!ROBUST) - { - assert (pid >= 0); - } + assert (pid >= 0); m_name = name; sent_value = n_sent_value; recv_value = n_recv_value; devicename = n_devicename; m_pid = pid; m_uid = uid; - if (!ROBUST) - { - assert (m_pid >= 0); - } + assert (m_pid >= 0); } void show (int row); @@ -72,10 +66,7 @@ char * uid2username (uid_t uid) if (pwd == NULL) { - if (!ROBUST) - { - assert(false); - } + assert(false); return strdup ("unlisted"); } else { return strdup(pwd->pw_name); @@ -85,11 +76,8 @@ char * uid2username (uid_t uid) void Line::show (int row) { - if (!ROBUST) - { - assert (m_pid >= 0); - assert (m_pid <= 100000); - } + assert (m_pid >= 0); + assert (m_pid <= 100000); if (DEBUG || tracemode) { @@ -336,24 +324,21 @@ void do_refresh() double sent_global = 0; double recv_global = 0; - if (!ROBUST) - { - // initialise to null pointers - for (int i = 0; i < nproc; i++) - lines[i] = NULL; - } +#ifndef NDEBUG + // initialise to null pointers + for (int i = 0; i < nproc; i++) + lines[i] = NULL; +#endif while (curproc != NULL) { // walk though its connections, summing up their data, and // throwing away connections that haven't received a package // in the last PROCESSTIMEOUT seconds. - if (!ROBUST) - { - assert (curproc != NULL); - assert (curproc->getVal() != NULL); - assert (nproc == processes->size()); - } + assert (curproc != NULL); + assert (curproc->getVal() != NULL); + assert (nproc == processes->size()); + /* remove timed-out processes (unless it's one of the the unknown process) */ if ((curproc->getVal()->getLastPacket() + PROCESSTIMEOUT <= curtime.tv_sec) && (curproc->getVal() != unknowntcp) @@ -408,30 +393,28 @@ void do_refresh() forceExit("Invalid viewmode"); } uid_t uid = curproc->getVal()->getUid(); - if (!ROBUST) - { - struct passwd * pwuid = getpwuid(uid); - assert (pwuid != NULL); - // value returned by pwuid should not be freed, according to - // Petr Uzel. - //free (pwuid); - assert (curproc->getVal()->pid >= 0); - assert (n < nproc); - } +#ifndef NDEBUG + struct passwd * pwuid = getpwuid(uid); + assert (pwuid != NULL); + // value returned by pwuid should not be freed, according to + // Petr Uzel. + //free (pwuid); +#endif + assert (curproc->getVal()->pid >= 0); + assert (n < nproc); + lines[n] = new Line (curproc->getVal()->name, value_recv, value_sent, curproc->getVal()->pid, uid, curproc->getVal()->devicename); previousproc = curproc; curproc = curproc->next; n++; - if (!ROBUST) - { - assert (nproc == processes->size()); - if (curproc == NULL) - assert (n-1 < nproc); - else - assert (n < nproc); - - } +#ifndef NDEBUG + assert (nproc == processes->size()); + if (curproc == NULL) + assert (n-1 < nproc); + else + assert (n < nproc); +#endif } } diff --git a/inode2prog.cpp b/inode2prog.cpp index 2774bcb..9bddeef 100644 --- a/inode2prog.cpp +++ b/inode2prog.cpp @@ -154,8 +154,7 @@ void get_info_for_pid(char * pid) { free (fromname); continue; } - if (!ROBUST) - assert (usedlen < linklen); + assert (usedlen < linklen); linkname[usedlen] = '\0'; //std::cout << "Linking to: " << linkname << std::endl; get_info_by_linkname (pid, linkname); diff --git a/nethogs.h b/nethogs.h index 20edeb3..c9969bb 100644 --- a/nethogs.h +++ b/nethogs.h @@ -32,12 +32,6 @@ #define DEBUG 0 -// if '0', do extra checks and -// assertions. good for finding bugs -// at an early stage of development. -// for production, should be 1. -#define ROBUST 1 - #define REVERSEHACK 0 // 2 times: 32 characters, 7 ':''s, a ':12345'. @@ -95,8 +89,7 @@ public: int result = inet_pton (AF_INET6, address, &addr6); - if (!ROBUST) - assert (result > 0); + assert (result > 0); sa_family = AF_INET6; } diff --git a/packet.cpp b/packet.cpp index c9ba6ba..d28a3c4 100644 --- a/packet.cpp +++ b/packet.cpp @@ -64,8 +64,7 @@ void getLocal (const char *device, bool tracemode) char address [33]; char ifname [9]; int n_results = sscanf (buffer, "%32[0-9a-f] %*d %*d %*d %*d %8[0-9a-zA-Z]", address, ifname); - if (!ROBUST) - assert (n_results = 2); + assert (n_results = 2); if (strcmp (stripspaces(ifname), device) == 0) { @@ -182,8 +181,7 @@ bool Packet::isOlderThan (timeval t) { bool Packet::Outgoing () { /* must be initialised with getLocal("eth0:1");) */ - if (!ROBUST) - assert (local_addrs != NULL); + assert (local_addrs != NULL); switch (dir) { case dir_outgoing: diff --git a/process.cpp b/process.cpp index e04cc09..cedf7ea 100644 --- a/process.cpp +++ b/process.cpp @@ -65,11 +65,8 @@ int Process::getLastPacket() ConnList * curconn=connections; while (curconn != NULL) { - if (!ROBUST) - { - assert (curconn != NULL); - assert (curconn->getVal() != NULL); - } + assert (curconn != NULL); + assert (curconn->getVal() != NULL); if (curconn->getVal()->getLastPacket() > lastpacket) lastpacket = curconn->getVal()->getLastPacket(); curconn = curconn->getNext(); @@ -83,8 +80,7 @@ Process * findProcess (struct prg_node * node) while (current != NULL) { Process * currentproc = current->getVal(); - if (!ROBUST) - assert (currentproc != NULL); + assert (currentproc != NULL); if (node->pid == currentproc->pid) return current->getVal(); @@ -127,8 +123,10 @@ void reviewUnknown () if (DEBUG || bughuntmode) std::cout << "FIXME: Previously unknown inode " << inode << " now got process - apparently it makes sense to review unknown connections\n"; /* Yay! - but how can this happen? */ - if (!ROBUST) - assert(false); + assert(false); + + /* TODO: this needs some investigation/refactoring - we should never get here due to assert(false) */ + if (previous_conn != NULL) { previous_conn->setNext (curr_conn->getNext()); diff --git a/process.h b/process.h index 6c6abb2..83a92d0 100644 --- a/process.h +++ b/process.h @@ -15,8 +15,7 @@ class ConnList public: ConnList (Connection * m_val, ConnList * m_next) { - if (!ROBUST) - assert (m_val != NULL); + assert (m_val != NULL); val = m_val; next = m_next; } ~ConnList () @@ -63,9 +62,7 @@ public: uid = 0; } void check () { - if (!ROBUST) { - assert (pid >= 0); - } + assert (pid >= 0); } ~Process () @@ -101,8 +98,7 @@ class ProcList public: ProcList (Process * m_val, ProcList * m_next) { - if (!ROBUST) - assert (m_val != NULL); + assert (m_val != NULL); val = m_val; next = m_next; } int size ();