From owner-svn-src-head@freebsd.org Thu Dec 1 16:54:03 2016 Return-Path: Delivered-To: svn-src-head@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 F255AC617D2; Thu, 1 Dec 2016 16:54:03 +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 C1D241DC2; Thu, 1 Dec 2016 16:54:03 +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 uB1Gs2XC023356; Thu, 1 Dec 2016 16:54:02 GMT (envelope-from vangyzen@FreeBSD.org) Received: (from vangyzen@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uB1Gs2s4023355; Thu, 1 Dec 2016 16:54:02 GMT (envelope-from vangyzen@FreeBSD.org) Message-Id: <201612011654.uB1Gs2s4023355@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: vangyzen set sender to vangyzen@FreeBSD.org using -f From: Eric van Gyzen Date: Thu, 1 Dec 2016 16:54:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r309364 - head/usr.bin/locale X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 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: Thu, 01 Dec 2016 16:54:04 -0000 Author: vangyzen Date: Thu Dec 1 16:54:02 2016 New Revision: 309364 URL: https://svnweb.freebsd.org/changeset/base/309364 Log: locale: fix buffer management Also, handle signed and unsigned chars, and more gracefully handle invalid input. Submitted by: bde in response to r309331 MFC after: 1 week Sponsored by: Dell EMC Modified: head/usr.bin/locale/locale.c Modified: head/usr.bin/locale/locale.c ============================================================================== --- head/usr.bin/locale/locale.c Thu Dec 1 15:46:26 2016 (r309363) +++ head/usr.bin/locale/locale.c Thu Dec 1 16:54:02 2016 (r309364) @@ -495,29 +495,29 @@ 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; - } + if (*cp < 0) + break; /* garbage input */ + 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 */ } - /* Remove the trailing ';'. */ - rval[len - 1] = '\0'; + /* 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()) */