From owner-svn-src-head@freebsd.org Wed Feb 20 20:17:55 2019 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 9D27714DA555; Wed, 20 Feb 2019 20:17:55 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 31F466B735; Wed, 20 Feb 2019 20:17:55 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 2336EA7D2; Wed, 20 Feb 2019 20:17:55 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x1KKHtlu062413; Wed, 20 Feb 2019 20:17:55 GMT (envelope-from dim@FreeBSD.org) Received: (from dim@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x1KKHtGZ062412; Wed, 20 Feb 2019 20:17:55 GMT (envelope-from dim@FreeBSD.org) Message-Id: <201902202017.x1KKHtGZ062412@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: dim set sender to dim@FreeBSD.org using -f From: Dimitry Andric Date: Wed, 20 Feb 2019 20:17:55 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r344381 - head/usr.bin/top X-SVN-Group: head X-SVN-Commit-Author: dim X-SVN-Commit-Paths: head/usr.bin/top X-SVN-Commit-Revision: 344381 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 31F466B735 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.98 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_SHORT(-0.98)[-0.981,0]; NEURAL_HAM_LONG(-1.00)[-0.999,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Feb 2019 20:17:55 -0000 Author: dim Date: Wed Feb 20 20:17:54 2019 New Revision: 344381 URL: https://svnweb.freebsd.org/changeset/base/344381 Log: Fix more AddressSanitizer violations in usr.bin/top In line_update(), set lastcol correctly after moving to any non-zero column, so the "overwrite old stuff" part does not attempt to address negative offsets in the current line. Rewrite setup_buffer() to always allocate at least 80 characters, otherwise various calls to summary_format() will overwrite the end of the buffers, if the screen width gets small enough. MFC after: 1 week Modified: head/usr.bin/top/display.c Modified: head/usr.bin/top/display.c ============================================================================== --- head/usr.bin/top/display.c Wed Feb 20 20:09:59 2019 (r344380) +++ head/usr.bin/top/display.c Wed Feb 20 20:17:54 2019 (r344381) @@ -378,13 +378,13 @@ u_procstates(int total, int *brkdn) if (ltotal != total) { /* move and overwrite */ -if (x_procstate == 0) { - Move_to(x_procstate, y_procstate); -} -else { - /* cursor is already there...no motion needed */ - assert(lastline == 1); -} + if (x_procstate == 0) { + Move_to(x_procstate, y_procstate); + } + else { + /* cursor is already there...no motion needed */ + assert(lastline == 1); + } printf("%d", total); /* if number of digits differs, rewrite the label */ @@ -1205,7 +1205,7 @@ line_update(char *old, char *new, int start, int line) cursor_on_line = true; putchar(ch); *old = ch; - lastcol = 1; + lastcol = start + 1; } old++; @@ -1341,33 +1341,27 @@ i_uptime(struct timeval *bt, time_t *tod) } } +#define SETUPBUFFER_MIN_SCREENWIDTH 80 #define SETUPBUFFER_REQUIRED_ADDBUFSIZ 2 static char * setup_buffer(char *buffer, int addlen) { - char *b = NULL; + size_t len; - if (NULL == buffer) { - setup_buffer_bufsiz = screen_width; - b = calloc(setup_buffer_bufsiz + addlen + - SETUPBUFFER_REQUIRED_ADDBUFSIZ, - sizeof(char)); - } else { - if (screen_width > setup_buffer_bufsiz) { - setup_buffer_bufsiz = screen_width; - free(buffer); - b = calloc(setup_buffer_bufsiz + addlen + - SETUPBUFFER_REQUIRED_ADDBUFSIZ, - sizeof(char)); - } else { - b = buffer; - } - } + setup_buffer_bufsiz = screen_width; + if (setup_buffer_bufsiz < SETUPBUFFER_MIN_SCREENWIDTH) + { + setup_buffer_bufsiz = SETUPBUFFER_MIN_SCREENWIDTH; + } - if (NULL == b) { - errx(4, "can't allocate sufficient memory"); - } + free(buffer); + len = setup_buffer_bufsiz + addlen + SETUPBUFFER_REQUIRED_ADDBUFSIZ; + buffer = calloc(len, sizeof(char)); + if (buffer == NULL) + { + errx(4, "can't allocate sufficient memory"); + } - return b; + return buffer; }