Bug 21075

Summary: top: can't allocate sufficient memory
Product: Base System Reporter: lan <lan>
Component: binAssignee: dwmalone
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.1-STABLE   
Hardware: Any   
OS: Any   

Description lan 2000-09-06 09:00:04 UTC
If to run /usr/bin/top in any Xterminal (kvt, konsole, xterm, Eterm)
and a size of the window of the terminal less or it is equal to six
strings on a vertical we receive error messages:
top: can't allocate sufficient memory.
At the greater size - without any errors.

How-To-Repeat: Reduce a vertical size of the window of the Xterminal up to six 
strings (lines) and start /usr/bin/top.
Comment 1 Sheldon Hearn 2000-09-06 11:05:02 UTC
On Wed, 06 Sep 2000 00:50:48 MST, lan@kru.ru wrote:

> >Number:         21075
> >Category:       bin
> >Synopsis:       top: can't allocate sufficient memory

This happens because top needs 7 lines for headers. display_resize()
calculates that the number of lines available is -1 and returns this
value.  As it happens, display_init() interprets this -1 return value as
a memory allocation error.

I'm downloading top-3.5beta9 to see whether this problem is fixed in
that version.

As a work-around, you can use top's -b (batch mode) option.

Ciao,
Sheldon.
Comment 2 Sheldon Hearn 2000-09-06 11:57:11 UTC
The following patch teaches top(1) to exit if it has fewer than
Header_lines available for display.  I don't think there's any more
sensible behaviour; automatically turning on batch mode in this
situation doesn't seem smart to me.

Although both affected files are off the vendor branch, I don't see this
as a critical bug and would prefer to submit this patch to the top
maintainers.  Once they have an "officially adopted" patch for us, we'll
commit that.

Ciao,
Sheldon.

Index: display.c
===================================================================
RCS file: /home/ncvs/src/contrib/top/display.c,v
retrieving revision 1.4
diff -u -d -r1.4 display.c
--- display.c	1999/01/09 20:20:33	1.4
+++ display.c	2000/09/06 10:49:33
@@ -99,6 +99,8 @@
     /* calculate the current dimensions */
     /* if operating in "dumb" mode, we only need one line */
     lines = smart_terminal ? screen_length - Header_lines : 1;
+    if (lines < 0)
+	return(-2);
 
     /* we don't want more than MAX_COLS columns, since the machine-dependent
        modules make static allocations based on MAX_COLS and we don't want
Index: top.c
===================================================================
RCS file: /home/ncvs/src/contrib/top/top.c,v
retrieving revision 1.4
diff -u -d -r1.4 top.c
--- top.c	1999/01/09 20:20:34	1.4
+++ top.c	2000/09/06 10:54:32
@@ -439,6 +439,11 @@
 	fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
 	exit(4);
     }
+    if (max_topn == -2) {
+	fprintf(stderr, "%s: this terminal has fewer than %d lines\n",
+		myname, Header_lines);
+	exit(1);
+    }
     
     /* print warning if user requested more processes than we can display */
     if (topn > max_topn)
Comment 3 Sheldon Hearn freebsd_committer freebsd_triage 2000-09-06 11:58:56 UTC
Responsible Changed
From-To: freebsd-bugs->sheldonh

I'll manage this one.
Comment 4 Sheldon Hearn freebsd_committer freebsd_triage 2000-09-06 12:04:30 UTC
State Changed
From-To: open->feedback

I have contacted the author (wnl@groupsys.com) and am now 
waiting for an "officially adopted" patch.
Comment 5 Sheldon Hearn freebsd_committer freebsd_triage 2001-12-11 09:20:14 UTC
Responsible Changed
From-To: sheldonh->dwmalone

David kindly volunteered to look at this one for me. ;-)
Comment 6 dwmalone freebsd_committer freebsd_triage 2003-04-27 16:32:36 UTC
State Changed
From-To: feedback->closed

Now resolved in -current and -stable.