Date: Wed, 20 Feb 2019 20:28:48 +0000 (UTC) From: Dimitry Andric <dim@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r344382 - stable/12/usr.bin/top Message-ID: <201902202028.x1KKSmDq069247@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: dim Date: Wed Feb 20 20:28:48 2019 New Revision: 344382 URL: https://svnweb.freebsd.org/changeset/base/344382 Log: MFC r343165 (by jhibbits): Fix top(1) long options handling getopt_long(3) requires the long options be terminated by a NULL block. Without the terminator, an invalid long option results in a segmentation fault. Reported by: Brandon Bergren MFC r343957: Fix multiple warnings in usr.bin/top about discarded qualifiers from both clang and gcc, by either constifying variables, or when that is not possible, using __DECONST. MFC r343958: Fix multiple warnings in usr.bin/top about variables shadowing global declarations from base gcc, by renaming those variables. MFC r343959: Fix the first couple of AddressSanitizer violations in usr.bin/top. Avoid setting zero bytes beyond the length of the 'thisline' parameters in i_process() and u_process(), and don't attempt to memset a negative number of bytes. Modified: stable/12/usr.bin/top/Makefile stable/12/usr.bin/top/display.c stable/12/usr.bin/top/display.h stable/12/usr.bin/top/machine.c stable/12/usr.bin/top/top.c stable/12/usr.bin/top/username.c stable/12/usr.bin/top/utils.c stable/12/usr.bin/top/utils.h Directory Properties: stable/12/ (props changed) Modified: stable/12/usr.bin/top/Makefile ============================================================================== --- stable/12/usr.bin/top/Makefile Wed Feb 20 20:17:54 2019 (r344381) +++ stable/12/usr.bin/top/Makefile Wed Feb 20 20:28:48 2019 (r344382) @@ -7,14 +7,5 @@ SRCS= commands.c display.c machine.c screen.c top.c \ username.c utils.c MAN= top.1 -.if ${COMPILER_TYPE} == "gcc" -.if ${COMPILER_VERSION} >= 50000 -CFLAGS.gcc=-Wno-error=discarded-qualifiers -Wno-error=incompatible-pointer-types -.else #base gcc -NO_WERROR= -.endif -.endif -CFLAGS.clang=-Wno-error=incompatible-pointer-types-discards-qualifiers - LIBADD= ncursesw m kvm jail util sbuf .include <bsd.prog.mk> Modified: stable/12/usr.bin/top/display.c ============================================================================== --- stable/12/usr.bin/top/display.c Wed Feb 20 20:17:54 2019 (r344381) +++ stable/12/usr.bin/top/display.c Wed Feb 20 20:28:48 2019 (r344382) @@ -184,7 +184,7 @@ int display_init(struct statics * statics) { int lines; - char **pp; + const char * const *pp; int *ip; int i; @@ -516,8 +516,8 @@ void z_cpustates(void) { int i = 0; - const char **names; - char *thisname; + const char * const *names; + const char *thisname; int cpu, value; for (cpu = 0; cpu < num_cpus; cpu++) { @@ -751,7 +751,7 @@ static int header_length; * allocated area with the trimmed header. */ -const char * +char * trim_header(const char *text) { char *s; @@ -829,7 +829,11 @@ i_process(int line, char *thisline) } /* truncate the line to conform to our current screen width */ - thisline[screen_width] = '\0'; + int len = strlen(thisline); + if (screen_width < len) + { + thisline[screen_width] = '\0'; + } /* write the line out */ fputs(thisline, stdout); @@ -839,7 +843,10 @@ i_process(int line, char *thisline) p = stpcpy(base, thisline); /* zero fill the rest of it */ - memset(p, 0, screen_width - (p - base)); + if (p - base < screen_width) + { + memset(p, 0, screen_width - (p - base)); + } } void @@ -853,7 +860,11 @@ u_process(int line, char *newline) bufferline = &screenbuf[lineindex(line)]; /* truncate the line to conform to our current screen width */ - newline[screen_width] = '\0'; + int len = strlen(newline); + if (screen_width < len) + { + newline[screen_width] = '\0'; + } /* is line higher than we went on the last display? */ if (line >= last_hi) @@ -878,7 +889,10 @@ u_process(int line, char *newline) optr = stpcpy(bufferline, newline); /* zero fill the rest of it */ - memset(optr, 0, screen_width - (optr - bufferline)); + if (optr - bufferline < screen_width) + { + memset(optr, 0, screen_width - (optr - bufferline)); + } } else { Modified: stable/12/usr.bin/top/display.h ============================================================================== --- stable/12/usr.bin/top/display.h Wed Feb 20 20:17:54 2019 (r344381) +++ stable/12/usr.bin/top/display.h Wed Feb 20 20:28:48 2019 (r344382) @@ -27,7 +27,7 @@ void i_timeofday(time_t *tod); void i_uptime(struct timeval *bt, time_t *tod); void new_message(int type, const char *msgfmt, ...); int readline(char *buffer, int size, int numeric); -const char *trim_header(const char *text); +char *trim_header(const char *text); void u_arc(int *stats); void u_carc(int *stats); void u_cpustates(int *states); Modified: stable/12/usr.bin/top/machine.c ============================================================================== --- stable/12/usr.bin/top/machine.c Wed Feb 20 20:17:54 2019 (r344381) +++ stable/12/usr.bin/top/machine.c Wed Feb 20 20:28:48 2019 (r344382) @@ -618,7 +618,7 @@ get_old_proc(struct kinfo_proc *pp) pp->ki_udata = NOPROC; return (NULL); } - pp->ki_udata = oldp; + pp->ki_udata = __DECONST(void *, oldp); return (oldp); } @@ -634,7 +634,7 @@ get_io_stats(const struct kinfo_proc *pp, long *inp, l static struct kinfo_proc dummy; long ret; - oldp = get_old_proc(pp); + oldp = get_old_proc(__DECONST(struct kinfo_proc *, pp)); if (oldp == NULL) { memset(&dummy, 0, sizeof(dummy)); oldp = &dummy; Modified: stable/12/usr.bin/top/top.c ============================================================================== --- stable/12/usr.bin/top/top.c Wed Feb 20 20:17:54 2019 (r344381) +++ stable/12/usr.bin/top/top.c Wed Feb 20 20:28:48 2019 (r344382) @@ -110,7 +110,8 @@ static const struct option longopts[] = { { "uids", no_argument, NULL, 'u' }, { "version", no_argument, NULL, 'v' }, { "swap", no_argument, NULL, 'w' }, - { "system-idle-procs", no_argument, NULL, 'z' } + { "system-idle-procs", no_argument, NULL, 'z' }, + { NULL, 0, NULL, 0 } }; static void @@ -218,7 +219,7 @@ end: } int -main(int argc, char *argv[]) +main(int argc, const char *argv[]) { int i; int active_procs; @@ -305,7 +306,7 @@ main(int argc, char *argv[]) optind = 1; } - while ((i = getopt_long(ac, av, "CSIHPabijJ:nquvzs:d:U:m:o:p:Ttw", longopts, NULL)) != EOF) + while ((i = getopt_long(ac, __DECONST(char * const *, av), "CSIHPabijJ:nquvzs:d:U:m:o:p:Ttw", longopts, NULL)) != EOF) { switch(i) { Modified: stable/12/usr.bin/top/username.c ============================================================================== --- stable/12/usr.bin/top/username.c Wed Feb 20 20:17:54 2019 (r344381) +++ stable/12/usr.bin/top/username.c Wed Feb 20 20:28:48 2019 (r344382) @@ -70,7 +70,7 @@ username(int uid) } int -userid(char username[]) +userid(char username_[]) { struct passwd *pwd; @@ -78,13 +78,13 @@ userid(char username[]) but for now we just do it simply and remember just the result. */ - if ((pwd = getpwnam(username)) == NULL) + if ((pwd = getpwnam(username_)) == NULL) { return(-1); } /* enter the result in the hash table */ - enter_user(pwd->pw_uid, username, 1); + enter_user(pwd->pw_uid, username_, 1); /* return our result */ return(pwd->pw_uid); Modified: stable/12/usr.bin/top/utils.c ============================================================================== --- stable/12/usr.bin/top/utils.c Wed Feb 20 20:17:54 2019 (r344381) +++ stable/12/usr.bin/top/utils.c Wed Feb 20 20:28:48 2019 (r344382) @@ -146,7 +146,7 @@ string_index(const char *string, const char * const *a * squat about quotes. */ -const char * const * +const char ** argparse(char *line, int *cntp) { const char **ap; @@ -292,11 +292,11 @@ char * format_k(int64_t amt) { static char retarray[NUM_STRINGS][16]; - static int index = 0; + static int index_ = 0; char *ret; - ret = retarray[index]; - index = (index + 1) % NUM_STRINGS; + ret = retarray[index_]; + index_ = (index_ + 1) % NUM_STRINGS; humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE); return (ret); } Modified: stable/12/usr.bin/top/utils.h ============================================================================== --- stable/12/usr.bin/top/utils.h Wed Feb 20 20:17:54 2019 (r344381) +++ stable/12/usr.bin/top/utils.h Wed Feb 20 20:28:48 2019 (r344382) @@ -16,7 +16,7 @@ int atoiwi(const char *); char *itoa(unsigned int); char *itoa7(int); int digits(int); -const char * const *argparse(char *, int *); +const char **argparse(char *, int *); long percentages(int, int *, long *, long *, long *); const char *format_time(long); char *format_k(int64_t);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201902202028.x1KKSmDq069247>