From netch@aleph.carrier.kiev.ua Tue Oct  5 08:45:28 1999
Return-Path: <netch@aleph.carrier.kiev.ua>
Received: from aleph.carrier.kiev.ua (aleph.carrier.kiev.ua [193.193.193.3])
	by hub.freebsd.org (Postfix) with ESMTP id 9B81014D10
	for <FreeBSD-gnats-submit@freebsd.org>; Tue,  5 Oct 1999 08:43:48 -0700 (PDT)
	(envelope-from netch@aleph.carrier.kiev.ua)
Received: (from netch@localhost)
	by aleph.carrier.kiev.ua (8.9.3-lucky-netch-pl10/8.9.3) id SQU07564;
	Tue, 5 Oct 1999 18:43:37 +0300 (EEST)
	(envelope-from netch)
Message-Id: <199910051543.SQU07564@aleph.carrier.kiev.ua>
Date: Tue, 5 Oct 1999 18:43:37 +0300 (EEST)
From: netch@lucky.net (Valentin Nechayev)
Sender: netch@aleph.carrier.kiev.ua
Reply-To: netch@lucky.net
To: FreeBSD-gnats-submit@freebsd.org
Subject: sendmail: mci.c: bad pointer conversion in debug print
X-Send-Pr-Version: 3.2

>Number:         14142
>Category:       bin
>Synopsis:       sendmail: mci.c: bad pointer conversion in debug print
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    gshapiro
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Oct  5 08:50:01 PDT 1999
>Closed-Date:    Tue Feb 27 18:57:37 PST 2001
>Last-Modified:  Tue Feb 27 18:57:52 PST 2001
>Originator:     Valentin Nechayev
>Release:        FreeBSD 3.3-STABLE alpha
>Organization:
Lucky Net Ltd.
>Environment:

FreeBSD 3.3-STABLE on Alpha

>Description:

The following piece of mci_dump() uses bad conversion:

        snprintf(p, SPACELEFT(buf, p), "MCI@%lx: ",
                sizeof(void *) == sizeof(u_long) ?
                (u_long)(void *)mci : (u_long)(u_int)(void *)mci);
 
On Alpha architecture, conversion from void* to u_int loses significant bits.
These conversions are FreeBSD-specific; original Allman's sendmail
does not contain them.

>How-To-Repeat:

Read and compile the code ;)

>Fix:

Convert to ptrdiff_t instead of u_int (?)

>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: Valentin Nechayev <netch@lucky.net>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/14142: sendmail: mci.c: bad pointer conversion in debug
 print
Date: Wed, 6 Oct 1999 10:59:25 +1000 (EST)

 > The following piece of mci_dump() uses bad conversion:
 > 
 >         snprintf(p, SPACELEFT(buf, p), "MCI@%lx: ",
 >                 sizeof(void *) == sizeof(u_long) ?
 >                 (u_long)(void *)mci : (u_long)(u_int)(void *)mci);
 >  
 > On Alpha architecture, conversion from void* to u_int loses significant bits.
 
 On FreeBSD-alpha sizeof(void *) == sizeof(long), so the conversion from
 void * to u_int is never executed.  Unfortunately, gcc apparently warns
 about casts from pointers to integers of a different size even in dead
 code.
 
 On FreeBSD_i386-with-64-bit-longs, gcc warns about the dead code in the
 other arm of the if and about 3 casts from pointers to u_longs.  All these
 problems can be fixed better now by casting pointers to
 (u_long)(uintptr_t)(void *) and printing them with %lx, or if the format
 doesn't matter, by casting pointers to (void *) and printing them with %p.
 
 > These conversions are FreeBSD-specific; original Allman's sendmail
 > does not contain them.
 
 The original code was broken at runtime (it shows only the low 32 bits of
 pointers on FreeBSD-alpha).
 
 Bruce
 
 

From: "Valentin Nechayev" <nn@nn.kiev.ua>
To: bde@zeta.org.au
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/14142: sendmail: mci.c: bad pointer conversion in debug
    print
