git: 61456545d957 - stable/13 - xdr: store chars consistently

From: Brooks Davis <brooks_at_FreeBSD.org>
Date: Wed, 18 Jan 2023 19:13:26 UTC
The branch stable/13 has been updated by brooks:

URL: https://cgit.FreeBSD.org/src/commit/?id=61456545d957be9a41aa83f5178b73509a4c2a1f

commit 61456545d957be9a41aa83f5178b73509a4c2a1f
Author:     Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2023-01-18 19:13:15 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2023-01-18 19:13:15 +0000

    xdr: store chars consistently
    
    Cast char's through unsigned char before storing as an integer in
    xdr_char(), this ensures that the encoded form is consistently not
    sign-extended following Open Solaris's example.
    
    Prior to this change, platforms with signed chars would sign extend
    values with the high bit set but ones with unsigned chars would not
    so 0xff would be stored as 0x000000ff on unsigned char platforms and
    0xffffffff on signed char platforms.  Decoding has the same
    result for either form so this is a largely cosmetic change, but it
    seems best to produce consistent output.
    
    For more discussion, see https://github.com/openzfs/zfs/issues/14173
    
    Reviewed by:    mav, imp
    Differential Revision:  https://reviews.freebsd.org/D37992
    
    (cherry picked from commit a872c37054172f3f7a03aef263ca5886a749771f)
---
 lib/libc/xdr/xdr.c | 8 ++++----
 sys/xdr/xdr.c      | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/lib/libc/xdr/xdr.c b/lib/libc/xdr/xdr.c
index fcf4e9029074..48b4cdbecd02 100644
--- a/lib/libc/xdr/xdr.c
+++ b/lib/libc/xdr/xdr.c
@@ -429,13 +429,13 @@ xdr_uint16_t(XDR *xdrs, uint16_t *u_int16_p)
 bool_t
 xdr_char(XDR *xdrs, char *cp)
 {
-	int i;
+	u_int i;
 
-	i = (*cp);
-	if (!xdr_int(xdrs, &i)) {
+	i = *((unsigned char *)cp);
+	if (!xdr_u_int(xdrs, &i)) {
 		return (FALSE);
 	}
-	*cp = i;
+	*((unsigned char *)cp) = i;
 	return (TRUE);
 }
 
diff --git a/sys/xdr/xdr.c b/sys/xdr/xdr.c
index 0a0bf3f66744..b57eb7d66ef4 100644
--- a/sys/xdr/xdr.c
+++ b/sys/xdr/xdr.c
@@ -358,13 +358,13 @@ xdr_uint16_t(XDR *xdrs, uint16_t *uint16_p)
 bool_t
 xdr_char(XDR *xdrs, char *cp)
 {
-	int i;
+	u_int i;
 
-	i = (*cp);
-	if (!xdr_int(xdrs, &i)) {
+	i = *((unsigned char *)cp);
+	if (!xdr_u_int(xdrs, &i)) {
 		return (FALSE);
 	}
-	*cp = i;
+	*((unsigned char *)cp) = i;
 	return (TRUE);
 }