From owner-svn-src-head@freebsd.org Thu May 26 20:55:16 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 62652B4B72D; Thu, 26 May 2016 20:55:16 +0000 (UTC) (envelope-from ed@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 1629814B7; Thu, 26 May 2016 20:55:16 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u4QKtF8h017469; Thu, 26 May 2016 20:55:15 GMT (envelope-from ed@FreeBSD.org) Received: (from ed@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u4QKtFcJ017468; Thu, 26 May 2016 20:55:15 GMT (envelope-from ed@FreeBSD.org) Message-Id: <201605262055.u4QKtFcJ017468@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ed set sender to ed@FreeBSD.org using -f From: Ed Schouten Date: Thu, 26 May 2016 20:55:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r300775 - head/lib/libc/stdlib 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.22 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, 26 May 2016 20:55:16 -0000 Author: ed Date: Thu May 26 20:55:15 2016 New Revision: 300775 URL: https://svnweb.freebsd.org/changeset/base/300775 Log: Let l64a() properly null terminate its result. Though the buffer used by l64a() is initialized with null bytes, repetetive calls may end up having trailing garbage of previous invocations because we don't end up terminating the string. Instead of importing NetBSD's fix, use this opportunity to simplify this function dramatically, for example by just storing the Base64 character set in a string. There is also no need to do the bitmasking, as we can just use the proper integer type from . MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D6511 Modified: head/lib/libc/stdlib/l64a.c Modified: head/lib/libc/stdlib/l64a.c ============================================================================== --- head/lib/libc/stdlib/l64a.c Thu May 26 20:37:49 2016 (r300774) +++ head/lib/libc/stdlib/l64a.c Thu May 26 20:55:15 2016 (r300775) @@ -12,18 +12,13 @@ __RCSID("$NetBSD: l64a.c,v 1.13 2003/07/ #include __FBSDID("$FreeBSD$"); +#include #include -#define ADOT 46 /* ASCII '.' */ -#define ASLASH ADOT + 1 /* ASCII '/' */ -#define A0 48 /* ASCII '0' */ -#define AA 65 /* ASCII 'A' */ -#define Aa 97 /* ASCII 'a' */ - char * l64a(long value) { - static char buf[8]; + static char buf[7]; (void)l64a_r(value, buf, sizeof(buf)); return (buf); @@ -32,21 +27,18 @@ l64a(long value) int l64a_r(long value, char *buffer, int buflen) { - long v; - int digit; - - v = value & (long)0xffffffff; - for (; v != 0 && buflen > 1; buffer++, buflen--) { - digit = v & 0x3f; - if (digit < 2) - *buffer = digit + ADOT; - else if (digit < 12) - *buffer = digit + A0 - 2; - else if (digit < 38) - *buffer = digit + AA - 12; - else - *buffer = digit + Aa - 38; + static const char chars[] = + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + uint32_t v; + + v = value; + while (buflen-- > 0) { + if (v == 0) { + *buffer = '\0'; + return (0); + } + *buffer++ = chars[v & 0x3f]; v >>= 6; } - return (v == 0 ? 0 : -1); + return (-1); }