Date: Wed,  6 Oct 1999 10:03:10 +0300 (EDT)

 Bruce Evans <bde@zeta.org.au> wrote:
 
 > On FreeBSD-alpha sizeof(void *) == sizeof(long), so the conversion from
 > void * to u_int is never executed.  Unfortunately, gcc apparently warns
 > about casts from pointers to integers of a different size even in dead
 > code.
 >
 > On FreeBSD_i386-with-64-bit-longs, gcc warns about the dead code in the
 > other arm of the if and about 3 casts from pointers to u_longs.  All these
 > problems can be fixed better now by casting pointers to
 > (u_long)(uintptr_t)(void *) and printing them with %lx, or if the format
 > doesn't matter, by casting pointers to (void *) and printing them with %p.
 
 Well, shall it be better to use only %p and casting to void*, as "the only
 really right and portable way"?
 
 > > These conversions are FreeBSD-specific; original Allman's sendmail
 > > does not contain them.
 >
 > The original code was broken at runtime (it shows only the low 32 bits of
 > pointers on FreeBSD-alpha).
 
 Of course, original code is broken also and really. Do you know the reason
 of using integral formats instead of %p in original code?
 
 
 
 

From: Bruce Evans <bde@zeta.org.au>
To: netch@lucky.net
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/14142: sendmail: mci.c: bad pointer conversion in debug   
 print
Date: Wed, 6 Oct 1999 18:46:08 +1000 (EST)

 On Wed, 6 Oct 1999, Valentin Nechayev wrote:
 
 > Bruce Evans <bde@zeta.org.au> wrote:
 > > ...  All these
 > > problems can be fixed better now by casting pointers to
 > > (u_long)(uintptr_t)(void *) and printing them with %lx, or if the format
 > > doesn't matter, by casting pointers to (void *) and printing them with %p.
 
 Except uintptr_t is not supported in RELENG_3.
 
 > > The original code was broken at runtime (it shows only the low 32 bits of
 > > pointers on FreeBSD-alpha).
 > 
 > Of course, original code is broken also and really. Do you know the reason
 > of using integral formats instead of %p in original code?
 
 It's probably "portability".  Some systems don't/didn't have %p.
 
 Bruce
 
 
Responsible-Changed-From-To: freebsd-bugs->gshapiro 
Responsible-Changed-By: gshapiro 
Responsible-Changed-When: Sat Aug 12 10:14:19 PDT 2000 
Responsible-Changed-Why:  
Assigned to sendmail maintainer. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=14142 
State-Changed-From-To: open->analyzed 
State-Changed-By: gshapiro 
State-Changed-When: Wed Feb 21 22:49:54 PST 2001 
State-Changed-Why:  
I don't understand why FreeBSD has a different version of code for that. 
The original sendmail code appears to be better for this case unless I am 
missing something.  I tried a test.  Give try.c: 

#include <sys/stat.h> 
#include <sys/types.h> 
#include <stdio.h> 

int 
main(int argc, char **argv) 
{ 
struct stat b; 
struct stat *a; 

a = &b; 
printf(" no cast: %lxn", a); 
printf("sendmail: %lxn", (u_long) a); 
printf(" FreeBSD: %lxn", 
sizeof(void *) == sizeof(u_long) ? 
(u_long)(void *)a : (u_long)(u_int)(void *)a); 
} 

Compiling on beast.freebsd.org gives a warning on the FreeBSD case: 

> cc -mcpu=ev56 -O -pipe  try.c  -o try 
try.c: In function `main': 
try.c:16: warning: cast from pointer to integer of different size 

But all three cases give the same result: 

> ./try 
no cast: 11ffb7e8 
sendmail: 11ffb7e8 
FreeBSD: 11ffb7e8 

Unless I am mistaken, the FreeBSD modifications should be removed. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=14142 
State-Changed-From-To: analyzed->feedback 
State-Changed-By: gshapiro 
State-Changed-When: Thu Feb 22 09:57:14 PST 2001 
State-Changed-Why:  
Return to the code as distributed by sendmail.org.  This eliminates a 
warning on Alphas.  It is still not the perfect solution for machines 
which sizeof(u_long) != sizeof(void *) but it is as close as we are going 
to get for now and consistent with the rest of the code.  8.12 has solved 
this problem by providing a portable snprintf() which understands %p. 

Unless I hear otherwise, I'll close this PR in the near future. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=14142 
State-Changed-From-To: feedback->closed 
State-Changed-By: gshapiro 
State-Changed-When: Tue Feb 27 18:57:37 PST 2001 
State-Changed-Why:  
The changes are now also in RELENG_4 (-STABLE). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=14142 
>Unformatted:
