From owner-svn-src-stable@freebsd.org Tue Dec 13 23:10:37 2016 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 2DD81C768A3; Tue, 13 Dec 2016 23:10:37 +0000 (UTC) (envelope-from vangyzen@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 mx1.freebsd.org (Postfix) with ESMTPS id DCC17CBE; Tue, 13 Dec 2016 23:10:36 +0000 (UTC) (envelope-from vangyzen@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uBDNAarc076655; Tue, 13 Dec 2016 23:10:36 GMT (envelope-from vangyzen@FreeBSD.org) Received: (from vangyzen@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uBDNAZVL076653; Tue, 13 Dec 2016 23:10:35 GMT (envelope-from vangyzen@FreeBSD.org) Message-Id: <201612132310.uBDNAZVL076653@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: vangyzen set sender to vangyzen@FreeBSD.org using -f From: Eric van Gyzen Date: Tue, 13 Dec 2016 23:10:35 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r310041 - stable/10/usr.bin/locale X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Dec 2016 23:10:37 -0000 Author: vangyzen Date: Tue Dec 13 23:10:35 2016 New Revision: 310041 URL: https://svnweb.freebsd.org/changeset/base/310041 Log: MFC r309364 r309367 r309624 locale: fix buffer management Also, handle signed and unsigned chars, and more gracefully handle invalid input. locale: enable more warnings; fix them Do not set WARNS, so it gets the current default of 6. Fix the warnings by sprinkling static, const, or strdup. Make some constant data tables const. Fix whitespace. Sponsored by: Dell EMC Modified: stable/10/usr.bin/locale/Makefile stable/10/usr.bin/locale/locale.c Directory Properties: stable/10/ (props changed) Modified: stable/10/usr.bin/locale/Makefile ============================================================================== --- stable/10/usr.bin/locale/Makefile Tue Dec 13 23:09:22 2016 (r310040) +++ stable/10/usr.bin/locale/Makefile Tue Dec 13 23:10:35 2016 (r310041) @@ -1,7 +1,6 @@ # $FreeBSD$ PROG= locale -WARNS?= 3 CFLAGS+= -I${.CURDIR}/../../lib/libc/locale .include Modified: stable/10/usr.bin/locale/locale.c ============================================================================== --- stable/10/usr.bin/locale/locale.c Tue Dec 13 23:09:22 2016 (r310040) +++ stable/10/usr.bin/locale/locale.c Tue Dec 13 23:10:35 2016 (r310041) @@ -55,8 +55,8 @@ void list_charmaps(void); void list_locales(void); const char *lookup_localecat(int); char *kwval_lconv(int); -int kwval_lookup(char *, char **, int *, int *); -void showdetails(char *); +int kwval_lookup(const char *, char **, int *, int *); +void showdetails(const char *); void showkeywordslist(char *substring); void showlocale(void); void usage(void); @@ -64,13 +64,12 @@ void usage(void); /* Global variables */ static StringList *locales = NULL; -int all_locales = 0; -int all_charmaps = 0; -int prt_categories = 0; -int prt_keywords = 0; -int more_params = 0; +static int all_locales = 0; +static int all_charmaps = 0; +static int prt_categories = 0; +static int prt_keywords = 0; -struct _lcinfo { +static const struct _lcinfo { const char *name; int id; } lcinfo [] = { @@ -108,7 +107,7 @@ struct _lcinfo { #define KW_INT_P_SIGN_POSN (KW_ZERO+21) #define KW_INT_N_SIGN_POSN (KW_ZERO+22) -struct _kwinfo { +static const struct _kwinfo { const char *name; int isstr; /* true - string, false - number */ int catid; /* LC_* */ @@ -222,7 +221,7 @@ struct _kwinfo { }; #define NKWINFO (sizeof(kwinfo)/sizeof(kwinfo[0])) -const char *boguslocales[] = { "UTF-8" }; +static const char *boguslocales[] = { "UTF-8" }; #define NBOGUS (sizeof(boguslocales)/sizeof(boguslocales[0])) int @@ -294,7 +293,7 @@ main(int argc, char *argv[]) } else { uint i; for (i = 0; i < sizeof (kwinfo) / sizeof (struct _kwinfo); i++) - showdetails ((char *)kwinfo [i].name); + showdetails(kwinfo[i].name); } exit(0); } @@ -339,7 +338,7 @@ list_locales(void) static int scmp(const void *s1, const void *s2) { - return strcmp(*(const char **)s1, *(const char **)s2); + return strcmp(*(const char * const *)s1, *(const char * const *)s2); } /* @@ -376,7 +375,7 @@ list_charmaps(void) /* add US-ASCII, if not yet added */ if (sl_find(charmaps, "US-ASCII") == NULL) - sl_add(charmaps, "US-ASCII"); + sl_add(charmaps, strdup("US-ASCII")); /* sort the list */ qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp); @@ -435,10 +434,10 @@ init_locales_list(void) * we also list 'C' for constistency */ if (sl_find(locales, "POSIX") == NULL) - sl_add(locales, "POSIX"); + sl_add(locales, strdup("POSIX")); if (sl_find(locales, "C") == NULL) - sl_add(locales, "C"); + sl_add(locales, strdup("C")); /* make output nicer, sort the list */ qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp); @@ -493,29 +492,31 @@ format_grouping(const char *binary) { static char rval[64]; const char *cp; - size_t len; + size_t roff; + int len; rval[0] = '\0'; + roff = 0; for (cp = binary; *cp != '\0'; ++cp) { - char group[sizeof("127;")]; - snprintf(group, sizeof(group), "%hhd;", *cp); - len = strlcat(rval, group, sizeof(rval)); - if (len >= sizeof(rval)) { - len = sizeof(rval) - 1; - break; - } - if (*cp == CHAR_MAX) { - break; - } - } - - /* Remove the trailing ';'. */ - rval[len - 1] = '\0'; +#if CHAR_MIN != 0 + if (*cp < 0) + break; /* garbage input */ +#endif + len = snprintf(&rval[roff], sizeof(rval) - roff, "%u;", *cp); + if (len < 0 || (unsigned)len >= sizeof(rval) - roff) + break; /* insufficient space for output */ + roff += len; + if (*cp == CHAR_MAX) + break; /* special termination */ + } + + /* Truncate at the last successfully snprintf()ed semicolon. */ + if (roff != 0) + rval[roff - 1] = '\0'; - return (rval); + return (&rval[0]); } - /* * keyword value lookup helper (via localeconv()) */ @@ -604,7 +605,7 @@ kwval_lconv(int id) * keyword value and properties lookup */ int -kwval_lookup(char *kwname, char **kwval, int *cat, int *isstr) +kwval_lookup(const char *kwname, char **kwval, int *cat, int *isstr) { int rval; size_t i; @@ -632,7 +633,7 @@ kwval_lookup(char *kwname, char **kwval, * command line options specified. */ void -showdetails(char *kw) +showdetails(const char *kw) { int isstr, cat, tmpval; char *kwval; @@ -647,9 +648,9 @@ showdetails(char *kw) } if (prt_categories) { - if (prt_keywords) + if (prt_keywords) printf("%-20s ", lookup_localecat(cat)); - else + else printf("%-20s\t%s\n", kw, lookup_localecat(cat)); }