I'd like to remove a NULL check in get_process_info() for the sake of
simplicity and to reflect that the process list is *always* sorted
(default is "cpu"), even if not explicitly requested;  this makes it
easier to argue about the code, imho.

Details on why this check is never true:

get_process_info() is the function that obtains the process list and
sorts it using the compare function passed to get_process_info().

The function pointer is always picked from the array ordernames[] using
the getorder() helper:

335 int
336 main(int argc, char *argv[])
337 {
...
400         /* determine sorting order index, if necessary */
401         if (order_name != NULL) {
402                 if ((order_index = getorder(order_name)) == -1) {
403                         new_message(MT_delayed,
404                             " %s: unrecognized sorting order", order_name);
405                         order_index = 0;
406                 }
407         }
...
472         /*
473          *  main loop -- repeat while display count is positive or while it
474          *              indicates infinity (by being -1)
475          */
476         while ((displays == -1) || (displays-- > 0)) {
477                 if (winchflag) {
...
513                 /* get the current set of processes */
514                 processes = get_process_info(&system_info, &ps,
515                     proc_compares[order_index]);

This is the only call path to get_process_info() and thus qsort().

order_name is NULL initialized and only set during getopt() parsing iff
-o is used.

order_index is zero initialized and only updated through CMD_order,
the interactive `o' prompt, which uses getorder() to only update it iff
the given order is valid.

getorder() never yields an invalid index for order_names[], hence
proc_compares[order_index] is never NULL and order_index is never out of
bound.


Feedback? OK?

Index: machine.c
===================================================================
RCS file: /cvs/src/usr.bin/top/machine.c,v
retrieving revision 1.102
diff -u -p -r1.102 machine.c
--- machine.c   6 Jan 2020 20:05:10 -0000       1.102
+++ machine.c   23 Jun 2020 15:27:48 -0000
@@ -491,10 +491,7 @@ get_process_info(struct system_info *si,
                }
        }
 
-       /* if requested, sort the "interesting" processes */
-       if (compare != NULL)
-               qsort((char *) pref, active_procs,
-                   sizeof(struct kinfo_proc *), compare);
+       qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare);
        /* remember active and total counts */
        si->p_total = total_procs;
        si->p_active = pref_len = active_procs;

Reply via email to