From owner-svn-src-head@FreeBSD.ORG Sun Dec 18 09:08:20 2011 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B122E106564A; Sun, 18 Dec 2011 09:08:20 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from mail09.syd.optusnet.com.au (mail09.syd.optusnet.com.au [211.29.132.190]) by mx1.freebsd.org (Postfix) with ESMTP id 3CCD78FC0A; Sun, 18 Dec 2011 09:08:19 +0000 (UTC) Received: from c211-28-227-231.carlnfd1.nsw.optusnet.com.au (c211-28-227-231.carlnfd1.nsw.optusnet.com.au [211.28.227.231]) by mail09.syd.optusnet.com.au (8.13.1/8.13.1) with ESMTP id pBI98GT6000654 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 18 Dec 2011 20:08:18 +1100 Date: Sun, 18 Dec 2011 20:08:16 +1100 (EST) From: Bruce Evans X-X-Sender: bde@besplex.bde.org To: Dimitry Andric In-Reply-To: <201112180040.pBI0eVKi084246@svn.freebsd.org> Message-ID: <20111218194714.W1622@besplex.bde.org> References: <201112180040.pBI0eVKi084246@svn.freebsd.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org Subject: Re: svn commit: r228678 - head/usr.sbin/rpc.ypupdated X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 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: Sun, 18 Dec 2011 09:08:20 -0000 On Sun, 18 Dec 2011, Dimitry Andric wrote: > Log: > In usr.sbin/rpc.ypupdated/yp_dbupdate.c, use the appropriate printf > length modifier for time_t (after casting it to intmax_t). > > MFC after: 1 week > > Modified: > head/usr.sbin/rpc.ypupdated/yp_dbupdate.c > > Modified: head/usr.sbin/rpc.ypupdated/yp_dbupdate.c > ============================================================================== > --- head/usr.sbin/rpc.ypupdated/yp_dbupdate.c Sun Dec 18 00:34:42 2011 (r228677) > +++ head/usr.sbin/rpc.ypupdated/yp_dbupdate.c Sun Dec 18 00:40:30 2011 (r228678) > @@ -130,7 +130,7 @@ ypmap_update(char *netname, char *map, u > return(rval); > } > > - snprintf(yplastbuf, sizeof(yplastbuf), "%lu", time(NULL)); > + snprintf(yplastbuf, sizeof(yplastbuf), "%ju", (intmax_t)time(NULL)); > key.data = yp_last; > key.size = strlen(yp_last); > data.data = (char *)&yplastbuf; It still has a sign mismatch. time_t can be unsigned, but in practice isn't. Now a sign mismatch is certain, since intmax_t is signed but the format is still unsigned. If time() somehow failed, it would return -1 and this is printed as an inscrutable huge number (1844mumble with 64-bit intmax_t). If time_t were unsigned, then there would be another sign error converting it to intmax_t. Casting to intmax_t is silly for time_t, at least for current times. Many places cast to long. This is guaranteed to work until 2038 even on i386. It will keep working past then provided long is enlarged before then. I think long is already 64 bits on all arches that have 64-bit time_t. Determining the appropriate printf modifier for time_t is especially difficult, even in POSIX, since POSIX still allows it to be a floating point type (it now says "integer or real floating type" where it used to say "arithmetic type". C still says "arithmetic type", and that now seems to allow it to be a complex floating point type too). In C, there might be no appropriate modifier, since the encoding is unspecified but code like the above wants seconds [since the Epoch]. Apart from that, determination of the appropriate modifier requires either: A. first determine if it is floating point. If so, cast to long double, etc. else determine if it is signed. If so, cast to intmax_t, etc. else cast to uintmax_t. B. just assume that it is signed integer and cast to the simplest type that works. I prefer plain long. Bruce